The examples in this article share with you the specific code of Java converting pdf image for your reference. The specific content is as follows
First, the PDFBox component 1.8.4 is used using apache.
package pdf; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Date; import java.util.List; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; public class PDFBox { @SuppressWarnings("rawtypes") public static void main(String[] args) throws IOException { String p=System.getProperty("user.dir") + "/"+"zk.pdf"; PDDocument doc = PDDocument.load(p); int pageCount = doc.getNumberOfPages(); System.out.println(pageCount); Date start = new Date(); try { List pages = doc.getDocumentCatalog().getAllPages(); for(int i=0;i<pages.size();i++){ PDPage page = (PDPage) pages.get(i); @SuppressWarnings("unused") int width = new Float(page.getTrimBox().getWidth()).intValue(); @SuppressWarnings("unused") int height = new Float(page.getTrimBox().getHeight()).intValue(); BufferedImage image = page.convertToImage(); ImageIO.write(image, "jpg", new File("img" + File.separator + (i + 1) + ".jpg")); System.out.println("image in the page -->"+(i+1)); } } catch (Exception e) { e.printStackTrace(); } finally{ if(doc != null){ doc.close(); } } Date end = new Date(); System.out.println(end.getTime()-start.getTime()); System.out.println("over"); } }But the problem lies in the problem:
When the PDF document is 180M, it directly reports a parsing exception
Processing is very slow when the number of PDF pages is more than 500 pages
Later, I tried using pdf-renderer version 1.0.5
package pdf; import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage; public class PDFRenderer { public static void main(String[] args) throws IOException{ String pdfRealePath=System.getProperty("user.dir") + "/"+"zk.pdf"; File file = new File(pdfRealePath); RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdffile = new PDFFile(buf); for (int i = 1; i <= pdffile.getNumPages(); i++) { PDFPage page = pdffile.getPage(i); Rectangle rect = new Rectangle(0, 0, ((int) page.getBBox() .getWidth()), ((int) page.getBBox().getHeight())); Image img = page.getImage(rect.width, rect.height, rect, null,true,true); BufferedImage tag = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height, null); FileOutputStream out = new FileOutputStream("img" + File.separator + (i + 1) + ".jpg"); // Output to file stream JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag); param2.setQuality(1f, false); // 1f is to improve the generated image quality encoder.setJPEGEncodeParam(param2); encoder.encode(tag); // JPEG encoding out.close(); System.out.println("image in the page -->"+(i+1)); } } }But the problem lies in the problem: When the version of pdf is not 1.4, an error is reported directly: Expected 'xref' at start of table
Compared with pdfbox, conversion is much less efficient. A pdf with about 200 pages takes about 6 times the time it takes for the latter. At the same time, there are some problems with the support for Chinese fonts.
However, there is no problem that the PDF version cannot be converted.
pdfrenderer cannot convert 1.4 or above versions. I found the solution but was not found.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.