Discussion:
[iText-questions] How can I extract Image from pdf file
u***@empal.com
2007-07-05 08:27:26 UTC
Permalink
Hello
My English ability is very poor. sorry
I want to read a pdf file
and save image to file
I try but..
for (int i = 0; i < reader.getXrefSize(); i++)
{
PdfObject pdfobj = reader.getPdfObject(i);
}

first I read file
and then getPdfObject....
How can I extract image and save it
plz help me

--
This message was sent on behalf of ***@empal.com at openSubscriber.com
http://www.opensubscriber.com/messages/itext-***@lists.sourceforge.net/topic.html
u***@empal.com
2007-07-05 08:28:05 UTC
Permalink
Hello
My English ability is very poor. sorry
I want to read a pdf file
and save image to file
I try but..
for (int i = 0; i < reader.getXrefSize(); i++)
{
PdfObject pdfobj = reader.getPdfObject(i);
}

first I read file
and then getPdfObject....
How can I extract image and save it
plz help me

--
This message was sent on behalf of ***@empal.com at openSubscriber.com
http://www.opensubscriber.com/messages/itext-***@lists.sourceforge.net/topic.html
Bruno Lowagie
2007-07-05 13:54:41 UTC
Permalink
Post by u***@empal.com
Hello
My English ability is very poor. sorry
I want to read a pdf file
and save image to file
I try but..
for (int i = 0; i < reader.getXrefSize(); i++)
{
PdfObject pdfobj = reader.getPdfObject(i);
}
first I read file
and then getPdfObject...
Not every object in a PDF is an image.
Images are stored in PDF objects of type stream,
but not all streams are images, so once you
have your pdfobj, you need to do the following checks:

if (!pdfobj.isStream()) {
throw new Exception("Not a stream");
}
PdfStream stream = (PdfStream) pdfobj;
PdfObject pdfsubtype = streal.get(PdfName.SUBTYPE);
if (pdfsubtype == null) {
throw new Exception("Not an image stream");
}
if (!pdfsubtype.toString().equals(PdfName.IMAGE.toString())) {
throw new Exception("Not an image stream");
}
// now you have a PDF stream object with an image
byte[] img = PdfReader.getStreamBytesRaw((PRStream) stream);
// but you don't know anything about the image format.
// you'll have to get info from the stream dictionary
System.out.println("height:" + stream.get(PdfName.HEIGHT));
System.out.println("width:" + stream.get(PdfName.WIDTH));
System.out.println("bitspercomponent:" +
stream.get(PdfName.BITSPERCOMPONENT));
...
// or you could try making a java.awt.Image from the array:
java.awt.Image i = Toolkit.getDefaultToolkit().createImage(img);

In any case, it's not as simple as you would think.
br,
Bruno

Continue reading on narkive:
Loading...