我正在尝试使用java.awt.print包打印PDF文档。我这样做是通过创建一个 Printable 对象来对文件进行简单转换(这是一种前一段时间从不同主题派生而来的技术)。
private static void printDocument(File finalFile, PrintService printer, String name) throws PrinterException {
System.out.println(String.format("Starting %s document", name));
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(finalFile);
byte[] pdfContent = new byte[fileInputStream.available()];
fileInputStream.read(pdfContent, 0, fileInputStream.available());
ByteBuffer buffer = ByteBuffer.wrap(pdfContent);
final PDFFile pdfFile = new PDFFile(buffer);
//Create Printable object in preparation for printing
Printable printable = new Printable() {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
int pagenum = pageIndex + 1;
if ((pagenum >= 1) && (pagenum <= pdfFile.getNumPages())) {
Graphics2D graphics2D = (Graphics2D) graphics;
PDFPage page = pdfFile.getPage(pagenum);
Rectangle imageArea = new Rectangle((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY(),
(int) pageFormat.getImageableWidth(), (int) pageFormat.getImageableHeight());
graphics2D.translate(0, 0);
PDFRenderer pdfRenderer = new PDFRenderer(page, graphics2D, imageArea, null, null);
try {
page.waitForFinish();
pdfRenderer.run();
}
catch (InterruptedException e) {
e.printStackTrace();
throw new PrinterException("Oh noes!");
}
return PAGE_EXISTS;
}
else {
return NO_SUCH_PAGE;
}
}
};
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintService(printer);
PageFormat pageFormat = printJob.defaultPage();
Book book = new Book();
book.append(printable, pageFormat, pdfFile.getNumPages());
printJob.setPageable(book);
Paper paper = new Paper();
paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
pageFormat.setPaper(paper);
PrintRequestAttributeSet pset = new HashPrintRequestAttributeSet();
printJob.setJobName(finalFile.getName() +
"|" + String.valueOf(1) +
"|" + ("T"));
printJob.print(pset);
}
catch (Exception e) {
e.printStackTrace();
throw new PrinterException();
}
finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(String.format("Finished %s document", name));
}
某些特定案例的PDF文件对此转化过程反应不佳, pdfRenderer.run(); 行失败这个堆栈跟踪:
java.lang.ArithmeticException: / by zero
at com.sun.pdfview.pattern.PatternType1$Type1PaintContext.getRaster(PatternType1.java:354)
at sun.java2d.pipe.AlphaPaintPipe.renderPathTile(AlphaPaintPipe.java:106)
at sun.java2d.pipe.DuctusShapeRenderer.renderTiles(DuctusShapeRenderer.java:147)
at sun.java2d.pipe.DuctusShapeRenderer.renderPath(DuctusShapeRenderer.java:100)
at sun.java2d.pipe.DuctusShapeRenderer.fill(DuctusShapeRenderer.java:50)
at sun.java2d.pipe.PixelToParallelogramConverter.fill(PixelToParallelogramConverter.java:146)
at sun.java2d.pipe.ValidatePipe.fill(ValidatePipe.java:142)
at sun.java2d.SunGraphics2D.fill(SunGraphics2D.java:2376)
at sun.print.ProxyGraphics2D.fill(ProxyGraphics2D.java:1381)
at com.sun.pdfview.pattern.PatternType1$TilingPatternPaint.fill(PatternType1.java:278)
at com.sun.pdfview.PDFRenderer.fill(PDFRenderer.java:261)
at com.sun.pdfview.PDFShapeCmd.execute(PDFShapeCmd.java:77)
at com.sun.pdfview.PDFRenderer.iterate(PDFRenderer.java:570)
at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101)
at **MYFILEHERE**
这导致文件中途失败,并且在一个只打印中途的文件中(并且有点幽默)结果,甚至在文本行的中心停止。
这个错误的原因是什么?它在渲染过程中发生在如此低级别,处理个别形状操作,我不得不相信我无法控制用。 有没有办法我可以允许我的流程绕过损坏的数据它可能正在努力并继续完整呈现文档?