PDF Transform JPG

 

 

    I've been complaining for a long time, there is a problem with iteye's editor today, I can't do it right, I can't insert the code, every time I insert it, it is displayed to the top, so annoying

  

    I don't have the time to write a blog, let's just look at the code:

   

   1. Import related jars

 

<!-- PDF to image-->
		<dependency>
		    <groupId>com.sun.pdfview</groupId>
		    <artifactId>pdfrenderer</artifactId>
		    <version>0.9.1-patched</version>
		</dependency>

 

 2. Run the code directly

 

private static void PdfToJpg() throws Exception {
        
        File file = new File("D:\\upload\\xxx.pdf"); // PDF路径
        
        String getPdfFilePath = "D:\\upload\\img"; // Generate image path
        
        
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        
        FileChannel channel = raf.getChannel();  
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());  
        PDFFile pdffile = new PDFFile(buf);  
 
        System.out.println("页数: " + pdffile.getNumPages());  
       
        int pageNum = pdffile.getNumPages();
        
        for (int i = 1; i <= pageNum; i++) {
            
            // draw the first page to an image  
            PDFPage page = pdffile.getPage(i);  
            
            int width = (int) page.getBBox().getWidth();
            int height = (int) page.getBBox().getHeight();
            
            System.out.println ("width:" + width);
            System.out.println ("height:" + height);
            
            // get the width and height for the doc at the default zoom  
            Rectangle rect = new Rectangle(0, 0, width, height);  
 
            int n = 2; //Image clarity (n>0 and n<7) pdf zoom parameter
            
            // Enlarge the pdf to n times and create a picture
            int imgW = width * n;
            int imgH = height * n;
            
            // generate the image  
            Image img = page.getImage(imgW, imgH, // width &  
                                                                // height  
                    rect, // clip rect  
                    null, // null for the ImageObserver  
                    true, // fill background with white  
                    true // block until drawing is done  
                    );  
 
            BufferedImage tag = new BufferedImage(imgW, imgH,BufferedImage.TYPE_INT_RGB);  
            tag.getGraphics().drawImage(img, 0, 0, imgW, imgH,null);
           
            FileOutputStream out = new FileOutputStream(getPdfFilePath + File.separator + i + ".jpg"); // output to file stream
            System.out.println("Successfully saved the picture to: " +getPdfFilePath+ File.separator + i + ".jpg");
           
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            // set image quality
            /*JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam (tag);
            param.setQuality (1f, true); //1f~0.01f is to improve the quality of the generated image
            encoder.setJPEGEncodeParam (param);*/
            
            encoder.encode(tag); // JPEG encoding  
 
            out.close();  
        }  
 
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326524341&siteId=291194637