iTextSharp的 - 合并PDF文件禁用扩展读者权限 [英] itextSharp - Merging pdf files disables extended reader permissions

查看:552
本文介绍了iTextSharp的 - 合并PDF文件禁用扩展读者权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iTextSharp的V5.1和创建启用Reader阅读PDF文件。
我写了一个C#类,填写表格,并保持每个单独的PDF文件阅读器扩展。
但是,当我在这里使用这个MergeFiles功能,创建一个新的合并文件没有延伸阅读器,我需要你的帮助,这请把它延伸阅读器...



我MergeFiles功能是:

 公共静态无效MergeFiles(字符串destinationFile,字符串[]来源档案)
{

{
// 1:目标文档创建的MemoryStream。使用
(MemoryStream的毫秒=新的MemoryStream())
{
// 2:创建PdfCopyFields对象。
PdfCopyFields复制=新PdfCopyFields(毫秒);
// - 设置目标文件的安全性和其他设置。
//copy.Writer.SetEncryption(PdfWriter.STRENGTH128BITS,空,1234,PdfWriter.AllowPrinting | PdfWriter.AllowCopy | PdfWriter.AllowFillIn);
copy.Writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;

// - 创建一个ArrayList来保存书签供以后使用。
ArrayList的概括=新的ArrayList();
INT pageOffset = 0;
INT F = 0;
// 3:导入在args规定[1],ARGS [2],等等...
文件,而(F< sourceFiles.Length)
{
/ /抓住从args []为文件和PdfReader打开它。
字符串文件=来源档案[F]
PdfReader读卡器=新PdfReader(文件,PdfEncodings.ConvertToBytes(1234,NULL));
// PdfStamper pdfStamper =新PdfStamper(pdfReader,新的F​​ileStream(NEWFILE,FileMode.Create),\0',真);

//导入从当前文件的页面。
copy.AddDocument(读卡器);

//创建书签的文件中一个ArrayList被导入。
// ArrayList的bookmarkLst = SimpleBookmark.GetBookmark(读卡器);
//移的页面accomidate这是当前文档前导入的页面。
// SimpleBookmark.ShiftPageNumbers(bookmarkLst,pageOffset,NULL);
//填写轮廓的ArrayList,每个书签作为哈希表。
//的foreach(在bookmarkLst的Hashtable HT)
// {
// outlines.Add(HT);
//}
//设置页面抵消进口的最后一页。
pageOffset + = reader.NumberOfPages;
˚F++;
}
// 4:将所有文件概括了一个新的根的轮廓和
//设置他们为目标文档
// copy.Writer.Outlines = GetBookmarks(根,((哈希表)大纲[0])[页],勾勒出);
// 5:关闭PdfCopyFields对象。
copy.Close();
// 6:保存的MemoryStream到文件中。
MemoryStreamToFile(MS,destinationFile);
}
}
赶上(System.Exception的E)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
到Console.ReadLine();
}
}
#endregion


#地区MemoryStreamToFile
///<总结>
///保存一个MemoryStream到指定的文件名
///< /总结>
///< PARAM NAME =MS>的MemoryStream保存< /参数>
///< PARAM NAME =文件名>文件名保存的MemoryStream为< /参数>
公共静态无效MemoryStreamToFile(MemoryStream的MS,字符串文件名)
{
使用(的FileStream FS =新的FileStream(@FileName,FileMode.Create))
{
字节[]数据= MS.ToArray();
fs.Write(数据,0,data.Length);
fs.Close();
}
}
#endregion


解决方案

为了保留现有的读者启用的权限,你必须使用追加方式的PdfStamper。



也有一些东西你不应该做这样的PDF或你反正禁用额外的权限......我相信添加页面和添加注解/场都是事情你不能做就行了。


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

解决方案

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.

这篇关于iTextSharp的 - 合并PDF文件禁用扩展读者权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆