In the previous article, we talked about the implementation of HTML-to-PDF conversion. In another aspect of that requirement, the project requires page-to-image conversion, mainly JPG, Tiff, PNG format. I was a little confused, but it was never done last time. No suitable tool was found for conversion.
A short time ago, we found that an open-source Apache tool could convert PDF files into images. Without a jar package that directly transforms HTML into images, we could only save the country by curve.
I forgot to introduce it. The package for converting PDF files to images is called Apache product_box. The right side is the official website of product_box.
The following code is directly pasted:
public void convertToTiff(String pdfFilePath, String tiffFileName)throws Exception { PDDocument doc = PDDocument.load(pdfFilePath); int pageCount = doc.getPageCount(); List pages = doc.getDocumentCatalog().getAllPages(); List<File> files = new ArrayList<File>(); List<File> deleteFiles = new ArrayList<File>(); for(int i=0;i<pages.size();i++){ PDPage page = (PDPage)pages.get(i); BufferedImage image = page.convertToImage(BufferedImage.TYPE_BYTE_BINARY, 300); Iterator iter = ImageIO.getImageWritersBySuffix("jpg"); ImageWriter writer = (ImageWriter)iter.next(); File outFile = new File("C:/1"+i+".jpg"); FileOutputStream out = new FileOutputStream(outFile); ImageOutputStream outImage = ImageIO.createImageOutputStream(out); writer.setOutput(outImage); writer.write(new IIOImage(image,null,null)); this.jpg2tif("C:/1"+i+".jpg", "C:/1"+i+".tif"); files.add(new File("C:/1"+i+".tif")); deleteFiles.add(outFile); deleteFiles.add(new File("C:/1"+i+".tif")); outImage.close(); out.close(); writer.dispose();} if(files != null){ this.tif2Marge(files, "C:/1.tif"); }}
/*** Convert JPG format to Tif format. * @ Param srcfile the source file to be replaced * @ Param descfile the transfer file after the change * @ throws exception */Public void jpg2tif (string srcfile, string descfile) throws exception {renderedop src = Jai. create ("fileload", srcfile); outputstream OS = new fileoutputstream (descfile); incluencodeparam Param = new incluencodeparam (); Param. setcompression (incluencodeparam. compression_pai_ttn2); imageencoder encoder = imagecodec. createimageenco Der ("tiff", OS, Param); encoder. encode (SRC); OS. close ();} /*** contract several TIF files into one tif file * @ Param srcfile * @ Param descfile * @ throws exception */Public void tif2marge (list <File> srcfile, string descfile) throws exception {list pages = new arraylist (srcfile. size ()-1); For (INT I = 0; I <srcfile. size (); I ++) {renderedop firstpage = Jai. create ("fileload", srcfile. get (0 ). getCanonicalPath (); if (I! = 0) {renderedop page = Jai. create ("fileload", srcfile. get (I ). getCanonicalPath (); pages. add (PAGE) ;}outputstream OS = new fileoutputstream (descfile); incluencodeparam Param = new incluencodeparam (); Param. setcompression (incluencodeparam. compression_assist_ttn2); Param. setextraimages (pages. iterator (); imageencoder encoder = imagecodec. createimageencoder ("tiff", OS, Param); encoder. encode (firstpage); firstpage. dispose (); For (Int J = 1; j <pages. size (); j ++) {(renderedop) pages. get (j )). dispose ();} OS. close (); pages. clear ();}}