我希望用户能够在不输入密码的情况下查看该文件,而我不介意选择/复制 *** 作,但我记住删除社交DRM的能力.
解决方法 加密您的pdf文档.简单的http处理程序工作示例让您开始:<%@ 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 ); } }总结
以上是内存溢出为你收集整理的c# – 使用iTextSharp锁定PDF以防编辑全部内容,希望文章能够帮你解决c# – 使用iTextSharp锁定PDF以防编辑所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)