Attached is a stripped-down version of com.lowagie.examples.general.copystamp.AddWatermarkPageNumbers.java - it uses the same files as in the example.
Putting together this example helped me further
track down the fault to the constructor:
PdfReader reader = new PdfReader(new
RandomAccessFileOrArray("ChapterSection.pdf"),
null);
If this is replaced with
PdfReader reader = new
PdfReader("ChapterSection.pdf");
then everything goes fine, regardless of the
sequence of the method calls
The problem lies somewhere in these 2 methods:
reader.getPageSizeWithRotation(i)
reader.getPageRotation(i)
--- Paulo Soares <[EMAIL PROTECTED]> wrote:
> As I said create a small example with the
> problem. I ask this because
> most of the times the devil is in the details
> and only running something
> exactly the same that you also ran can solve
> it.
>
__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com/* * $Id: AddWatermarkPageNumbers.java,v 1.3 2005/05/09 11:52:51 blowagie Exp $ * $Name: $ * * This code is part of the 'iText Tutorial'. * You can find the complete tutorial at the following address: * http://itextdocs.lowagie.com/tutorial/ * * 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. * * [email protected] */ package com.lowagie.examples.general.copystamp; import java.io.FileOutputStream; import java.util.HashMap; import com.lowagie.text.*; import com.lowagie.text.pdf.*; /** * Reads the pages of an existing PDF file, adds pagenumbers and a watermark. */ public class AddWatermarkPageNumbers2 { /** * Reads the pages of an existing PDF file, adds pagenumbers and a watermark. * @param args no arguments needed */ public static void main(String[] args) { System.out.println("Add watermarks and pagenumbers"); try { // we create a reader for a certain document PdfReader reader = new PdfReader(new RandomAccessFileOrArray("ChapterSection.pdf"), null); int n = reader.getNumberOfPages(); // we create a stamper that will copy the document to a new file PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("watermark_pagenumbers.pdf")); int i = 0; PdfContentByte over; Image img = Image.getInstance("watermark.jpg"); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED); img.setAbsolutePosition(200, 400); while (i < n) { i++; // text over the existing page over = stamp.getOverContent(i); //int rotation=reader.getPageRotation(i); Rectangle r=reader.getPageSizeWithRotation(i); int rotation=r.getRotation(); over.beginText(); over.setFontAndSize(bf, 18); over.setTextMatrix(30, 30); over.showText("page " + i); over.setFontAndSize(bf, 32); over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45); over.endText(); } stamp.close(); } catch (Exception de) { de.printStackTrace(); } } }
ChapterSection.pdf
Description: 2738391535-ChapterSection.pdf
SimpleAnnotations1.pdf
Description: 1456326483-SimpleAnnotations1.pdf
<<attachment: watermark.jpg>>
