【问题标题】:I am using iText for pdf create and encryption我正在使用 iText 进行 pdf 创建和加密
【发布时间】:2016-10-21 18:38:20
【问题描述】:
                    string inputfile = "input file path";
                    string outfile = "outfile file path";

                    using (Stream output = new FileStream(outfile, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        PdfReader reader = new PdfReader(inputfile);             
                        Dictionary<string, string> newInfo = new Dictionary<string, string>();
                        newInfo.Add("Title", "Title");
                        newInfo.Add("Subject", "Subject");
                        newInfo.Add("Keywords", "Keywords");
                        newInfo.Add("Creator", "Creator");
                        newInfo.Add("Author", "Author");
                        newInfo.Add("CustomInfo", "CustomeInformationCanStoreHere");                           
                        PdfEncryptor.Encrypt(reader,output,true,"*****","*****",PdfWriter.DO_NOT_ENCRYPT_METADATA, newInfo);
                    }

我已经使用上面的代码加密了 PDF 文件(密码和设置选项为 PdfWriter.DO_NOT_ENCRYPT_METADATA)

正如选项所建议的(DO_NOT_ENCRYPT_METADATA)我不想加密元数据,但它仍然加密元数据,如标题、主题、Auther、关键字信息..

上面的代码中是否缺少任何内容。

public void manipulatePdf(string source, string destination) {
        PdfReader reader = new PdfReader(source);
        Stream output = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None);
        PdfStamper stamper = new PdfStamper(reader, output);

        Dictionary<string, string> newInfo = new Dictionary<string, string>();
        newInfo.Add("Title", "Title");
        newInfo.Add("Subject", "Subject");
        newInfo.Add("Keywords", "Keywords");
        newInfo.Add("Creator", "Creator");
        newInfo.Add("Author", "Author");
        newInfo.Add("CustomInfo", "CustomeInformationCanStoreHere");

        stamper.MoreInfo = newInfo;             

        MemoryStream outStream = new MemoryStream();
        XmpWriter xmpw = new XmpWriter(outStream,newInfo);            
        stamper.XmpMetadata = outStream.ToArray();            
        byte[] password = Encoding.ASCII.GetBytes("password");            
        stamper.SetEncryption(password, password, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);

        xmpw.Close();
        stamper.Close();
        reader.Close();
    }

【问题讨论】:

    标签: c# itext


    【解决方案1】:

    您对DO_NOT_ENCRYPT_METADATA 的解释是错误的。您认为此设置不会加密信息字典。那不是该设置的目的。该设置与 XMP 流有关。 XMP 代表 XML 元数据平台,它是作为 XML 流存储在 PDF 中(或其他文件,如图像)中的元数据。当我查看您的代码时,我发现您没有创建 XMP 流。请参阅ChangeMetadata 示例。

    此外,您应该知道并非所有类型的加密都支持DO_NOT_ENCRYPT_METADATA。例如,40 位标准加密应该忽略该设置(但我看到您使用的是 128 位标准加密,所以应该没问题)。

    【讨论】:

    • Bruno 非常感谢您的输入我已经根据您的建议进行了代码更改并更新了问题,我的问题是我需要读取 XMP 数据
    • 现在我需要从 pdf 中读取 XMP 数据,但我不想为读取 XMP 数据提供密码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多