【问题标题】:Extract Embedded files from pdf using pdfbox in .NET application在 .NET 应用程序中使用 pdfbox 从 pdf 中提取嵌入文件
【发布时间】:2013-06-10 08:43:50
【问题描述】:

我正在开发 .NET 应用程序,使用 pdfbox 从 PDF 中提取元数据、内容和附件。我能够提取元数据和内容,但在提取附件/嵌入文件时卡住了。

我有一个带有嵌入/附加文档文件的 pdf,并且想要检索该文件。 我已经完成了 java 示例 - http://svn.apache.org/repos/asf/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java 。但是在尝试在.Net中使用它时,我在下面的代码sn-p中得到“非泛型类型'java.util.Map'不能与类型参数一起使用”

java.util.Map<String, COSObjectable> names = efTree.getNames();

所以,如果有人帮我从 pdf 中提取文件,我将不胜感激。

提前致谢。

【问题讨论】:

    标签: .net pdfbox


    【解决方案1】:
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import com.itextpdf.text.pdf.PRStream;
    import com.itextpdf.text.pdf.PdfArray;
    import com.itextpdf.text.pdf.PdfDictionary;
    import com.itextpdf.text.pdf.PdfName;
    import com.itextpdf.text.pdf.PdfReader;
    
    public class PDFAttachments {
    
        public PDFAttachments() {
    
        }
    
        public void extractAttachments(String src, String dest) throws IOException {
            PdfReader reader = new PdfReader(src);
            PdfArray array;
            PdfDictionary annot;
            PdfDictionary fs;
            PdfDictionary refs;
            String fName;
            try {
                for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                    array = reader.getPageN(i).getAsArray(PdfName.ANNOTS);
                    if (array == null)
                        continue;
                    for (int j = 0; j < array.size(); j++) {
                        annot = array.getAsDict(j);
                        if (PdfName.FILEATTACHMENT.equals(annot
                                .getAsName(PdfName.SUBTYPE))) {
                            fs = annot.getAsDict(PdfName.FS);
                            refs = fs.getAsDict(PdfName.EF);
                            for (PdfName name : refs.getKeys()) {
                                fName = dest + fs.getAsString(name).toString();
                                /*
                                 * FileOutputStream fos = new
                                 * FileOutputStream(String.format(dest,
                                 * fs.getAsString(name).toString()));
                                 */
                                FileOutputStream fos = new FileOutputStream(fName);
                                fos.write(PdfReader.getStreamBytes((PRStream) refs
                                        .getAsStream(name)));
                                fos.flush();
                                fos.close();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                System.err.println("exception " + e.getMessage());
            }
        }
    
    }
    }
    

    【讨论】:

      【解决方案2】:

      我通过省略泛型并尝试这样的方法解决了这个问题:

      java.util.Map 名称 = efTree.getNames();

      现在我可以提取位于附件选项卡中的附件,但是 无法提取附加文件位于页面中。在这种情况下,我得到了 null efTree。

      PDDocumentNameDictionary namesDictionary = new PDDocumentNameDictionary(pdfDoc.getDocumentCatalog());
      PDEmbeddedFilesNameTreeNode efTree=namesDictionary.getEmbeddedFiles();
      

      因此,如果有人知道如何提取附加/嵌入在页面中的文件,可以帮助我在 .NET 应用程序中进行操作。

      【讨论】:

        猜你喜欢
        • 2012-12-30
        • 2015-11-19
        • 2014-07-11
        • 2012-02-01
        • 1970-01-01
        • 2018-01-15
        • 2021-11-23
        • 1970-01-01
        相关资源
        最近更新 更多