Fast PDF file Preview with Icepdf

Source: Internet
Author: User

Prior to work, the need to implement an online preview of the PDF function, a start with the Jquery-media plug-in to achieve, and later feel a bit slow, continue to look for better alternatives, until met the ICE PDF ...

Icepdf (official website: HTTP://WWW.ICESOFT.ORG/JAVA/HOME.JSF) The principle is based on swing in Java SE (who says swing is useless ...). ), use a PDF file as a Document object, call the encapsulated method, and generate a picture of each page of the file!

The key code is as follows:

 Public Static voidMain (string[] args) {String FilePath= "Test.pdf"; //Open the fileDocument document =NewDocument (); Try{document.setfile (FilePath); } Catch(pdfexception e) {System.out.println ("Parsing PDF document Failure" +e); } Catch(pdfsecurityexception e) {System.out.println ("Encrytion not supported" +e); } Catch(IOException e) {System.out.println ("IOException" +e); }        //save page captures to file        floatScale = 2.0f; floatrotation =360f; //Paint each pages content to a image and write the image to fileSystem.out.println ("The Number of page:" +document.getnumberofpages ());  for(inti = 0; I < 5; i++) {bufferedimage image=(bufferedimage) document.getpageimage (i, Graphicsrenderinghints.print, page.boundary_trimbox, rotation, scale); Try{System.out.println ("Capturing page:" +i); File File=NewFile ("imagecapture_" + i + ". png"); Imageio.write (Image,"PNG", file); } Catch(IOException e) {e.printstacktrace ();        } image.flush (); }        //Clean up ResourcesDocument.dispose (); }

My jar is Icepdf-core-6.0.0.jar,

First, declare a Document object, call the Setfile (String FilePath) method, the parameter is PDF absolute or equivalent path;

The variable scale represents the scaling ratio, which defaults to 1, and the larger the resulting picture is, the clearer;

Variable rotation is a rotation angle, 360 degrees a period, you can set any value to try;

Then call the Getpageimage () method, generate the picture stream, and then process the picture, you can output to the local, or stream to the page, to achieve a quick preview effect.

Finally, I use this function, in the first preview, generate the corresponding page number picture to local or server, the second preview will directly see the picture, make the user experience better ~

Fast PDF file Preview with Icepdf

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.