Java converts PDF to image

Source: Internet
Author: User

The PDFBox component version 1.8.4, which uses Apache, was first used

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 {Str   
	 	
        ing 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 is the question:

Report parse exception directly when PDF document is 180M size

Processing is very slow when PDF pages are more than 500 pages

A subsequent attempt to use the Pdf-renderer 1.0.5 version

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.getp
		Roperty ("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 picture quality Encoder.setjpegencodeparam (param2); Encoder.encode (tag);
			JPEG encoding out.close ();
		System.out.println ("Image in the page-->" + (i+1));
 }
	}
}
But the problem is the problem: when the PDF version is not 1.4, a direct error: expected ' XRef ' at start of table


PDFBox compared with pdfrenderer, the conversion efficiency is much lower. The 200-page PDF takes about 6 times times as much time as the latter. At the same time, there are some problems with support for Chinese fonts.

However, there is no question that the PDF version is different from being converted.

Pdfrenderer cannot convert more than 1.4, found the solution but didn't find it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.