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.