Here's the code to replace images in PDFs. It will replace 
the first image in the first page.

PdfReader pdf = new PdfReader("in.pdf");
PdfStamper stp = new PdfStamper(pdf, new
FileOutputStream("c:\\out.pdf"));
PdfWriter writer = stp.getWriter();
Image img = Image.getInstance("image.png");
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.IMAGE.equals(type)) {
                PdfReader.killIndirect(obj);
                Image maskImage = img.getImageMask();
                if (maskImage != null)
                    writer.addDirectImageSimple(maskImage);
                writer.addDirectImageSimple(img,
(PRIndirectReference)obj);
                break;
            }
        }
    }
}
stp.close(); 

Paulo

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of aqeel saifi
> Sent: Tuesday, October 31, 2006 7:44 AM
> To: [email protected]
> Subject: [iText-questions] Modifying the images in PDF
> 
> Hi All,
> As far as I have digged, we can not directly modify the 
> images in pdf file with iText. Although we can get the image 
> data from the XObject. right?
> Actually I intend to create a new pdf from an existing one 
> with some of the contained images changed/repalced with some other.
> As we can't replace image directly with iText, I have two 
> options in mind to achieve this.
> a. Is it possible that I render the changed image (different 
> image) on the existing image position to give the effect that 
> image is actually changed.
> b. Other could be, if possible, I create new pdf from the 
> existing one and all the pdf objects to new one except the 
> images to be modified. In place of these images I can add my 
> changed images to new pdf.
> 
> Please guide if any of this is feasible, please share if you 
> have any other option in mind.
> 
> Any help would be greatly appreciated.
> 
> Thanks
> aqueel
> 
> ________________________________
> 
> Find out what India is talking about on - Yahoo! Answers 
> India 
> <http://us.rd.yahoo.com/mail/in/yanswers/*http://in.answers.ya
> hoo.com/>  
> Send FREE SMS to your friend's mobile from Yahoo! Messenger 
> Version 8. Get it NOW 
> <http://us.rd.yahoo.com/mail/in/messengertagline/*http://in.me
> ssenger.yahoo.com> 
> 


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.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to