Here's some code to get you started:
PdfReader pdf = new PdfReader("in.pdf");
PdfStamper stp = new PdfStamper(pdf, new FileOutputStream("out.pdf"));
PdfWriter writer = stp.getWriter();
Image img = Image.getInstance("image.png");
PdfContentParser cp = new PdfContentParser(new
PRTokeniser(pdf.getPageContent(1)));
PdfName first = null;
while (true) {
ArrayList ar = cp.parse(null);
if (ar.size() == 0)
break;
if ("Do".equals(ar.get(ar.size() - 1).toString())) {
first = (PdfName)ar.get(0);
break;
}
}
if (first != null) {
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) {
PdfObject obj = xobj.get(first);
if (obj.isIndirect()) {
PdfDictionary tg =
(PdfDictionary)PdfReader.getPdfObject(obj);
PdfName type =
(PdfName)PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE));
if (PdfName.IMAGE.equals(type)) {
PdfReader.killIndirect(obj);
Image maskImage = img.getImageMask();
if (maskImage != null)
writer.addDirectImageSimple(maskImage);
writer.addDirectImageSimple(img,
(PRIndirectReference)obj);
}
}
}
}
stp.close();
Paulo
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On
> Behalf Of java.jago
> Sent: Sunday, June 04, 2006 3:42 AM
> To: [email protected]
> Subject: Re: [iText-questions] Generate PDF, send it to the
> server and add a Background Image on each page...isthis
> possible with iText ???
>
> Paulo Soares wrote:
>
> > Have a look at
> >
> http://article.gmane.org/gmane.comp.java.lib.itext.general/226
> 48. To know
> > what's the image to replace use PdfContentParser to get the
> first "Do"
> > command. The operand will be a name that will point to the
> image in the
> > resource dictionary. You don't need to change the content,
> just the image.
> >
> > Paulo
>
> thanks for the answer,
>
> In http://article.gmane.org/gmane.comp.java.lib.itext.general/22648
> there is a new pdf created - can I also do in-place editing of the
> original-pdf ?
>
> I don't get this completely: to get the first "Do" command.
> The operand
> will be a name that will point to the image in the resource
> dictionary.
>
> Can you explain it in detail ?
>
> Cheers,
> jago
>
>
>
> >
> > ----- Original Message -----
> > From: "Andreas Schmidt" <[EMAIL PROTECTED]>
> > To: <[email protected]>
> > Sent: Saturday, June 03, 2006 12:55 AM
> > Subject: [iText-questions] Generate PDF,send it to the
> server and add a
> > Background Image on each page...isthis possible with iText ???
> >
> >
> >
> >>Hi,
> >>
> >>>From what I read it is impossible to add more content to a
> PDF with iText
> >>once it is "created"/"closed" ?
> >>
> >>What I want to do:
> >>
> >>The user can create pdfs and sends them to a server. (i have done
> >>everything
> >>to that point).
> >>
> >>On every page of the pdf there is a background-image (in
> the layer given
> >>by
> >>getDirectContentUnder()). This background image has a low
> resolution and
> >>is
> >>supposed to be replaced by a high-resolution version of
> that image, which
> >>only resides on the server.
> >>
> >>>From what I read in the iText Api/Tutorial such a
> replcement/edit is not
> >>possible with iText - but perhaps I am missing out
> something hopefully ? I
> >>would really like to do it this way - is there perhaps
> another library
> >>that
> >>can do such an edit ?
> >>
> >>Cheers,
> >>jago
Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter
informação confidencial ou legalmente protegida. A incorrecta transmissão desta
mensagem não significa a perca de confidencialidade. Se esta mensagem for
recebida por engano, por favor envie-a de volta para o remetente e apague-a do
seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de
usar, revelar ou distribuir qualquer parte desta mensagem.
Disclaimer:
This message is destined exclusively to the intended receiver. It may contain
confidential or legally protected information. The incorrect transmission of
this message does not mean the loss of its confidentiality. If this message is
received by mistake, please send it back to the sender and delete it from your
system immediately. It is forbidden to any person who is not the intended
receiver to use, distribute or copy any part of this message.
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions