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 reader.NumberOfPages;
29:                       Console.WriteLine("There are " " 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(documentnew FileStream(args[1], FileMode.Create));
35:                       writer.SetEncryption(PdfWriter.STRENGTH128BITSargs[2], nullPdfWriter.AllowPrinting);
36:                       // step 3: we open the document
37:                       document.Open();
38:                       PdfContentByte cb writer.DirectContent;
39:                       PdfImportedPage page;
40:                       int rotation;
41:                       int 0;
42:                       // step 4: we add content
43:                       while (n
44:                       {
45:                           i++;
46:                           document.SetPageSize(reader.GetPageSizeWithRotation(i));
47:                           document.NewPage();
48:                           page writer.GetImportedPage(readeri);
49:                           rotation reader.GetPageRotation(i);
50:                           if (rotation == 90 || rotation == 270
51:                           {
52:                               cb.AddTemplate(page0, -1f1f00reader.GetPageSizeWithRotation(i).Height);
53:                           }
54:                           else 
55:                           {
56:                               cb.AddTemplate(page1f001f00);
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:   }