我正在使用 iText 进行 pdf 创建和加密

I am using iText for pdf create and encryption

                    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);
                    }

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

如选项所示(DO_NOT_ENCRYPT_METADATA)我不想加密元数据,但它仍然加密标题、主题、作者、关键字信息等元数据。

上面的代码有什么遗漏吗

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();
    }

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

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