【发布时间】:2019-09-03 21:43:59
【问题描述】:
我正在尝试在特殊类型的纸张上打印 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,..
【问题讨论】:
标签: java printing pdfbox java-print