Java Implementation of PDF file screenshot method [attached PDFRenderer. jar download], export PDFRenderer. jar

Source: Internet
Author: User

Java PDF file implementation method [attached PDFRenderer. jar download], export PDFRenderer. jar

This example describes how to implement PDF files in java. We will share this with you for your reference. The details are as follows:

In a recent website, you need to upload a pdf file, display a pdf page, and click the page to read it online. Here PDFRender is used for pdf.

Public static boolean createScreenShoot (String source, String target) {File file = new File (source); if (! File. exists () {System. err. println ("path [" + source + "] the corresponding PDF file does not exist! "); Return false;} try {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); int num = pdffile. getNumPages (); for (int I = 1; I <num; I ++) {PDFPage page = pdffile. getPage (1); // get the width and height for the doc at the default zoom Rectangle rect = new Rectangle (0, 0, (int) page. getBBox (). getWidth (), (int) page. getBBox (). getHeight (); // generate the image Image img = page. getImage (rect. width, rect. height, // width & rect, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done ); 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 (target + I + "jpg"); required imageencoder encoder = required codec. createJPEGEncoder (out); encoder. encode (tag); // JPEG encoded out. close ();} return true;} catch (Exception e) {e. printStackTrace (); return true ;}

In addition, if you need to display pdf files online, you need to set a response header.

response.setContentType("application/pdf");

Appendix:PdfRender. jar click hereDownload from this site.

For more java-related content, refer to this topic: java image operation skills summary, java Date and Time Operation Skills summary, Java operation DOM node skills summary, Java file and directory operation skills summary, and Java data structure and algorithm tutorial.

I hope this article will help you with java programming.

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.