9
我创建使用iTextSharp的用于读取PDF,附加社会DRM内容,然后保存文件C#程序。如何锁定这个新的PDF进一步编辑?对编辑锁PDF使用iTextSharp的
我希望用户能够查看该文件,而无需输入密码,我不介意选择/复制操作,但我很介意去除DRM社会的能力。
我创建使用iTextSharp的用于读取PDF,附加社会DRM内容,然后保存文件C#程序。如何锁定这个新的PDF进一步编辑?对编辑锁PDF使用iTextSharp的
我希望用户能够查看该文件,而无需输入密码,我不介意选择/复制操作,但我很介意去除DRM社会的能力。
加密PDF文档。简单HTTP Handler
工作例子,让你开始:
<%@ WebHandler Language="C#" Class="lockPdf" %>
using System;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class lockPdf : IHttpHandler {
public void ProcessRequest (HttpContext context) {
HttpServerUtility Server = context.Server;
HttpResponse Response = context.Response;
Response.ContentType = "application/pdf";
using (Document document = new Document()) {
PdfWriter writer = PdfWriter.GetInstance(
document, Response.OutputStream
);
writer.SetEncryption(
// null user password => users can open document __without__ pasword
null,
// owner password => required to __modify__ document/permissions
System.Text.Encoding.UTF8.GetBytes("ownerPassword"),
/*
* bitwise or => see iText API for permission parameter:
* http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfWriter.html
*/
PdfWriter.ALLOW_PRINTING
| PdfWriter.ALLOW_COPY
,
// encryption level also in documentation referenced above
PdfWriter.ENCRYPTION_AES_128
);
document.Open();
document.Add(new Paragraph("hello world"));
}
}
public bool IsReusable { get { return false; } }
}
内注释应该是不言自明的。请参阅PdfWriter documentation。
您也可以使用使用PdfEncryptor class一个PdfReader
对象加密PDF文档。换句话说,你也可以做这样的事情(未经测试):
PdfReader reader = new PdfReader(INPUT_FILE);
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
// add your content
}
using (FileStream fs = new FileStream(
OUTPUT_FILE, FileMode.Create, FileAccess.ReadWrite))
{
PdfEncryptor.Encrypt(
new PdfReader(ms.ToArray()),
fs,
null,
System.Text.Encoding.UTF8.GetBytes("ownerPassword"),
PdfWriter.ALLOW_PRINTING
| PdfWriter.ALLOW_COPY,
true
);
}
}
这是完美的工作。 – CrispinH
谢谢!这样可行 –