itextSharp - 合并pdf文件会禁用扩展的读者权限

时间:2011-06-28 14:24:33

标签: c# pdf itextsharp acrobat

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

我的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

2 个答案:

答案 0 :(得分:1)

只有Adobe产品才能启用阅读器权限。它们基于自定义数字签名,在修改文件时无效。此数字签名的证书由Adobe拥有,并且不公开,此数字签名的计算也未记录。合并文件后,您无法以任何方式重新启用阅读器权限,除非您在合并文件上使用Acrobat。

答案 1 :(得分:1)

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

对于这样的PDF还有许多你不应该做的事情,或者你无论如何都会禁用额外的权限......我相信“添加页面”和“添加注释/字段”都在列表中你做不到。

相关问题