In JAVA, you can use a package provided by a third party to convert pdf to pictures. Here are several commonly used ones that can be selected and used according to your own needs.
1. Icepdf. There are paid and open source versions, the most recommended among several methods. The conversion effect is relatively good and can identify the Chinese in the file I have. That is, after the conversion, the spacing between the characters in the font may be a bit wide. Because font support is charged, the converted pictures will have an official watermark. How to remove watermarks, you can view another article: icepdf method for removing watermarks
1. Download the icepdf package and import it into the project. 4 are used here, as follows:
2. Attach code example:
String filePath = "c:/test.pdf"; Document document = new Document(); document.setFile(filePath); float scale = 2.5f;//Scaling float rotation = 0f;//Rotation angle for (int i = 0; i < document.getNumberOfPages(); i++) { BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale); RenderedImage spandImage = image; try { File file = new File("c:/iecPDF_" + i + ".png"); ImageIO.write(rendImage, "png", file); 14 } catch (IOException e) { e.printStackTrace(); } image.flush(); } document.dispose();In the example, it is in pdf to png format. You can also change lines 12 and 13 to jpg and transfer them to jpg format, but from the conversion effect, the clarity of png will be relatively high. There is a trick to change 12 lines to jpg, but 13 lines to use png, that is, convert pictures to jpg format but with png clarity.
2. PDFbox. The conversion effect is OK, which can recognize most of the contents of the files in my hands, and some of them cannot be recognized.
1. Download the pdfbox rack package and import the project. Two of them are used here, as follows:
2. Attach code example:
File file = new File("c://test.pdf"); try { PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); 5 int pageCount = doc.getNumberOfPages(); for(int i=0;i<pageCount;i++){ BufferedImage image = renderer.renderImageWithDPI(i, 296);// BufferedImage image = renderer.renderImage(i, 2.5f); ImageIO.write(image, "PNG", new File("C://pdfbox_image.png")); } } catch (IOException e) { e.printStackTrace(); }In the example, the second parameter of rederImageWithDPI is dpi resolution unit, which can be adjusted according to the requirements. The eighth line of the code provides another method of turning pictures in the package, and the second parameter is the scaling ratio.
3. jpedal. The effect is not ideal, and it seems that the support for Chinese is not very good. The following lgpl version is an open source version.
1. Download the jpedal package and import it into the project as follows:
2. Attach code example:
PdfDecoder decode_pdf = new PdfDecoder(true); try { decode_pdf.openPdfFile("c://test.pdf"); //file// decode_pdf.openPdfFile("C:/jpedalPDF.pdf", "password"); //encrypted file// decode_pdf.openPdfArray(bytes); //bytes is byte[] array with PDF// decode_pdf.openPdfFileFromURL("http://www.mysite.com/jpedalPDF.pdf",false);// decode_pdf.openPdfFileFromInputStream(in, false); int start = 1, end = decode_pdf.getPageCount(); for(int i = start; i < end+1; i++){ BufferedImage img=decode_pdf.getPageAsImage(i); try { ImageIO.write(img, "png", new File("C://jpedal_image.png")); } catch (IOException e) { e.printStackTrace(); } } decode_pdf.closePdfFile(); } catch (PdfException e) { e.printStackTrace(); }Lines 3-7 of the example also provide several different ways to open PDFs, which can be selected and used according to your needs.
The above method of implementing PDF to pictures in Java is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.