Use pdfrenderer to convert a PDF file to an image

Source: Internet
Author: User

One problem encountered over the past few days is to convert all the content in the PDF document into images.

I found a lot of information on the Internet and found many ways to achieve this first. I also sorted out these materials. I will introduce an implementation method that I think is good at using Java. 1) download a pdfrenderer. jar package URL: https://pdf-renderer.dev.java.net/2) on this official website there are a few samples, you can take a look: https://pdf-renderer.dev.java.net/examples.html3) the following code is to generate a pdf png Format Image Public void pai_png (INT pagenumber) {<br/> int pagen = pagenumber; <br/> file = new file ("D:/test.pdf "); </P> <p> pdffile = NULL; <br/> // set up the PDF reading <br/> try {<br/> randomaccessfile RAF = new randomaccessfile (file, "R "); <br/> filechannel channel = Raf. getchannel (); <br/> bytebuffer Buf = channel. map (filechannel. mapmode. read_only, 0, channel. size (); <br/> pdffile = new pdffile (BUF); <br/>}< br/> catch (exception E) {}</P> <p> If (pagen <pdffile. getnumpages () <br/> return; <br/> // print the number of pages of the PDF document <br/> system. out. println (pdffile. getnumpages (); </P> <p> // you can specify pagen to generate a PNG image. <br/> pdfpage page = pdffile. getpage (pagen); <br/> // create and configure a graphics object <br/> int width = (INT) page. getbbox (). getwidth (); <br/> int Height = (INT) page. getbbox (). getheight (); <br/> bufferedimage IMG = new bufferedimage (width, height, bufferedimage. type_int_argb); <br/> graphics2d g2 = IMG. creategraphics (); <br/> g2.setrenderinghint (renderinghints. key_antialiasing, renderinghints. value_antialias_on); <br/> // do the actual drawing <br/> pdfrenderer Renderer = new pdfrenderer (page, G2, <br/> New rectangle (0, 0, width, height), null, color. red); <br/> try {<br/> page. waitforfinish (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> Renderer. run (); <br/> g2.dispose (); </P> <p> try {<br/> ImageIO. write (IMG, "PNG", new file ("D:/image.png"); <br/>}< br/> catch (exception ex ){}In addition to generating PNG images, the PDF Renderer also has a function to view PDF in your own applications.

Print and preview PDF files.

Merge PDF files into 3D scenarios.

Drawing on a PDF file and tagging on a network viewer.

Address: http://baiyun11095.blog.163.com/blog/static/33798962201032415853412/

 

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.