コンテンツの位置が重要で、シフトが許可されていない特殊な種類の用紙に PDF を印刷しようとしています。
私は使用java.awt.print.PrinterJob
してい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);
}
}
元の PDF は次のようになります。
ただし、印刷されたものはシフトされています。
そのため、シフトの代わりに、ドキュメント全体のシフトではなく、破線の境界線が印刷されないことを期待しています。
残念ながら、内容を変更せずに PDF を印刷することはできませんでした..