我使用PDFBox(1.8.12)从Java中打印pdf文档:
PDDocument pdf = PDDocument.load(new File(args[0]));
PrinterJob job = PrinterJob.getPrinterJob();
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(MediaSizeName.ISO_A4); // <<< supposedly prints in A4 format
attr_set.add(Sides.ONE_SIDED);
PDPageable p = new PDPageable(pdf);
job.setPageable(p);
PrintService ps = null;
for (PrintService i : PrintServiceLookup.lookupPrintServices(null,null)) {
if (i.getName().equals(args[1])) {
ps = i;
}
}
if (ps == null) {
try {
throw new SystemException(ErrorCode.NO_PRINTER_FOUND);
} catch (SystemException e) {
e.printStackTrace();
}
}
else
{
job.setPrintService(ps);
job.print(attr_set);
}
问题是打印的文档有一个被剪掉的边距,我不知道为什么。我进行了测试,将pdf循环回到pdf虚拟打印机中,它们似乎是相同的,这意味着PDFbox没有以错误的方式处理pdf。
进一步研究后,打印纸上的文本看起来像是被放大了,它开始得更高,结束得比原来低(当我直接从具体打印机打印时)。
我打印的纸张是A4格式,所以我尝试像上面那样设置格式为A4,但问题仍然存在。