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