I'm trying to print PDF on special type of paper, where content's position matter, and shift is not allowed.
I'm using java.awt.print.PrinterJob
and org.apache.pdfbox.printing.PDFPrintable
:
public void printPDF(byte[] pdf) throws IOException, PrinterException {
MediaSize media = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A4);
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(media.getMediaSizeName());
attributes.add(new MediaPrintableArea(
0, 0, media.getSize(1)[0], media.getSize(1)[1],
1
));
PrinterJob job = PrinterJob.getPrinterJob();
if (job.printDialog(attributes)) {
PageFormat pageFormat = (PageFormat) job.getPageFormat(attributes).clone();
Paper paper = pageFormat.getPaper();
paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
pageFormat.setPaper(paper);
PDDocument document = PDDocument.load(pdf);
PDFPrintable printable = new PDFPrintable(
document,
Scaling.ACTUAL_SIZE,
false,
0,
false
);
job.setPrintable(printable, pageFormat);
job.print(attributes);
}
}
The original PDF looks like this:
But, the printed one is shifted:
So, instead of shift I expect the dashed border to be not printed instead of shift of the whole document.
Unfortunately, I wasn't able to print the PDF without shifting the content,..