iTextSharp - 如何打开/读取/提取文件附件?

发布于 2024-09-05 03:48:10 字数 393 浏览 0 评论 0原文

我有一些 PDF 文件,其中包含两个带有静态名称的附加文件。我想使用 iTextSharp 将这些文件提取到临时目录,以便我可以进一步使用它们。我尝试按照这里的教程进行操作,但是当我遇到问题时iTextSharp.text.pdf.PdfReader 没有 getCatalog() 方法,如底部示例所示。

关于如何提取附件有什么建议吗?简单地说,PDF 文档位于“C:\test.pdf”,两个附件存储为“attach1.xml”和“attach2.xml”。

I have some PDF's all with two attached files with static names. I would like to use iTextSharp to extract these files to a temp directory so that I can work with them further. I tried following the tutorial here but I ran into problems when the iTextSharp.text.pdf.PdfReader didn't have a getCatalog() method as shown in the bottom example.

Any advice on how I can extract the attachments? Let's just say for ease that the PDF document is at "C:\test.pdf" and the two attachments are stored as "attach1.xml" and "attach2.xml".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2024-09-12 03:48:42

我最终找到了一种方法来做到这一点——尽管不完全是通过编程方式。我包含了一个名为“pdftk.exe”的二进制文件,它是 PDF ToolKit,它具有用于提取附件的命令行选项。

为了澄清这一点,我添加了 pdftk.exe,然后通过 Process.Start("./pdftk", "contains_attachments.pdf unpack_files output \"C:\\output_directory\"") 调用它。请注意,pdftk 不会输出到尾部带有反斜杠的文件夹。您可以在这里找到 pdftk: http://www.accesspdf.com/pdftk/

添加后.exe 文件复制到项目中,您需要将其属性设置为“始终复制”或“如果较新则复制”。

I ended up finding a way to do this - although not exactly programmatically. I included a binary called "pdftk.exe" which is PDF ToolKit, which has command-line options to extract the attachments.

To clarify, I added pdftk.exe, then called it via Process.Start("./pdftk", "contains_attachments.pdf unpack_files output \"C:\\output_directory\""). Note that pdftk will not output to a folder with a trailing backslash. You can find pdftk here: http://www.accesspdf.com/pdftk/

After adding the .exe file to the project, you need to set its properties to "Copy always" or "Copy if newer".

爱要勇敢去追 2024-09-12 03:48:34

我找到了这个解决方案。我不知道这是否是最好的方法,但是,它有效!

protected void btnTransfer_Click(object sender, EventArgs e)
{
    PdfReader reader = new PdfReader(FileUpload1.FileContent);
    List<FileContent> lstAtt = GetAttachments(reader);
    reader.Close(); 
}

private class FileContent
{
    public string Name { get; set; }

    public byte[] Content { get; set; }
}

private List<FileContent> GetAttachments(PdfReader reader)
{
    #region Variables

    PdfDictionary catalog = null;
    PdfDictionary documentNames = null;
    PdfDictionary embeddedFiles = null;
    PdfDictionary fileArray = null;
    PdfDictionary file = null;

    PRStream stream = null;

    FileContent fContent = null;
    List<FileContent> lstAtt = null;

    #endregion

    // Obtengo el conjunto de Diccionarios del PDF.
    catalog = reader.Catalog;

    // Variable que contiene la lista de archivos adjuntos.
    lstAtt = new List<FileContent>();

    // Obtengo documento
    documentNames = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.NAMES));

    if (documentNames != null)
    {
        // Obtengo diccionario de objetos embebidos
        embeddedFiles = (PdfDictionary)PdfReader.GetPdfObject(documentNames.Get(PdfName.EMBEDDEDFILES));
        if (embeddedFiles != null)
        {
            // Obtengo lista de documentos del Diccionario de objetos embebidos
            PdfArray filespecs = embeddedFiles.GetAsArray(PdfName.NAMES);

            // Cada archivo posee 2 posiciones en el array
            for (int i = 0; i < filespecs.Size; i++)
            {
                // Como posee 2 posiciones por archivo, hago "i++"
                i++;
                fileArray = filespecs.GetAsDict(i);

                // Obtengo diccionario del adjunto
                file = fileArray.GetAsDict(PdfName.EF);

                foreach (PdfName key in file.Keys)
                {
                    stream = (PRStream)PdfReader.GetPdfObject(file.GetAsIndirectObject(key));

                    fContent = new FileContent();
                    // Nombre del Archivo.
                    fContent.Name = fileArray.GetAsString(key).ToString();

                    // Array de bytes del Contenido del Archivo.
                    fContent.Content = PdfReader.GetStreamBytes(stream);
                    lstAtt.Add(fContent);
                }
            }
        }
    }

    // Y al fin, devuelvo una lista de adjuntos del PDF - podrían haberlo echo un poco mas facil :@
    return lstAtt;
}

I found this solution. I don't know if it's the best way, but, it work!!

protected void btnTransfer_Click(object sender, EventArgs e)
{
    PdfReader reader = new PdfReader(FileUpload1.FileContent);
    List<FileContent> lstAtt = GetAttachments(reader);
    reader.Close(); 
}

private class FileContent
{
    public string Name { get; set; }

    public byte[] Content { get; set; }
}

private List<FileContent> GetAttachments(PdfReader reader)
{
    #region Variables

    PdfDictionary catalog = null;
    PdfDictionary documentNames = null;
    PdfDictionary embeddedFiles = null;
    PdfDictionary fileArray = null;
    PdfDictionary file = null;

    PRStream stream = null;

    FileContent fContent = null;
    List<FileContent> lstAtt = null;

    #endregion

    // Obtengo el conjunto de Diccionarios del PDF.
    catalog = reader.Catalog;

    // Variable que contiene la lista de archivos adjuntos.
    lstAtt = new List<FileContent>();

    // Obtengo documento
    documentNames = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.NAMES));

    if (documentNames != null)
    {
        // Obtengo diccionario de objetos embebidos
        embeddedFiles = (PdfDictionary)PdfReader.GetPdfObject(documentNames.Get(PdfName.EMBEDDEDFILES));
        if (embeddedFiles != null)
        {
            // Obtengo lista de documentos del Diccionario de objetos embebidos
            PdfArray filespecs = embeddedFiles.GetAsArray(PdfName.NAMES);

            // Cada archivo posee 2 posiciones en el array
            for (int i = 0; i < filespecs.Size; i++)
            {
                // Como posee 2 posiciones por archivo, hago "i++"
                i++;
                fileArray = filespecs.GetAsDict(i);

                // Obtengo diccionario del adjunto
                file = fileArray.GetAsDict(PdfName.EF);

                foreach (PdfName key in file.Keys)
                {
                    stream = (PRStream)PdfReader.GetPdfObject(file.GetAsIndirectObject(key));

                    fContent = new FileContent();
                    // Nombre del Archivo.
                    fContent.Name = fileArray.GetAsString(key).ToString();

                    // Array de bytes del Contenido del Archivo.
                    fContent.Content = PdfReader.GetStreamBytes(stream);
                    lstAtt.Add(fContent);
                }
            }
        }
    }

    // Y al fin, devuelvo una lista de adjuntos del PDF - podrían haberlo echo un poco mas facil :@
    return lstAtt;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文