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 n = 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(psize, 50, 50, 50, 50); | |
31: | // step 2: we create a writer that listens to the document | |
32: | PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap13_pdfreader.pdf", FileMode.Create)); | |
33: | // step 3: we open the document | |
34: | try { | |
35: | Watermark watermark = new Watermark(Image.GetInstance("watermark.jpg"), 200, 320); | |
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 i = 0; | |
45: | int p = 0; | |
46: | Console.WriteLine("There are " + n + " pages in the document."); | |
47: | while (i < n) { | |
48: | document.NewPage(); | |
49: | p++; | |
50: | i++; | |
51: | PdfImportedPage page1 = writer.GetImportedPage(reader, i); | |
52: | cb.AddTemplate(page1, .5f, 0, 0, .5f, 0, height / 2); | |
53: | Console.WriteLine("processed page " + i); | |
54: | if (i < n) { | |
55: | i++; | |
56: | PdfImportedPage page2 = writer.GetImportedPage(reader, i); | |
57: | cb.AddTemplate(page2, .5f, 0, 0, .5f, width / 2, height / 2); | |
58: | Console.WriteLine("processed page " + i); | |
59: | } | |
60: | if (i < n) { | |
61: | i++; | |
62: | PdfImportedPage page3 = writer.GetImportedPage(reader, i); | |
63: | cb.AddTemplate(page3, .5f, 0, 0, .5f, 0, 0); | |
64: | Console.WriteLine("processed page " + i); | |
65: | } | |
66: | if (i < n) { | |
67: | i++; | |
68: | PdfImportedPage page4 = writer.GetImportedPage(reader, i); | |
69: | cb.AddTemplate(page4, .5f, 0, 0, .5f, width / 2, 0); | |
70: | Console.WriteLine("processed page " + i); | |
71: | } | |
72: | cb.SetRGBColorStroke(255, 0, 0); | |
73: | cb.MoveTo(0, height / 2); | |
74: | cb.LineTo(width, height / 2); | |
75: | cb.Stroke(); | |
76: | cb.MoveTo(width / 2, height); | |
77: | cb.LineTo(width / 2, 0); | |
78: | cb.Stroke(); | |
79: | BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); | |
80: | cb.BeginText(); | |
81: | cb.SetFontAndSize(bf, 14); | |
82: | cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 4) + (n % 4 > 0? 1 : 0)), width / 2, 40, 0); | |
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: | } |