java 合并文件给出错误:文件结束,预期行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34871270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
merge files gives error: End-of-File, expected line
提问by royB
I'm Using PdfBox for androidin order to append data to a PDF
file.
我正在使用PdfBox for android将数据附加到PDF
文件。
The data to append
要追加的数据
public byte [] prerparePdfToAppend() {
final PDDocument document = new PDDocument();
final PDPage sourcePage = new PDPage();
document.addPage(sourcePage);
PDPageContentStream contentStream = new PDPageContentStream(document, sourcePage);
contentStream.beginText();
contentStream.setFont(PDType1Font.COURIER, 12);
contentStream.showText("Name: " + firstName + " " + lastName);
contentStream.newLine();
...
contentStream.endText();
contentStream.close();
output = new ByteArrayOutputStream();
document.save(output);
document.close();
byte [] bytesToAppend = new byte[output.size()];
output.write(bytes);
output.close();
return bytesToAppend;
}
Merge Code (simplified)
合并代码(简化)
public void merge (String assetFileName) {
byte [] toAppendPdf = prerparePdfToAppend();
PDFMergerUtility mergerUtility = new PDFMergerUtility();
mergerUtility.addSource(PDFBoxResourceLoader.getStream(assetFileName));
mergerUtility.addSource(new ByteArrayInputStream(toAppendPdf));
mergerUtility.setDestinationStream(destStream);
mergerUtility.mergeDocuments(); //IOException
}
The Exception
例外
java.io.IOException: Error: End-of-File, expected line
at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1419)
at org.apache.pdfbox.pdfparser.COSParser.parseHeader(COSParser.java:1648)
at org.apache.pdfbox.pdfparser.COSParser.parsePDFHeader(COSParser.java:1627)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:348)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:888)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:802)
at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:172)
回答by Tilman Hausherr
The last lines of the prerparePdfToAppend method look weird to me. But why make your life complicated? Return a PDDocument:
prerparePdfToAppend 方法的最后几行对我来说看起来很奇怪。但是为什么要让你的生活变得复杂呢?返回一个 PDDocument:
public PDDocument prerparePdfToAppend()
{
final PDDocument document = new PDDocument();
final PDPage sourcePage = new PDPage();
document.addPage(sourcePage);
PDPageContentStream contentStream = new PDPageContentStream(document, sourcePage);
contentStream.beginText();
contentStream.setFont(PDType1Font.COURIER, 12);
contentStream.showText("Name: " + firstName + " " + lastName);
contentStream.newLine();
...
contentStream.endText();
contentStream.close();
return document;
}
Your merge code would then look like this:
您的合并代码将如下所示:
public void merge (String assetFileName)
{
PDFMergerUtility mergerUtility = new PDFMergerUtility();
PDDocument srcDoc = PDDocument.load(PDFBoxResourceLoader.getStream(assetFileName));
PDDocument dstDoc = prerparePdfToAppend();
mergerUtility.appendDocument(dstDoc, srcDoc);
dstDoc.save(destStream);
srcDoc.close();
dstDoc.close();
}
If this doesn't work - make sure that
如果这不起作用 - 请确保
PDFBoxResourceLoader.getStream(assetFileName)
is really the stream of a real PDF. If it still doesn't work, mention which line of this new code produces the exception. And of course, make sure you're using the latest version of PDFBox.
是真正的 PDF 流。如果它仍然不起作用,请提及此新代码的哪一行产生异常。当然,请确保您使用的是最新版本的 PDFBox。