0

我正在处理一个 PDF 文件,这是一个安全的文件,并且在 PDF 文件中附加了一个 Excel。

以下是我尝试过的代码。

    static void Main(string[] args)
    {
        Program pgm = new Program();
        pgm.EmbedAttachments();
        //pgm.ExtractAttachments(pgm.pdfFile);
    }

    private void ExtractAttachments(string _pdfFile)
    {
        try
        {
            if (!Directory.Exists(attExtPath))
                Directory.CreateDirectory(attExtPath);

            byte[] password = System.Text.ASCIIEncoding.ASCII.GetBytes("TFAER13052016");
            //byte[] password = System.Text.ASCIIEncoding.ASCII.GetBytes("Password");


            PdfDictionary documentNames = null;
            PdfDictionary embeddedFiles = null;
            PdfDictionary fileArray = null;
            PdfDictionary file = null;
            PRStream stream = null;

            //PdfReader reader = new PdfReader(_pdfFile);

            PdfReader reader = new PdfReader(_pdfFile, password);

            PdfDictionary catalog = reader.Catalog;

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

            if (documentNames != null)
            {
                embeddedFiles = (PdfDictionary)PdfReader.GetPdfObject(documentNames.Get(PdfName.EMBEDDEDFILES));
                if (embeddedFiles != null)
                {
                    PdfArray filespecs = embeddedFiles.GetAsArray(PdfName.NAMES);

                    for (int i = 0; i < filespecs.Size; i++)
                    {
                        i++;
                        fileArray = filespecs.GetAsDict(i);
                        file = fileArray.GetAsDict(PdfName.EF);

                        foreach (PdfName key in file.Keys)
                        {
                            stream = (PRStream)PdfReader.GetPdfObject(file.GetAsIndirectObject(key));
                            string attachedFileName = fileArray.GetAsString(key).ToString();
                            byte[] attachedFileBytes = PdfReader.GetStreamBytes(stream);

                            System.IO.File.WriteAllBytes(attExtPath + attachedFileName, attachedFileBytes);
                        }

                    }
                }
                else
                    throw new Exception("Unable to Read the attachment or There may be no Attachment");
            }
            else
            {
                throw new Exception("Unable to Read the document");
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.ReadKey();
        }
    }

    private void EmbedAttachments()
    {
        try
        {

            if (File.Exists(pdfFile))
                File.Delete(pdfFile);

            Document PDFD = new Document(PageSize.LETTER);



            PdfWriter writer;
            writer = PdfWriter.GetInstance(PDFD, new FileStream(pdfFile, FileMode.Create));

            PDFD.Open();
            PDFD.NewPage();
            PDFD.Add(new Paragraph("This is test"));

            PdfFileSpecification pfs = PdfFileSpecification.FileEmbedded(writer, @"C:\PDFReader\1.xls", "11.xls", null);

            //PdfFileSpecification pfs = PdfFileSpecification.FileEmbedded(writer, attFile, "11", File.ReadAllBytes(attFile), true);
            writer.AddFileAttachment(pfs);
            //writer.AddAnnotation(PdfAnnotation.CreateFileAttachment(writer, new iTextSharp.text.Rectangle(100, 100, 100, 100), "File Attachment", PdfFileSpecification.FileExtern(writer, "C:\\test.xml")));

            //writer.Close();
            PDFD.Close();

            Program pgm=new Program();

            using (Stream input = new FileStream(pgm.pdfFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (Stream output = new FileStream(pgm.epdfFile, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    PdfReader reader = new PdfReader(input);
                    PdfEncryptor.Encrypt(reader, output, true, "Password", "secret", PdfWriter.ALLOW_SCREENREADERS);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.StackTrace.ToString());
            Console.ReadKey();
        }
    }
}

上面的代码包含创建一个带有 excel 附件的加密 PDF 并提取它。

现在真正的问题是我已经拥有作为需求文档的文件(我无法共享文件),它也有一个像我的示例一样的 excel 附件。

但是上面的代码适用于我创建的安全 PDF,但不适用于实际的安全 PDF。

在调试时,我发现问题出在以下代码上

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

其中,

catalog.Get(PdfName.NAMES)

返回为 NULL,而我创建的 File 提供了预期的输出。

请在上面指导我。

TIA。

4

1 回答 1

0

正如 mkl 建议的那样,它已作为带注释的附件附加。但是示例中使用的参考提供了不再支持ZipFile方法。因此,我在下面找到了一个替代代码。

public void ExtractAttachments(byte[] src)
    {
        PRStream stream = null;
        string attExtPath = @"C:\PDFReader\Extract\";

        if (!Directory.Exists(attExtPath))
            Directory.CreateDirectory(attExtPath);

        byte[] password = System.Text.ASCIIEncoding.ASCII.GetBytes("TFAER13052016");
        PdfReader reader = new PdfReader(src, password);
        for (int i = 1; i <= reader.NumberOfPages; i++)
        {
            PdfArray array = reader.GetPageN(i).GetAsArray(PdfName.ANNOTS);
            if (array == null) continue;
            for (int j = 0; j < array.Size; j++)
            {
                PdfDictionary annot = array.GetAsDict(j);
                if (PdfName.FILEATTACHMENT.Equals(
                    annot.GetAsName(PdfName.SUBTYPE)))
                {
                    PdfDictionary fs = annot.GetAsDict(PdfName.FS);
                    PdfDictionary refs = fs.GetAsDict(PdfName.EF);
                    foreach (PdfName name in refs.Keys)
                    {
                        //zip.AddEntry(
                        //  fs.GetAsString(name).ToString(),
                        //  PdfReader.GetStreamBytes((PRStream)refs.GetAsStream(name))
                        //);
                        stream = (PRStream)PdfReader.GetPdfObject(refs.GetAsIndirectObject(name));
                        string attachedFileName = fs.GetAsString(name).ToString();
                        var splitname = attachedFileName.Split('\\');
                        if (splitname.Length != 1)
                            attachedFileName = splitname[splitname.Length - 1].ToString();
                        byte[] attachedFileBytes = PdfReader.GetStreamBytes(stream);

                        System.IO.File.WriteAllBytes(attExtPath + attachedFileName, attachedFileBytes);
                    }
                }
            }
        }
    }

请让我知道是否可以通过任何其他方式实现。

谢谢!!!

于 2016-07-26T09:20:09.173 回答