如何使用itext删除PDF的附件 [英] How to delete attachment of PDF using itext

查看:158
本文介绍了如何使用itext删除PDF的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pdf的新手,我使用以下代码将文件嵌入到pdf中。但是,我想编写另一个程序来删除嵌入文件。我可以知道怎么办吗?非常感谢!

  public void addAttachments(String src,String dest,String [] attachments)抛出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)抛出IOException {
PdfFileSpecification fs =
PdfFileSpecification.fileEmbedded(writer,src.getAbsolutePath(),src .getName(),null);
writer.addFileAttachment(src.getName()。substring(0,src.getName()。indexOf('。')),fs);
}


解决方案

让我先重写你的用于添加嵌入文件的代码。

  public void manipulatePdf(String src,String dest)抛出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();
}

您可以在此处找到完整的代码示例:示例的代码,看看我们如何在面向对象的情况下进行导航PDF格式的文件格式:

  public void manipulatePdf(String src,String dest)抛出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();
}

如您所见,我们从文档的根目录开始(又名目录)我们通过名称 EmbeddedFiles 走到名称数组。我知道我要删除的嵌入文件是数组中的第一个,我通过删除索引为0的元素两次删除名称和值。这首先删除描述,然后删除对文件的引用。附件现已消失:





由于我的示例中只有一个嵌入文件,所以当我查看PDF时,我现在看到一个空数组:





如果要一次删除所有嵌入文件,代码就更容易了。这显示在 RemoveEmbeddedFiles 示例中:

  public void manipulatePdf(String src,String dest)抛出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();
}

现在我们甚至不查看<$ c $的条目c> EmbeddedFiles 字典。 名称词典中不再有这样的条目:




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);
                                }

解决方案

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":

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

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:

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

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:

这篇关于如何使用itext删除PDF的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆