Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:  
8:   namespace iTextSharp.tutorial.Chap01
9:   {
10:       /// <summary>
11:       /// Encrypt
12:       /// </summary>
13:       public class Encrypt
14:       {
15:           public Encrypt(String[] args)
16:           {
17:               if (args.Length != 3) 
18:               {
19:                   Console.Error.WriteLine("This tools needs 3 parameters:\njava Encrypt srcfile destfile password");
20:               }
21:               else 
22:               {
23:                   try 
24:                   {
25:                       // we create a reader for a certain document
26:                       PdfReader reader = new PdfReader(args[0]);
27:                       // we retrieve the total number of pages
28:                       int n = reader.NumberOfPages;
29:                       Console.WriteLine("There are " + n + " pages in the original file.");
30:                   
31:                       // step 1: creation of a document-object
32:                       Document document = new Document(reader.GetPageSizeWithRotation(1));
33:                       // step 2: we create a writer that listens to the document
34:                       PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(args[1], FileMode.Create));
35:                       writer.SetEncryption(PdfWriter.STRENGTH128BITS, args[2], null, PdfWriter.AllowPrinting);
36:                       // step 3: we open the document
37:                       document.Open();
38:                       PdfContentByte cb = writer.DirectContent;
39:                       PdfImportedPage page;
40:                       int rotation;
41:                       int i = 0;
42:                       // step 4: we add content
43:                       while (i < n) 
44:                       {
45:                           i++;
46:                           document.SetPageSize(reader.GetPageSizeWithRotation(i));
47:                           document.NewPage();
48:                           page = writer.GetImportedPage(reader, i);
49:                           rotation = reader.GetPageRotation(i);
50:                           if (rotation == 90 || rotation == 270) 
51:                           {
52:                               cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
53:                           }
54:                           else 
55:                           {
56:                               cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
57:                           }
58:                           Console.WriteLine("Processed page " + i);
59:                       }
60:                       // step 5: we close the document
61:                       document.Close();
62:                   }
63:                   catch(Exception e) 
64:                   {
65:                       Console.Error.WriteLine(e.Message);
66:                       Console.Error.WriteLine(e.StackTrace);
67:                   }
68:               }
69:  
70:           }
71:       }
72:   }