最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

printing - Java PageFormat not working for Custom Paper size - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论