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.