C# 利用itextsharp对PDF文件加密

    xiaoxiao2021-03-25  133

    最近在研究itextsharp对PDF文件的加密,整理如下,希望帮助到有需要的朋友

    部分代码如下:

    public static string src = "";//要加密的PDF文件 public static string dest="";//加密后生成的PDF文件 static void Main(string[] args) { if (string.IsNullOrEmpty(src) || string.IsNullOrEmpty(dest)) Console.WriteLine("源文件或目标文件不能为空"); PdfReader reader = new PdfReader(src); //读取要加密的PDF文件 int n = reader.NumberOfPages; //获取PDF文件的页数 Rectangle pagesize = reader.GetPageSize(1); Document document = new Document(pagesize); FileStream stream = new FileStream(dest, FileMode.Create); PdfCopy copy = new PdfCopy(document, stream); copy.SetEncryption(PdfWriter.STRENGTH128BITS, "123", null, PdfWriter.AllowCopy | PdfWriter.AllowPrinting); //加密必须放在文档打开之前 document.Open(); //写文件 for (int i = 1; i <= n; i++) { PdfImportedPage page = copy.GetImportedPage(reader, i); copy.AddPage(page); } document.Close(); Console.WriteLine("文档加密完成"); }说明:itextsharp版本为5.1.2.0

    转载请注明原文地址: https://ju.6miu.com/read-12195.html

    最新回复(0)