【发布时间】: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();
}
【问题讨论】: