Get the 1st page of a pdf as Image from the Byte Array of the pdf

后端 未结 2 403
不思量自难忘°
不思量自难忘° 2021-01-07 08:43

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

相关标签:
2条回答
  • 2021-01-07 09:04

    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.

    0 讨论(0)
  • 2021-01-07 09:17

    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);
    
    0 讨论(0)
提交回复
热议问题