1

I am getting a pdf in byte array. I want to convert just the 1st page of the pdf into image.

I have tired the classes provided by com.lowagie.text.pdf as follows -

PdfReader reader = new PdfReader(input);
reader.selectPages("1");
File file = new File("D:/img1.jpg");
BufferedImage pdfImage = ImageIO.read(new ByteArrayInputStream(reader.getPageContent(1)));
ImageIO.write(pdfImage, "jpg", file);

Doing this gives me an Exception when ImageIO.write is called? When I fetch the size of the byte array returned by reader.getPageContent(1), I get a 1000+ value. What confuses me is why do I get the Exception.

Exception -

java.lang.IllegalArgumentException: image == null!

I tried itext as well but it was of no use.

Could you suggest me a way to get just image of the 1st page (1st page as image) from the byte array of the pdf file?

JHS
  • 7,761
  • 2
  • 29
  • 53
  • I don't think `getPageContent` returns an image. Are you sure this library offers such functionality? – Piotr Praszmo Jun 02 '12 at 14:25
  • @Banthar - `getPageContent`returns `byte array` which i pass to get a `ByteArrayInputStream` and then I am trying to write an `Image`. – JHS Jun 02 '12 at 14:29

2 Answers2

2

Answering on my own question so that others can be benefited with it. After some research I found it and got the solution.

Have a look at this link.

PDFDocumentReader document = new PDFDocumentReader(<byteArraOfThePDF>);
PageDetail pageDetail = new PageDetail("<docIDanything>", "", <pagenumber>, "");
ResourceDetail det = document.getPageAsImage(pageDetail);

BufferedImage image = ImageIO.read(new ByteArrayInputStream(det.getBytes()));
File file = new File("d:/img2.jpg");
ImageIO.write(image, "jpg", file);
JHS
  • 7,761
  • 2
  • 29
  • 53
1

As far as I know this is not possible with iText (at least some time ago while I searched for a similar issue).

But you can use the PDFToImage from Apache PDFBox:

String [] args =  new String[7];
args[0] = "-startPage";
args[1] = "1";
args[2] = "-endPage";
args[3] = "1";
args[4] = "-outputPrefix";
args[5] = "MyJpgFile";
args[6] = "MyPdfFile";

PDFToImage.main(args);

It's easy to write a wrapper for this. Perhaps such a wrapper is available in PDFBox in the meantime.

Thor
  • 6,607
  • 13
  • 62
  • 96