4

I'm Using PdfBox for android in order to append data to a PDF file.

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)
royB
  • 12,779
  • 15
  • 58
  • 80
  • 2
    Please correct the first method to the actual code. prerparePdfToAppend() does not return anything. It should return output.toByteArray(). The three last lines are not needed. You could even shorten this more by returning your PDDocument and appending that one. – Tilman Hausherr Jan 19 '16 at 09:46

1 Answers1

5

The last lines of the prerparePdfToAppend method look weird to me. But why make your life complicated? Return a 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.

royB
  • 12,779
  • 15
  • 58
  • 80
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97