java pdf turn pictures

<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.19</version>
        </dependency>

        <dependency>
            <groupId>com.twelvemonkeys.imageio</groupId>
            <artifactId>imageio-jpeg</artifactId>
            <version>3.4.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>jbig2-imageio</artifactId>
            <version>3.0.2</version>
        </dependency>

        <dependency>
            <groupId>com.github.jai-imageio</groupId>
            <artifactId>jai-imageio-core</artifactId>
            <version>1.4.0</version>
        </dependency>

        <dependency>
            <groupId>com.github.jai-imageio</groupId>
            <artifactId>jai-imageio-jpeg2000</artifactId>
            <version>1.3.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.16</version>
        </dependency>
Dependencies
public static List<String> pdfToImage(String pdfPath,int size) {
        List<String> list = new ArrayList<>();
        String imagePath;
        try {
            FileName String = pdfPath.substring (0, pdfPath.lastIndexOf ( "." ));
             // image combining parameters
             // total width 
            int width = 0 ;
             // save the image in an RGB data 
            int [] singleImgRGB;
             int = 0 shiftHeight ;
             // save each image pixel values of 
            the BufferedImage ImageResult = null ;
             // use PdfBox generates image 
            PDDocument pdDocument = PDDocument.load ( new new File (pdfPath));
            The renderer PDFRenderer = new new PDFRenderer (pdDocument);
             // cycle for each page 
            for ( int I = 0, len = pdDocument.getNumberOfPages (); I <len; I ++ ) {
                 IF (I == size) {
                     BREAK ;
                }
                Image the BufferedImage = renderer.renderImageWithDPI (I, 105 , ImageType.RGB);
                 int imageHeight = image.getHeight ();
                 int imageWidth = image.getWidth ();
                 // calculate the height and offset
                 // use the first image width ; 
                width = imageWidth;
                 // save each page picture pixel value 
                ImageResult = new new BufferedImage (width, imageHeight, BufferedImage.TYPE_INT_RGB);
                 // there is a high degree can be imageHeight * len, I extract a value here so no 
                singleImgRGB image.getRGB = (0, 0, width, imageHeight, null, 0, width);
                // 写入流中
                imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
                File f = new File(fileName);
                if (!f.exists()) {
                    f.mkdir();
                }
                imagePath = fileName + "/image" + i+1 + ".png";
                File file = new File(imagePath);
                if (!file.exists()) {
                    file.mkdir();
                }
                // 写图片
                ImageIO.write(imageResult, "png", new File(imagePath));
                list.add(imagePath);
            }
            pdDocument.close();

        } catch (Exception e) {

            e.printStackTrace ();
        }
        return list;
        //OVER
    }
method

 

 

Guess you like

Origin www.cnblogs.com/changeEveryDay/p/12375804.html