| 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: | /// reading PDF | |
| 12: | /// </summary> | |
| 13: | public class Chap0112 | |
| 14: | { | |
| 15: | public Chap0112() | |
| 16: | { | |
| 17: | Console.WriteLine("Chapter 1 example 12: reading an existing PDF file"); | |
| 18: | ||
| 19: | try | |
| 20: | { | |
| 21: | // we create a reader for a certain document | |
| 22: | PdfReader reader = new PdfReader("Chap0703.pdf"); | |
| 23: | // we retrieve the total number of pages | |
| 24: | int n = reader.NumberOfPages; | |
| 25: | // we retrieve the size of the first page | |
| 26: | Rectangle psize = reader.GetPageSize(1); | |
| 27: | float width = psize.Width; | |
| 28: | float height = psize.Height; | |
| 29: | ||
| 30: | // step 1: creation of a document-object | |
| 31: | Document document = new Document(psize, 50, 50, 50, 50); | |
| 32: | // step 2: we create a writer that listens to the document | |
| 33: | PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0112.pdf", FileMode.Create)); | |
| 34: | // step 3: we open the document | |
| 35: | try | |
| 36: | { | |
| 37: | Watermark watermark = new Watermark(Image.GetInstance("watermark.jpg"), 200, 320); | |
| 38: | document.Add(watermark); | |
| 39: | } | |
| 40: | catch(Exception e) | |
| 41: | { | |
| 42: | Console.Error.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?"); | |
| 43: | } | |
| 44: | document.Open(); | |
| 45: | // step 4: we add content | |
| 46: | PdfContentByte cb = writer.DirectContent; | |
| 47: | int i = 0; | |
| 48: | int p = 0; | |
| 49: | Console.WriteLine("There are " + n + " pages in the document."); | |
| 50: | while (i < n) | |
| 51: | { | |
| 52: | document.NewPage(); | |
| 53: | p++; | |
| 54: | i++; | |
| 55: | PdfImportedPage page1 = writer.GetImportedPage(reader, i); | |
| 56: | cb.AddTemplate(page1, .5f, 0, 0, .5f, 0, height / 2); | |
| 57: | Console.WriteLine("processed page " + i); | |
| 58: | if (i < n) | |
| 59: | { | |
| 60: | i++; | |
| 61: | PdfImportedPage page2 = writer.GetImportedPage(reader, i); | |
| 62: | cb.AddTemplate(page2, .5f, 0, 0, .5f, width / 2, height / 2); | |
| 63: | Console.WriteLine("processed page " + i); | |
| 64: | } | |
| 65: | if (i < n) | |
| 66: | { | |
| 67: | i++; | |
| 68: | PdfImportedPage page3 = writer.GetImportedPage(reader, i); | |
| 69: | cb.AddTemplate(page3, .5f, 0, 0, .5f, 0, 0); | |
| 70: | Console.WriteLine("processed page " + i); | |
| 71: | } | |
| 72: | if (i < n) | |
| 73: | { | |
| 74: | i++; | |
| 75: | PdfImportedPage page4 = writer.GetImportedPage(reader, i); | |
| 76: | cb.AddTemplate(page4, .5f, 0, 0, .5f, width / 2, 0); | |
| 77: | Console.WriteLine("processed page " + i); | |
| 78: | } | |
| 79: | cb.SetRGBColorStroke(255, 0, 0); | |
| 80: | cb.MoveTo(0, height / 2); | |
| 81: | cb.LineTo(width, height / 2); | |
| 82: | cb.Stroke(); | |
| 83: | cb.MoveTo(width / 2, height); | |
| 84: | cb.LineTo(width / 2, 0); | |
| 85: | cb.Stroke(); | |
| 86: | BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); | |
| 87: | cb.BeginText(); | |
| 88: | cb.SetFontAndSize(bf, 14); | |
| 89: | cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 4) + (n % 4 > 0? 1 : 0)), width / 2, 40, 0); | |
| 90: | cb.EndText(); | |
| 91: | } | |
| 92: | // step 5: we close the document | |
| 93: | document.Close(); | |
| 94: | } | |
| 95: | catch (Exception de) | |
| 96: | { | |
| 97: | Console.Error.WriteLine(de.Message); | |
| 98: | Console.Error.WriteLine(de.StackTrace); | |
| 99: | } | |
| 100: | ||
| 101: | } | |
| 102: | } | |
| 103: | } |