How to delete attachment of PDF using itext

前端 未结 1 1741
孤街浪徒
孤街浪徒 2021-01-07 09:29

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? Real

相关标签:
1条回答
  • 2021-01-07 10:05

    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

    0 讨论(0)
提交回复
热议问题