admin管理员组文章数量:1306767
I am working on java project which prints on 58mm-110mm (variable on user choice) continuous thermal printer paper. I have successfully printed the contents but final output of paper size is not as intended. for 58mm width it works fine but for higher page width, there seems to be more blank size on the right.
public PageFormat getPageFormat(PrinterJob pj) {
//PageFormat pf = new PageFormat();
PageFormat pf = pj.getPageFormat(getPrintAttributes());
Paper paper = pf.getPaper();
paper.setSize(pw_PX, ph_PX);
paper.setImageableArea(plm_PX, // left margin
ptm_PX, // top margin
piw_PX,
pih_PX
);
// Set orientation and paper
pf.setOrientation(PageFormat.PORTRAIT);
pf.setPaper(paper);
pj.setPrintable(this);
return pf;
}
// ***************** creating media format
// @return
public HashPrintRequestAttributeSet getPrintAttributes() {
printAttributes = new HashPrintRequestAttributeSet();
printAttributes.add(new MediaPrintableArea(
plm_MM, ptm_MM,
piw_MM, pih_MM,
MediaPrintableArea.MM));
return printAttributes;
}
out put is
page width is received from use in MM and height is calculated according to a table data. on 58mm width it works fine, but not on higher widths. what is exactly is problem here. can anyone help?
i use overridden print method in printable class to print on to the paper
'''
@Override
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON);
g2d.translate(pf.getPaper().getImageableX(),
pf.getPaper().getImageableY());
int y = 0;
//draw headers
y += h1Height;
g2d.setFont(h1Font);
y = drawCenteredString(g2d, companyName, 0, y, (int) piw_PX);
...
(...code goes on to print more)
Later in my "Print" button function, I call a PrinterJob and set this printable class to PrinterJob.
本文标签: printingJava PageFormat not working for Custom Paper sizeStack Overflow
版权声明:本文标题:printing - Java PageFormat not working for Custom Paper size - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741821314a2399381.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论