> There are no examples on annotations but how wrong can you get with one
> method call?
> Did you try?

OK, you're right. I've just tested it again and it works fine.


> You made all the pages the size of the first page size, which may not
> necessarily be true. You'll have to make each page size the size of the
> media box and then apply the size of the crop box in PdfWriter.

I've tried to do exactly what you say, but it doesn't seem to work 100 per
cent.
Some pages are not the size they should be.
Maybe you can help me again?


Here's the code:

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.util.*;
import java.math.*;
import java.awt.Color;
import java.io.*;

public class AddAnnotationToPDF {

   public static void main(String[] args) {

      try {

         // random filename
         String checkPDF = new String(args[0]);
         int randInt = (int) (Math.random()*1000000000);
         String errorDocFilename =
checkPDF.substring(0,(checkPDF.length()-4)) + "_" + randInt + ".pdf";

         // creating the PdfReader
         PdfReader reader = new PdfReader(args[0]);

         // retrieving the total number of pages
         int n = reader.getNumberOfPages();

         // retrieving the size of the first page
         Rectangle psize = reader.getPageSize(1);
         float width = psize.width();
         float height = psize.height();
         float llx = (width / 2);
         float lly = (height / 2);
         float urx = (width / 2);
         float ury = (height / 2);

         // creation of a document-object
         Document document = new Document();

         // creating a writer that listens to the document
         PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(errorDocFilename));

         // opening the document
         document.open();

         // adding the content
         PdfContentByte cb = writer.getDirectContent();
         int i = 1;
         System.out.println("Page count: " + n);

         while (i <= n) {

            document.newPage();
            document.setPageSize(reader.getPageSize(i));
            PdfImportedPage page = writer.getImportedPage(reader, i);
            writer.setCropBoxSize(reader.getCropBox(i));
            cb.addTemplate(page, 0, 0);

            // adding an annotation to the first page
            if (i == 1) {

               Rectangle rect = new Rectangle(llx - 10, lly - 10, urx + 10,
ury + 10);

               String errorText = new String ("This is a text!");
               PdfAnnotation pdfan = PdfAnnotation.createText(writer, rect,
"Title", errorText, true, "icon");
               pdfan.setColor(new Color(255,0,0));

               writer.addAnnotation(pdfan);
            }

            i++;
         }

         // closing the document
         document.close();

      } catch (Exception de) {
         de.printStackTrace();
      }
   }
}


Best regards,
Sven





-------------------------------------------------------
This SF.net email is sponsored by: eBay
Get office equipment for less on eBay!
http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to