Hello,

I am trying to add & retrieve a bmp image in the last page of a pdf file using 
iText. My code, so far, is the following:

private void cmd_addImageToPDF() throws Exception {

PdfReader reader = new PdfReader(pdfFileName.getName()); 
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("output.pdf"));
PdfWriter writer = stamp.getWriter();
PdfContentByte content = stamp.getOverContent(reader.getNumberOfPages());
com.lowagie.text.Image img = 
com.lowagie.text.Image.getInstance(BMPFileLocation);

//img.setDirectReference(new PRIndirectReference(reader, 10));
img.setAbsolutePosition(450,10);
content.addImage(img);

stamp.close();
reader.close();
}

The image is added perfectly in the last page of the pdf.

The problem comes when I try to retrieve and re-create the BMP image. The code 
goes like that:

private void retrieveImageFromPDF() {

try {

PdfReader reader = new PdfReader(pdfFileName.getName());

for (int i = 0; i < reader.getXrefSize(); i++) {
PdfObject pdfobj = reader.getPdfObject(i);
if (pdfobj != null) {
if (pdfobj.isStream()) {
PdfStream pdfdict = (PdfStream) pdfobj;
PdfObject pdfsubtype = pdfdict.get(PdfName.SUBTYPE);
if (pdfsubtype == null) {
continue;
}
if (!pdfsubtype.toString().equals(
PdfName.IMAGE.toString())) {
continue;
}
System.out.println("height:"
+ pdfdict.get(PdfName.HEIGHT));
System.out.println("width:"
+ pdfdict.get(PdfName.WIDTH));
System.out.println("bitspercomponent:"
+ pdfdict.get(PdfName.BITSPERCOMPONENT));
System.out.println(pdfdict.getRawLength()+" ");
byte[] barr = PdfReader.getStreamBytesRaw((PRStream) pdfdict);

stegoImage = Toolkit.getDefaultToolkit().createImage(barr);
System.out.println(pdfdict.toString()+" ");

System.out.println(barr.length);

for ( int a = 0; a < barr.length; a++ )
System.out.print(barr[a]+" ");

return;
}
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

So when stegoImage (awt.Image) is re-created by the pdf file, I try to write it 
to a file, using a specific class re-creating the BMP file using the raw data 
(awt.Image --> File bmp).
But all I am left with is a black bmp image.

Is this the correct way to go, or am I missing something? Should I retrieve the 
image's bytes using something like pdfbox?

My main requirement is to retrieve the image bytes, exactly as they are, 
because I am "embedding" hidden information in them, which will be retrieved
using other functions operating on the bmp file.

Thanks in advance and sorry for my inexpertise but I have just started 
experimenting with iText,

Antony
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
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