/* * This code is free software. It may only be copied or modified * if you include the following copyright notice: * * --> Copyright 2001 by Bruno Lowagie <-- * --> Copyright 2004 by Kazuya Ujihara <-- * * This code is example code of 'iText .NET'. * See http://www.ujihara.jp/iTextdotNET/examples.html * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ using System; using com.lowagie.text; using com.lowagie.text.pdf; using System.IO; public class Chap0112 { public static void Main(string[] args) { Console.WriteLine("Chapter 1 example 12: reading an existing PDF file"); // we create a reader for a certain document PdfReader reader = new PdfReader("Chap0703.pdf"); // we retrieve the total number of pages int n = reader.getNumberOfPages(); // we retrieve the size of the first page Rectangle psize = reader.getPageSize(1); float width = psize.width(); float height = psize.height(); // step 1: creation of a document-object Document document = new Document(psize, 50, 50, 50, 50); // step 2: we create a writer that listens to the document PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0112.pdf", FileMode.Create)); // step 3: we open the document try { Watermark watermark = new Watermark(Image.getInstance("watermark.jpg"), 200, 320); document.add(watermark); } catch(Exception e) { Console.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?"); } document.open(); // step 4: we add content PdfContentByte cb = writer.getDirectContent(); int i = 0; int p = 0; Console.WriteLine("There are " + n + " pages in the document."); while (i < n) { document.newPage(); p++; i++; PdfImportedPage page1 = writer.getImportedPage(reader, i); cb.addTemplate(page1, .5f, 0, 0, .5f, 0, height / 2); Console.WriteLine("processed page " + i); if (i < n) { i++; PdfImportedPage page2 = writer.getImportedPage(reader, i); cb.addTemplate(page2, .5f, 0, 0, .5f, width / 2, height / 2); Console.WriteLine("processed page " + i); } if (i < n) { i++; PdfImportedPage page3 = writer.getImportedPage(reader, i); cb.addTemplate(page3, .5f, 0, 0, .5f, 0, 0); Console.WriteLine("processed page " + i); } if (i < n) { i++; PdfImportedPage page4 = writer.getImportedPage(reader, i); cb.addTemplate(page4, .5f, 0, 0, .5f, width / 2, 0); Console.WriteLine("processed page " + i); } cb.setRGBColorStroke(255, 0, 0); cb.moveTo(0, height / 2); cb.lineTo(width, height / 2); cb.stroke(); cb.moveTo(width / 2, height); cb.lineTo(width / 2, 0); cb.stroke(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 14); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 4) + (n % 4 > 0? 1 : 0)), width / 2, 40, 0); cb.endText(); } // step 5: we close the document document.close(); } }