Hi,
using the below listed code i can only replace the first image of each and
every page of a PDF file.how to replace all the images in a pdf file.
//code
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfAnnotation;
import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfAppearance;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfFileSpecification;
import com.lowagie.text.pdf.PdfName;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PRIndirectReference;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.AcroFields;
/**
* Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
*/
public class ImageLink1 {
static String pdfFileName;
static int i=1;
public static void main(String[] args) {
try {
// we create a reader for a certain document
String filename;
// JFileChooser jff1=new JFileChooser();
// jff1.showOpenDialog(null);
// filename=jff1.getSelectedFile().getPath();
//
PdfReader pdf = new PdfReader("0768668336.pdf");
int n = pdf.getNumberOfPages();
PdfStamper stp = new PdfStamper(pdf, new
FileOutputStream("out.pdf"));
while(i<=n)
{
PdfWriter writer = stp.getWriter();
Image img = Image.getInstance("image.jpg");
PdfDictionary pg = pdf.getPageN(i);
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;
}
}
}
}
i=i+1;
}
stp.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
}
Thanks and Regards,
Selvi. K
-------------------------------------------------------------------------
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
Buy the iText book: http://itext.ugent.be/itext-in-action/