> 
> Bruno Lowagie wrote:
> 
> Sorry, but I don't have the time,
> you'll have to Google for the answer.
> It's out there somewhere.
> br,
> Bruno
> 

Hello Bruno!

Sorry but I need your help again! I found an example to find a PdfTemplate in 
a Document. This example was in the mail list. In this example an Image will be
replaced with another Image. I only changed the type from IMAGE to FORM and 
found the PdfTemplate in my Document. But there is no method to add a new
PdfTemplate. Therefore I create a new Template, wrap this template in an Image 
and add this Image to the Document. But nothing happens?

Could you please explain me, what I do wrong?

Br,
Eric

Code:

PdfReader pdf = new PdfReader("Test.pdf");
PdfStamper stp = new PdfStamper(pdf, new FileOutputStream("Test2.pdf"));
PdfWriter writer = stp.getWriter();
PdfDictionary pg = pdf.getPageN(1);
PdfDictionary res = (PdfDictionary)PdfReader.getPdfObject(pg.get
(PdfName.RESOURCES));
PdfDictionary xobj = (PdfDictionary)PdfReader.getPdfObject(res.get
(PdfName.XOBJECT));
if (xobj != null) {
   for (Iterator it = xobj.getKeys().iterator(); it.hasNext();) {
      PdfObject obj = xobj.get((PdfName)it.next());
      if (obj.isIndirect()) {
         PdfDictionary tg =(PdfDictionary)PdfReader.getPdfObject(obj);
         PdfName type = (PdfName)PdfReader.getPdfObject(tg.get
(PdfName.SUBTYPE));
         if (PdfName.FORM.equals(type)) {
            //PdfReader.killIndirect(obj);

            BaseFont font = BaseFont.createFont(BaseFont.TIMES_ROMAN, 
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
            String text = "Test";
            float fontSize = 10;

            PdfTemplate tpl =  stp.getOverContent(1).createTemplate(10, 10);
            tpl.beginText();
            tpl.setFontAndSize(font, fontSize);
            tpl.setTextMatrix(0, 0);
            tpl.showText(text);
            tpl.endText();

            Image image = Image.getInstance(tpl);
            writer.addDirectImageSimple(image);
            writer.addDirectImageSimple(image, (PRIndirectReference)obj);
            break;
         }
      }
   }
}
stp.close();





-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to