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

标签 c# pdf pdf-generation itext

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

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

最佳答案

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

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

关于c# - iTextSharp - 如何打开/读取/提取文件附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3007780/

相关文章:

coldfusion - CFDocumentItem - 更改字体大小

c# - 如何使用 JavaScript 创建带有字典选项的可编辑下拉列表?

c# - 无法使用 Microsoft.Office.Interop.Excel 加载文件或程序集“office,版本 = 15.0.0.0”

c# - CaSTLe Windsor Typed Factory 无释放不泄漏

ruby - 在 Mac 上将 html 文件转换为 pdf

c# - 我有什么选择可以从 .NET 中的代码生成 PDF 报告以获取科学数据 (winforms)

java - 使用 Spring Boot 在单个 PDF 中渲染表格和图表

c# - 字符串到字符串的精度

html - 在 div 中打开 pdf

node.js - 在 JavaScript 中将八位字节流数据保存为 PDF 文件