In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how JAVA turns PDF into pictures". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how JAVA turns PDF into a picture.
1. Reference: the versions of fontbox-2.0.16.jar and pdfbox-app-2.0.16.jar must be correct, otherwise there will be a problem with the code.
Main function:
Package kevin.cn;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.imageio.ImageIO;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;import org.apache.pdfbox.rendering.ImageType;import org.apache.pdfbox.rendering.PDFRenderer;import org.apache.pdfbox.tools.PDFBox Import kevin.cn.PdfUtil;@SuppressWarnings ("unused") public class Test {/ / after testing, the dpi is 96100105120150200, the display effect is clear and the volume is stable, the higher the dpi, the larger the picture size, the general computer display resolution is 96 public static final float DEFAULT_DPI = 105; / / the default converted image format is jpg public static final String DEFAULT_FORMAT = "jpg". Public static void main (String [] args) throws Exception {pdfToImage ("/ media/kevin/FileData/JavaCode/pdfboxTest/ Enterprise Informatization Construction thesis .pdf", "/ media/kevin/FileData/JavaCode/pdfboxTest/img/7.jpg", 5);}
Implement the function:
/ * pdf to picture * * @ param pdfPath PDF path * @ imgPath img path * @ page_end the page number to be converted, you can also define the start page number and the end page number. I only need one page here. Add * / public static void pdfToImage (String pdfPath, String imgPath,int page_end) {try {/ / Image combination parameter / / total width int width = 0 according to the demand / / Save the RGB data in a picture int [] singleImgRGB; int shiftHeight = 0; / / Save the pixel value of each picture BufferedImage imageResult = null; / / generate an image using PdfBox PDDocument pdDocument = PDDocument.load (new File (pdfPath)); PDFRenderer renderer = new PDFRenderer (pdDocument); / / cycle each page number for (int I = 0, len = pdDocument.getNumberOfPages (); I
< len; i++) { if (i==page_end) { BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB); int imageHeight = image.getHeight(); int imageWidth = image.getWidth(); //计算高度和偏移量 //使用第一张图片宽度; width = imageWidth; //保存每页图片的像素值 imageResult = new BufferedImage(width, imageHeight, BufferedImage.TYPE_INT_RGB); //这里有高度,可以将imageHeight*len,我这里值提取一页所以不需要 singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width); // 写入流中 imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); }else if(i>Page_end) {continue;}} pdDocument.close (); / / write picture ImageIO.write (imageResult, DEFAULT_FORMAT, new File (imgPath));} catch (Exception e) {e.printStackTrace ();} / / OVER} so far, I believe you have a deeper understanding of "how JAVA turns PDF into a picture". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.