Itextsharp Remove PDF Encryption

Source: Internet
Author: User



In the operation of the PDF file will encounter the PDF file encryption, can not operate the problem, looking for data from the network the morning, tinker out the following code, you can convert the encrypted PDF into an unencrypted PDF file, pure code, without the use of PDF decryption software, before using the following references need to be imported, 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 /// PDF encryption will be removed
 3 /// </ summary>
 4 /// <param name = "sourceFullName"> Source file path (eg: D: \ old.pdf) </ param>
 5 /// <param name = "newFullName"> destination file path (eg: 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 ("Source or destination file path cannot be empty or null.")
11}
12 //Console.WriteLine("Read PDF ");
13 try
14 {
15 // Create a PdfReader object
16 PdfReader reader = new PdfReader (sourceFullName);
17 PdfReader.unethicalreading = true;
18 // Get document pages
19 int n = reader.NumberOfPages;
20 // Get 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, 50);
26 // Create this document
27 PdfWriter writer = PdfWriter.GetInstance (document, new FileStream (newFullName, FileMode.Create));
28 // open document
29 document.Open ();
30 // add content
31 PdfContentByte 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 the document
43 document.Close ();
44}
45 catch (Exception ex)
46 {
47 throw new Exception (ex.Message);
48}
49} 


Before using code to convert the properties of the PDF, such as:






After conversion:






Itextsharp Remove PDF Encryption


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.