问题
I'm using Itextsharp v5.1 and created enabled reader pdf files. I wrote a c# class that fill the form and keep each individual pdf file extended reader. But, when I use this MergeFiles function here, it create a new Merged file NOT extended reader and I need your help with this please to turn it extended reader...
my MergeFiles function is :
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
回答1:
Reader permissions can be enabled only by Adobe products. They are based on a custom digital signature that is invalidated when the file is modified. The certificate for this digital signature is owned by Adobe and it is not public, also the computation of this digital signature is not documented. You cannot re-enable in any way the reader permissions after you merged the files, unless you use Acrobat on the merged file.
回答2:
In order to preserve existing reader enabled permissions, you have to use a PdfStamper in APPEND mode.
There are also a number of things you Shouldn't Do to such a PDF or you'll disable the extra permissions anyway... I believe "add pages" and "add annotations/fields" are on the list of things you can't do.
来源:https://stackoverflow.com/questions/6508022/itextsharp-merging-pdf-files-disables-extended-reader-permissions