Itextsharp removes PDF encryption and itextsharppdf Encryption
When you operate a PDF file, you will encounter the problem that the PDF file is encrypted and cannot be operated. Search for the information from the network for the morning and click the following code, the encrypted PDF file can be converted into an unencrypted PDF file, which is pure code and does not require the use of PDF decryption software. Before use, you need to import the following reference. The itextsharp version used is 5.5.9.0.
1 using iTextSharp.text.pdf;2 using iTextSharp.text;3 using System.IO;
1 // <summary> 2 // the PDF file is removed. 3 // </summary> 4 // <param name = "sourceFullName"> the source file path (such: d: \ old.pdf) </param> 5 // <param name = "newFullName"> target file path (for example, D: \ new.pdf) </param> 6 private static void deletePDFEncrypt (string sourceFullName, string newFullName) 7 {8 if (string. isNullOrEmpty (sourceFullName) | string. isNullOrEmpty (newFullName) 9 {10 throw new Exception ("the source file path or target file path cannot be empty or null. "); 11} 12 // Console. writeLine ("read PDF document"); 13 try14 {15 // create a PdfReader object 16 PdfReader reader = new PdfReader (sourceFullName); 17 PdfReader. unethicalreading = true; 18 // get the document page 19 int n = reader. numberOfPages; 20 // obtain the size of the first page 21 Rectangle pagesize = reader. getPageSize (1); 22 float width = pagesize. width; 23 float height = pagesize. height; 24 // create a Document variable 25 document Document = new Document (pagesize, 50, 50, 50); 26 // create this document 27 author writer = author writer. getInstance (document, new FileStream (newFullName, FileMode. create); 28 // open document 29 document. open (); 30 // Add 31 bytes contentbyte cb = writer. directContent; 32 int I = 0; 33 int p = 0; 34 while (I <n) 35 {36 document. newPage (); 37 p ++; 38 I ++; 39 PdfImportedPage page1 = writer. getImportedPage (reader, I); 40 cb. addTemplate (page1, 1f, 0, 0, 1f, 0, 0); 41} 42 // close document 43 document. close (); 44} 45 catch (Exception ex) 46 {47 throw new Exception (ex. message); 48} 49}
PDF attributes before code conversion are used, for example:
After conversion: