【问题标题】:itextSharp - Merging pdf files disables extended reader permissionsitextSharp - 合并 pdf 文件会禁用扩展阅读器权限
【发布时间】:2011-09-24 09:08:40
【问题描述】:

我正在使用 Itextsharp v5.1 并创建了启用的阅读器 pdf 文件。 我编写了一个 c# 类来填写表单并保留每个单独的 pdf 文件扩展阅读器。 但是,当我在这里使用这个 MergeFiles 函数时,它会创建一个新的合并文件而不是扩展阅读器,我需要你的帮助来把它变成扩展阅读器......

我的 MergeFiles 函数是:

public static void MergeFiles(string destinationFile, string[] sourceFiles)
{
    try
    {
        //1:  Create the MemoryStream for the destination document.
        using (MemoryStream ms = new MemoryStream())
        {
            //2:  Create the PdfCopyFields object.
            PdfCopyFields copy = new PdfCopyFields(ms);
            // - Set the security and other settings for the destination file.
            //copy.Writer.SetEncryption(PdfWriter.STRENGTH128BITS, null, "1234", PdfWriter.AllowPrinting | PdfWriter.AllowCopy | PdfWriter.AllowFillIn);
            copy.Writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;

            // - Create an arraylist to hold bookmarks for later use.
            ArrayList outlines = new ArrayList();
            int pageOffset = 0;
            int f = 0;
            //3:  Import the documents specified in args[1], args[2], etc...
            while (f < sourceFiles.Length)
            {
                //  Grab the file from args[] and open it with PdfReader.
                string file = sourceFiles[f];
                PdfReader reader = new PdfReader(file, PdfEncodings.ConvertToBytes("1234", null));
               // PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create), '\0', true);

                //  Import the pages from the current file.
                copy.AddDocument(reader);

                //  Create an ArrayList of bookmarks in the file being imported.
                //      ArrayList bookmarkLst = SimpleBookmark.GetBookmark(reader);
                //  Shift the pages to accomidate any pages that were imported before the current document.
                //     SimpleBookmark.ShiftPageNumbers(bookmarkLst, pageOffset, null);
                //  Fill the outlines ArrayList with each bookmark as a HashTable.
                //      foreach (Hashtable ht in bookmarkLst)
                //      {
                //         outlines.Add(ht);
                //     }
                //  Set the page offset to the last page imported.
                pageOffset += reader.NumberOfPages;
                f++;
            }
            //4:  Put the outlines from all documents under a new "Root" outline and 
            //    set them for destination document 
            //   copy.Writer.Outlines = GetBookmarks("Root", ((Hashtable)outlines[0])["Page"], outlines);
            //5:  Close the PdfCopyFields object.
                copy.Close();
            //6:  Save the MemoryStream to a file.
                MemoryStreamToFile(ms, destinationFile);
        }
    }
    catch (System.Exception e)
    {
        Console.Error.WriteLine(e.Message);
        Console.Error.WriteLine(e.StackTrace);
        Console.ReadLine();
    }
}
#endregion


#region MemoryStreamToFile
/// <summary>
/// Saves a MemoryStream to the specified file name
/// </summary>
/// <param name="MS">MemoryStream to save</param>
/// <param name="FileName">File name to save MemoryStream as</param>
public static void MemoryStreamToFile(MemoryStream MS, string FileName)
{
    using (FileStream fs = new FileStream(@FileName, FileMode.Create))
    {
        byte[] data = MS.ToArray();
        fs.Write(data, 0, data.Length);
        fs.Close();
    }
}
#endregion

【问题讨论】:

    标签: c# pdf itextsharp acrobat


    【解决方案1】:

    读者权限只能由 Adob​​e 产品启用。它们基于在文件被修改时失效的自定义数字签名。此数字签名的证书归 Adob​​e 所有,不公开,此数字签名的计算也未记录在案。合并文件后,您无法以任何方式重新启用阅读权限,除非您对合并的文件使用 Acrobat。

    【讨论】:

    • 我已经通过 Adob​​e 启用了每个单独的文档。问题是通过 itextsharp 将几个 PDF 文件合并为一个并保持阅读器启用
    • @Ron:由于上述原因,无法合并启用阅读器的表单并保留启用阅读器的属性。
    • @Ron,重要的是要了解扩展读者权限 (ERP) 是基于文档的,而不是基于页面的。尽管您确实将每个单独的源 PDF 设置为具有 ERP,但您也在创建一个全新的 PDF 以将这些文档添加到其中,并且由于@iPDFdev 所说的原因,该文档没有 ERP。由于各个页面本身没有 ERP 的概念,因此将它们添加到新文档不会带来该(不存在的)属性。您可以尝试获取其中一个 PDF 并将其他 PDF 添加到其中,但由于 @Mark Storer 所说的原因,它也可能会失败。
    • @Chris,iPDFdev,感谢您的回复。我可以使用什么其他方法来解决它,或者使用 3rd 方应用程序。我还读到,如果 PdfCopyFields 类具有附加模式,它将起作用??
    • @Ron:唯一的解决方法是使用 Adob​​e Acrobat 在合并文件上启用 ERP。
    【解决方案2】:

    为了保留现有的阅读器启用权限,您必须在 APPEND 模式下使用 PdfStamper。

    还有一些你不应该对这样的 PDF 做的事情,否则你会禁用额外的权限......我相信“添加页面”和“添加注释/字段”在事情清单上你做不到。

    【讨论】:

    • 马克,我已经使用扩展阅读器 PDF 表单。我确实使用了 append = true 并且工作正常。但是当我尝试将这些 PDF 扩展阅读器表单中的一些合并到 1 个 PDF 文件时,它会创建一个没有扩展阅读器权限的新文件
    • 那么阅读器启用输出的唯一方法是使用 Acrobat(或他们的服务器东西:$$$)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 2011-04-24
    • 2018-10-08
    相关资源
    最近更新 更多