We *_*org 3 java pdf file-conversion
我正在尝试创建由 Ballasamic Mockups 创建的 PDF 文件的预览。大约 50% 的情况下,我无法获得预览并出现外部参照缺失错误。我究竟做错了什么?
错误日志:
com.sun.pdfview.PDFParseException: Expected 'xref' at start of table
at com.sun.pdfview.PDFFile.readTrailer(PDFFile.java:974)
at com.sun.pdfview.PDFFile.parseFile(PDFFile.java:1175)
at com.sun.pdfview.PDFFile.<init>(PDFFile.java:126)
at com.sun.pdfview.PDFFile.<init>(PDFFile.java:102)
Run Code Online (Sandbox Code Playgroud)
代码 :
private byte[] onlyCreatePdfPreview(String path, int attachId) {
try {
File file = new File(path);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new com.sun.pdfview.PDFFile(buf);
PDFPage page = pdffile.getPage(0);
Rectangle rect = new Rectangle(0, 0,
(int) page.getBBox().getWidth(),
(int) page.getBBox().getHeight());
java.awt.Image img = page.getImage(
rect.width, rect.height, //width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage buffered = toBufferedImage(img);
buffered = Scalr.resize(buffered, Scalr.Method.ULTRA_QUALITY, 400, 250);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffered, "png", baos);
baos.flush();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?谢谢。
最终工作代码
try {
String pdfPath = zipLocation + String.valueOf(new BigInteger(130, random).toString(32));
PdfReader reader = new PdfReader(path);
PdfStamper pdfStamper = new PdfStamper(reader,new FileOutputStream(pdfPath));
pdfStamper.getWriter().setPdfVersion(PdfWriter.PDF_VERSION_1_4);
pdfStamper.close();
reader.close();
RandomAccessFile raf = new RandomAccessFile(pdfPath, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new com.sun.pdfview.PDFFile(buf);
PDFPage page = pdffile.getPage(0);
Rectangle rect = new Rectangle(0, 0,
(int) page.getBBox().getWidth(),
(int) page.getBBox().getHeight());
java.awt.Image img = page.getImage(
rect.width, rect.height, //width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage buffered = toBufferedImage(img);
buffered = Scalr.resize(buffered, Scalr.Method.ULTRA_QUALITY, 400, 250);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffered, "png", baos);
baos.flush();
return baos.toByteArray();
}//catch block
Run Code Online (Sandbox Code Playgroud)
(这个答案从对问题的评论中收集信息,最终得出解决方案。)
显然com.sun.pdfview.PDFFile期望交叉引用以xref开头。但这种期望仅对 2001 年 11 月发布的 PDF 参考修订版 3(版本 1.4)之后的 PDF 才有意义;遵循后续参考甚至 ISO 32000 标准(第 1 部分或第 2 部分)的 PDF 可以选择使用交叉引用流(以对象编号开头)而不是交叉引用表(以xref开头)。
因此,人们应该转而使用遵循更新规范的软件,而不是一些超过 15 年的旧版本,或者必须将 PDF 转换为遵循旧规范(至少表面上如此)。
可以手动转换(例如使用 Adobe Acrobat)或自动转换(例如使用 iText)。(这些示例软件产品实际上只是示例,其他产品也可以用于此任务。)
如果使用当前的 iText 5 版本,转换如下所示:
PdfReader reader = new PdfReader(SOURCE);
PdfStamper stamper = new PdfStamper(reader, DEST);
stamper.getWriter().setPdfVersion(PdfWriter.PDF_VERSION_1_4);
stamper.close();
reader.close();
Run Code Online (Sandbox Code Playgroud)
并且必须注意,如果SOURCE是文件名或随机访问文件,则DEST一定不能是同一文件的文件输出流。PdfStamper否则,在有机会将其全部复制到其输出之前,原始文件将被截断。
| 归档时间: |
|
| 查看次数: |
2471 次 |
| 最近记录: |