如何正确打印带有“text/rtf”的 JTextPane 硬拷贝内容?
我正在尝试使用 JTextPane
将一些简单的 RTF 格式的文本打印到激光打印机。
结果在软件 PDF 打印机 (FreePDF XP) 上看起来不错,但在打印到真实打印机时,文本的格式化部分之间没有适当的空间。
编辑:我已经上传了示例输出(底部是扫描的打印输出)
示例http://ompldr.org/vNXo4Zg/output.png
在我看来,Graphics 对象开始绘制 RTF 代码的各个部分时出现问题。好像它无法找出正确放置每个部分的位置(X 坐标)。
我是否必须提供某种坐标系转换?
使用的简单测试代码:
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JFrame;
import javax.swing.JTextPane;
class MyTextComp extends JTextPane implements Printable
{
public MyTextComp()
{
setContentType("text/rtf");
setText("{\\rtf1 HelloWorld! \\par {\\i This} is formatted {\\b Text}.}");
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
public int print(Graphics g, PageFormat pf, int pIndex)
{
if(pIndex > 0)
return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now print the window and its visible contents */
printAll(g);
return Printable.PAGE_EXISTS;
}
}
public class TextCompPrint extends JFrame
{
public static void main(String[] args) throws PrinterException
{
TextCompPrint myFrame = new TextCompPrint();
MyTextComp myComp = new MyTextComp();
myFrame.add(myComp, BorderLayout.CENTER);
myFrame.setSize(200, 200);
myFrame.setVisible(true);
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(myComp);
pj.print();
}
}
I'm trying to print out some simple RTF-formatted text to a laser printer using a JTextPane
.
The result looks fine on a software PDF printer (FreePDF XP), but the text doesn't have proper space between it's formatted parts when print to a real printer.
Edit: I have uploaded an example output (The bottom is the scanned printout)
Example http://ompldr.org/vNXo4Zg/output.png
It seems to me that there is a problem with the Graphics object starting to paint the indiviual parts of the RTF code. As if it couldn't figure out where to correctly put each part (the X coordinate).
Do I have to provide some kind of coordinate system translation?
The simple test code used:
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JFrame;
import javax.swing.JTextPane;
class MyTextComp extends JTextPane implements Printable
{
public MyTextComp()
{
setContentType("text/rtf");
setText("{\\rtf1 HelloWorld! \\par {\\i This} is formatted {\\b Text}.}");
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
public int print(Graphics g, PageFormat pf, int pIndex)
{
if(pIndex > 0)
return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now print the window and its visible contents */
printAll(g);
return Printable.PAGE_EXISTS;
}
}
public class TextCompPrint extends JFrame
{
public static void main(String[] args) throws PrinterException
{
TextCompPrint myFrame = new TextCompPrint();
MyTextComp myComp = new MyTextComp();
myFrame.add(myComp, BorderLayout.CENTER);
myFrame.setSize(200, 200);
myFrame.setVisible(true);
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(myComp);
pj.print();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢迎来到地狱。稍等一下 :-)
Java 使用一些复杂的代码来布局打印机的文本(因此它不会发送
用粗体字体打印“文本”
,而是选择 Times-Bold
code>,将光标移动到 x,y
,绘制字母“T”
,移动到 x2,y
, 绘制字母“e ", ...`你的问题是 Java 和你的打印机对字符的宽度有不同的想法。如果你仔细观察,粗体文本
Text
的字母间隔有点宽。如何才能你能解决这个问题吗?尝试使用不同的字体,直到它起作用为止。我不知道有什么方法可以使用Java打印API下载轮廓字体
。 nofollow noreferrer">PDFBox 自己生成 PDF。
[编辑]Java 不是 DTP 系统。打印支持充其量只是基本的。
如果您需要更多,请考虑使用 OpenOffice 进行转换RTF 到 PDF 进行打印(请参阅有没有免费的方法将 RTF 转换为 PDF? 和 如何在服务器模式下将 OpenOffice 用作多线程服务?)。
或者使用 OpenOffice 作为文本窗格。
Welcome to hell. Stay a while :-)
Java uses some complex code to layout the text for the printer (so it doesn't send
print "Text" with a bold font
butselect Times-Bold
,Move the cursor to x,y
,Draw the letter "T"
,Move to x2,y
, Draw the letter "e", ...`Your problem is that Java and your printer have different ideas how wide the characters are. If you look closely, the letters of the bold face text
Text
are bit wide apart.How can you solve this? Try a different font until it works. I don't know any way to download outline fonts with the Java print API.
Or use PDFBox to generate PDF yourself.
[EDIT] Java is not a DTP system. The printing support is rudimentary at best.
If you need more, consider using OpenOffice to convert from RTF to PDF for printing (see Is there a free way to convert RTF to PDF? and How can I use OpenOffice in server mode as a multithreaded service?).
Or use OpenOffice as text pane.