1

I am new to pdf and i use the following code to embed the file to pdf. However, I want to write another program to delete the embeded files. May I know how can I do it? Really Thanks!

public void addAttachments(String src, String dest, String[] attachments) throws IOException,DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
for (int i = 0; i < attachments.length; i++) {
        addAttachment(stamper.getWriter(), new File(attachments[i]));
                                  }
                                  stamper.close();
                                }

                                protected void addAttachment(PdfWriter writer, File src) throws IOException {
                                  PdfFileSpecification fs =
                                    PdfFileSpecification.fileEmbedded(writer, src.getAbsolutePath(), src.getName(), null);
                                  writer.addFileAttachment(src.getName().substring(0, src.getName().indexOf('.')), fs);
                                }
brian
  • 37
  • 1
  • 5

1 Answers1

1

Let me start by rewriting your code to add an embedded file.

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 can find the full code sample here: AddEmbeddedFile

Now when we look at the Attachments panel of the resulting PDF, we see an attachment test.txt with description "some test file":

enter image description here

After you have added this file, you now want to remove it. To do this, please use RUPS and take a look inside:

enter image description here

This gives us a hint on where to find the embedded file. Take a look at the code of the RemoveEmbeddedFile example to see how we navigate through the object-oriented file format that PDF is:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfDictionary root = reader.getCatalog();
    PdfDictionary names = root.getAsDict(PdfName.NAMES);
    PdfDictionary embeddedFiles = names.getAsDict(PdfName.EMBEDDEDFILES);
    PdfArray namesArray = embeddedFiles.getAsArray(PdfName.NAMES);
    namesArray.remove(0);
    namesArray.remove(0);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
}

As you can see, we start at the root of the document (aka the catalog) and we walk via Names and EmbeddedFiles to the Names array. As I know that the embedded file I want to remove is the first in the array, I remove the name and value by removing the element with index 0 twice. This first removes the description, then the reference to the file. The attachment is now gone:

enter image description here

As there was only one embedded file in my example, I now see an empty array when I look inside the PDF:

enter image description here

If you want to remove all the embedded files at once, the code is even easier. That is shown in the RemoveEmbeddedFiles example:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfDictionary root = reader.getCatalog();
    PdfDictionary names = root.getAsDict(PdfName.NAMES);
    names.remove(PdfName.EMBEDDEDFILES);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
}

Now we don't even look at the entries of the EmbeddedFiles dictionary. There is no longer such an entry in the Names dictionary:

enter image description here

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • is itext free? kindly let me know? – Naveen Kulkarni Oct 30 '14 at 11:22
  • 3
    It is free software, but that doesn't mean it is gratis. You can use iText without paying a fee if you respect the AGPL. This means that you are obliged to distribute your source code for free under the same AGPL license. As soon as you use iText in a project, product or service that is not free, you should buy a commercial license. See http://itextpdf.com/salesfaq – Bruno Lowagie Oct 30 '14 at 11:25