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

我正在使用Itextsharp v5.1并创建了启用的阅读器pdf文件。 我写了一个填充表格的ac#class,并保留每个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 ///  /// Saves a MemoryStream to the specified file name ///  /// MemoryStream to save /// File name to save MemoryStream as 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 

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

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

对于这样的PDF还有一些你不应该做的事情,或者你会禁用额外的权限……我相信“添加页面”和“添加注释/字段”都列在你可以做的事情上做。