1

I've been able to write a code to create a new PDF or open an existing PDF and append. What I want to achieve now is to insert another PDF into it. Below is the sample code I've written so far to append PDF.

Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();


PdfReader reader = new PdfReader(templateInputStream);
PdfImportedPage page = writer.getImportedPage(reader, 1); 


document.newPage();
cb.addTemplate(page, 0, 0);

// Append sample line
document.add(new Paragraph("my timestamp")); 

document.close();

As per my knowledge Adobe reader does support inserting documents. Not sure if the same can be achieved using Java code too.

EDIT:

Below is the code I've written to embed PDF inside PDF. I'm getting error while I click and open embedded pdf/attachment.

public class AddEmbeddedFile {
    public static final String SRC = "C:\\Users\\User\\Desktop\\testSource.pdf";
    public static final String Embed = "C:\\Users\\User\\Desktop\\testEmbed.pdf";
    public static final String DEST = "C:\\Users\\User\\Desktop\\testDest.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddEmbeddedFile().manipulatePdf(SRC, DEST, Embed);
    }
    public void manipulatePdf(String src, String dest, String embed) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfReader reader2 = new PdfReader(embed);
        int n = reader2.getNumberOfPages();
        reader2.close();
        ByteArrayOutputStream boas;
        byte[] PDFContent = null;
        byte[] PDFContent2 = new byte[0];
        for (int i = 0; i < n; ) {
            reader2.selectPages(String.valueOf(++i));
            boas = new ByteArrayOutputStream();
            PdfStamper stamper2 = new PdfStamper(reader2, boas);
            PDFContent = boas.toByteArray();
            byte[] c = new byte[PDFContent.length + PDFContent2.length];
            System.arraycopy(PDFContent, 0, c, 0, PDFContent.length);
            System.arraycopy(PDFContent2, 0, c, PDFContent.length, PDFContent2.length);
            PDFContent2 = c;
        }
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
                stamper.getWriter(), null, "tests.pdf", PDFContent2, "pdf", null, 0);
        stamper.addFileAttachment("some test file", fs);
        stamper.close();
    }
}

This is the error I'm receiving: enter image description here

4
  • 1
    itextpdf.com/sandbox/annotations/AddEmbeddedFile
    – Stefan
    Commented Sep 23, 2015 at 10:59
  • @Stefan, Can you please confirm if it's OpenSource? Also it just inserts a text and text document, not a pdf document. Can you please help with that. Thanks. Commented Sep 23, 2015 at 19:07
  • 1
    iText is open source software released under the AGPL. This means that you have to release your own code as AGPL too. In many cases, this excludes using iText in a commercial context unless you purchase a commercial license from iText Software. This is explained in this one-minute video. In short: iText is open source, but this doesn't mean that you don't have to purchase a commercial license if you use iText in a commercial application. Commented Sep 23, 2015 at 19:38
  • @BrunoLowagie, Thanks for the explanation. Commented Sep 23, 2015 at 19:55

2 Answers 2

6

The code you're using in your question is wrong. If you imported PdfImportedPage objects into a PdfWriter, you lose all interactivity that may exist in the original document. You need to use PdfStamper instead.

There are two different ways to add an attachment to an existing PDF. One was already mentioned in the comments where you are referred to the AddEmbeddedFile example:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
            stamper.getWriter(), null, "test.txt", "Some test".getBytes());
    stamper.addFileAttachment("some test file", fs);
    stamper.close();
}

You have an existing PDF src in which you embed a text file. It should be fairly easy to adapt the example so that you add PDF bytes instead of plain text. In this case, the end user will only see the PDF if he opens the attachments panel.

Another way to attach a file is to use an attachment annotation:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
        stamper.getWriter(), null, "test.txt", "Some test".getBytes());
    Rectangle rect = new Rectangle(36, 770, 72, 806);
    PdfAnnotation attachment = dfAnnotation.createFileAttachment(
        stamper.getWriter(), rect, "My attachment", fs);
    stamper.addAnnotation(attachment, 1);
    stamper.close();
}

This will add a visible annotation on the first page of the existing PDF on the coordinates defined by rect.

It is also possible that you are using the concept "attaching a file" incorrectly. Maybe you mean to see concatenating one file to another. This is explained in my answer to the following question: How to merge documents correctly?

Update:

You are using this method to create a PdfFileSpecification instance:

fileEmbedded(
    PdfWriter writer,
    String filePath,
    String fileDisplay,
    byte[] fileStore,
    String mimeType,
    PdfDictionary fileParameter,
    int compressionLevel)

But your parameters are all wrong:

PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
    stamper.getWriter(), // correct
    null, // should be "C:\\Users\\User\\Desktop\\testEmbed.pdf"
    "tests.pdf", // correct
    PDFContent2, // this is completely wrong!!!
    "pdf", // should be "application/pdf"
    null, // OK
    0); // choosing 0 means that you don't want to compress the attachment. Why not?

The way you creat PdfContent2 is completely wrong. It is hard to imagine what you are trying to achieve here. You are concatenating PDF bytes in ways that are completely in violation with the PDF reference.

1
  • Thank you so much. I'm trying to insert PDF inside PDF. This is how I'm doing it, I'm reading the Content of the PDF I'm suppose to insert then use it in CreateFileAttachement. But I'm receiving error on opening the attachment. I've updated my question (under EDIT section) to reflect the updated code and Error Commented Sep 23, 2015 at 20:03
-2

Update:

PdfFileSpecification fileSpecification = PdfFileSpecification.fileEmbedded(pdfWriter,
                    pdfAttach.getAbsolutePath(), pdfAttach.getName(), null);
pdfWriter.addFileAttachment("Sample Attachment", fileSpecification);
1
  • What do you want to update? Why do you want to update it?
    – mkl
    Commented Feb 23, 2018 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.