修复使用 Java 打印到收据打印机时的文本拉伸问题
我通过实现 Java Printable 并将我的代码放入 print 方法中来打印到一些 Epson 收据打印机。为了将文本绘制到打印机,我使用 Graphics2D.drawString。我还在打印机上绘制一个矩形,以了解在打印到其他打印机时如何与文本大小进行比较。当打印到收据打印机时,纸张上的文本宽度大约是激光打印机或 XPS 写入器虚拟打印的宽度的两倍。这是 Java 将文本绘制到 Graphics2D 对象的方式的问题吗?我安装了最新版本的 Java 6 update 20。
任何有关要研究的内容的想法都会有所帮助。
谢谢。
这是我正在使用的代码。在此示例中,当将其发送到 XPS 写入器时,我在矩形的右边缘看到字母“c”,如果我将其打印到收据打印机,则 6 位于矩形的右边缘,您可以告诉文本比应有的宽得多。矩形似乎大小正确。
我尝试过更改页面和边距大小,但它似乎无法解决我的文本问题。我通过 Microsoft Word 自动检测打印机的方式获得了这些纸张尺寸和边距。 Word 将文本正确打印到收据打印机。
public static void main(String[] args) {
PageFormat format = new PageFormat();
Paper paper = new Paper();
double paperWidth = 3.25;
double paperHeight = 11.69;
double leftMargin = 0.19;
double rightMargin = 0.25;
double topMargin = 0;
double bottomMargin = 0.01;
paper.setSize(paperWidth * 72.0, paperHeight * 72.0);
paper.setImageableArea(leftMargin * 72.0, topMargin * 72.0,
(paperWidth - leftMargin - rightMargin) * 72.0,
(paperHeight - topMargin - bottomMargin) * 72.0);
format.setPaper(paper);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
PrinterJob printerJob = PrinterJob.getPrinterJob();
Printable printable = new ReceiptPrintTest();
format = printerJob.validatePage(format);
printerJob.setPrintable(printable, format);
try {
printerJob.print(aset);
}
catch (Exception e) {
e.printStackTrace();
}
}
public class ReceiptPrintTest implements Printable {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex < 0 || pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
Font font = new Font("Arial",Font.PLAIN, 14);
g2d.setFont(font);
g2d.drawString("1234567890abcdefg", 50, 70);
g2d.drawRect(50, 0, 100, 50);
return Printable.PAGE_EXISTS;
}
I am printing to some Epson receipt printers by implementing the Java Printable and placing my code into the print method. To draw the text to the printer I use Graphics2D.drawString. I am also drawing a rect to the printer to see how to compares to the text size when printing to other printers. When printing to the receipt printer the text on the paper is about double the width of printing to a laser printer or the XPS writer virtual print. Is this a problem with the way Java draws text to the Graphics2D object? I have the newest version of Java installed of 6 update 20.
Any Ideas of what to look into would be helpful.
Thanks.
Here the code I am using. With this example I am seeing the letter 'c' on the right edge of the rect when sending it to a XPS writer and if I print it to my receipt printer the 6 is on the right edge of the rect and you can tell the text is much wider then it should be. The rect seems to be the correct size.
I have tried changing the page and margin sizes but it does not seem to fix my text problem. I got these paper sizes and margins from how Microsoft Word is auto detecting the printer. Word prints the text correctly to the receipt printer.
public static void main(String[] args) {
PageFormat format = new PageFormat();
Paper paper = new Paper();
double paperWidth = 3.25;
double paperHeight = 11.69;
double leftMargin = 0.19;
double rightMargin = 0.25;
double topMargin = 0;
double bottomMargin = 0.01;
paper.setSize(paperWidth * 72.0, paperHeight * 72.0);
paper.setImageableArea(leftMargin * 72.0, topMargin * 72.0,
(paperWidth - leftMargin - rightMargin) * 72.0,
(paperHeight - topMargin - bottomMargin) * 72.0);
format.setPaper(paper);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
PrinterJob printerJob = PrinterJob.getPrinterJob();
Printable printable = new ReceiptPrintTest();
format = printerJob.validatePage(format);
printerJob.setPrintable(printable, format);
try {
printerJob.print(aset);
}
catch (Exception e) {
e.printStackTrace();
}
}
public class ReceiptPrintTest implements Printable {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex < 0 || pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
Font font = new Font("Arial",Font.PLAIN, 14);
g2d.setFont(font);
g2d.drawString("1234567890abcdefg", 50, 70);
g2d.drawRect(50, 0, 100, 50);
return Printable.PAGE_EXISTS;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用 setFont 设置字体?
Have you tried setting the font using
setFont
?