Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:  
8:   namespace iTextSharp.tutorial.Chap13
9:   {
10:       /// <summary>
11:       /// Chap13_pdfreader ժҪ˵
12:       /// </summary>
13:       public class Chap13_pdfreader
14:       {
15:           public Chap13_pdfreader()
16:           {
17:           Console.WriteLine("Chapter 13 example pdfreader: reading an existing PDF file");
18:           try {
19:               // we create a reader for a certain document
20:               PdfReader reader new PdfReader("Chap0702.pdf");
21:               // we retrieve the total number of pages
22:               int reader.NumberOfPages;
23:               // we retrieve the size of the first page
24:               Rectangle psize reader.GetPageSize(1);
25:               float width psize.Width;
26:               float height psize.Height;
27:               
28:               // step 1: creation of a document-object
29:               Document.Compress true;
30:               Document document new Document(psize50505050);
31:               // step 2: we create a writer that listens to the document
32:               PdfWriter writer PdfWriter.GetInstance(documentnew FileStream("Chap13_pdfreader.pdf"FileMode.Create));
33:               // step 3: we open the document
34:               try {
35:                   Watermark watermark new Watermark(Image.GetInstance("watermark.jpg"), 200320);
36:                   document.Add(watermark);
37:               }
38:               catch(Exception e) {
39:                   Console.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?");
40:               }
41:               document.Open();
42:               // step 4: we add content
43:               PdfContentByte cb writer.DirectContent;
44:               int 0;
45:               int 0;
46:               Console.WriteLine("There are " " pages in the document.");
47:               while (n) {
48:                   document.NewPage();
49:                   p++;
50:                   i++;
51:                   PdfImportedPage page1 writer.GetImportedPage(readeri);
52:                   cb.AddTemplate(page1.5f00.5f0height 2);
53:                  Console.WriteLine("processed page " i);
54:                   if (n) {
55:                       i++;
56:                       PdfImportedPage page2 writer.GetImportedPage(readeri);
57:                       cb.AddTemplate(page2.5f00.5fwidth 2height 2);
58:                      Console.WriteLine("processed page " i);
59:                   }
60:                   if (n) {
61:                       i++;
62:                       PdfImportedPage page3 writer.GetImportedPage(readeri);
63:                       cb.AddTemplate(page3.5f00.5f00);
64:                      Console.WriteLine("processed page " i);
65:                   }
66:                   if (n) {
67:                       i++;
68:                       PdfImportedPage page4 writer.GetImportedPage(readeri);
69:                       cb.AddTemplate(page4.5f00.5fwidth 20);
70:                      Console.WriteLine("processed page " i);
71:                   }
72:                   cb.SetRGBColorStroke(25500);
73:                   cb.MoveTo(0height 2);
74:                   cb.LineTo(widthheight 2);
75:                   cb.Stroke();
76:                   cb.MoveTo(width 2height);
77:                   cb.LineTo(width 20);
78:                   cb.Stroke();
79:                   BaseFont bf BaseFont.CreateFont(BaseFont.HELVETICABaseFont.CP1252BaseFont.NOT_EMBEDDED);
80:                   cb.BeginText();
81:                   cb.SetFontAndSize(bf14);
82:                   cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER"page " " of " + ((4) + (00)), width 2400);
83:                   cb.EndText();
84:               }
85:               // step 5: we close the document
86:               document.Close();
87:           }
88:           catch (Exception de) {
89:               Console.WriteLine(de.StackTrace);
90:           }
91:           }
92:       }
93:   }