Convert a PDF to a picture using Pdfrenderer

Source: Internet
Author: User

One of the problems I've encountered these days is to convert all of the content in a PDF document into a picture. I found a lot of information on the Internet, hair first there are many ways to achieve this, I have done some collation of these materials. I'm here to introduce an implementation that I think is a good idea, using Java. 1) Download a Pdfrenderer.jar package        url:  HTTPS://PDF-RENDERER.DEV.JAVA.NET/2) There are several sample on this website, You can take a look at: https://pdf-renderer.dev.java.net/examples.html 3 The following code is a picture of a PDF generated in Png format   public void pdf_png (int pagenumber) {int pagen= pagenumber; File File = new file ("D:/test.pdf"); Pdffile Pdffile=null; Set up the PDF reading try{randomaccessfile RAF = new Randomaccessfile (file, "R"); FileChannel channel = Raf.getchannel (); Bytebuffer buf = Channel.map (FileChannel.MapMode.READ_ONLY, 0, Channel.size ()); Pdffile = new Pdffile (BUF); catch (Exception e) {} if (Pagen<pdffile.getnumpages ()) return; Print out the number of pages in the PDF document SYSTEM.OUT.PRINTLN (Pdffile.getnumpages ()); Setting the Pagen also generates PNG pictures pdfpage page = Pdffile.getpage (Pagen); Create and configure a Graphics object int width = (int) page.getbbox (). GetWidth (); int height = (int) page.getbbox (). GetHeight (); BufferedImage img = new BufferedImage (width, height, bufferedimage.type_int_argb); graphics2d g2 = Img.creategraphics (); G2.setrenderinghint (renderinghints.key_antialiasing, renderinghints.value_antialias_on); Do the actual drawing pdfrenderer renderer = new Pdfrenderer (page, G2, New Rectangle (0, 0, width, height), null, Color. RED); try{page.waitforfinish ();} catch (Exception e) {e.printstacktrace ();} renderer.run (); G2.dispose (); try{imageio.write (IMG, PNG, New File ("D:/image.png");} catch (Exception ex) {}  in addition to generating PNG pictures, PDF renderer also features &N bsp;      View PDFs in the user's own application.  

Provides a print preview of a PDF file.

Merge the PDF into the 3D scene.

Draw on top of the PDF and can be annotated in the network viewer. Original 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.