Overview
Convert PDFs to a single TIFF file based on Java.
Maven Dependency
<groupId>Com.sun.media</groupId> <Artifactid>Jai_codec</Artifactid> <version>1.1-mr</version></Dependency><Dependency> <groupId>Javax.media</groupId> <Artifactid>Jai_core</Artifactid> <version>1.1-mr</version></Dependency><Dependency> <groupId>Com.sun.medialib</groupId> <Artifactid>Mlibwrapper_jar</Artifactid> <version>1.1</version></Dependency><Dependency> <groupId>Org.apache.pdfbox</groupId> <Artifactid>PDFBox</Artifactid> <version>2.0.0-rc2</version></Dependency>
Partial dependencies are not found in the MAVEN central repository and are now available in a unified compressed file: dependent on download.
Implementing a class Diagram
Pdf2tiffconstant
Img_format
The default picture format
Dpi
The default conversion precision
Pdf2tiffutil
public static Pdf2tiff (Is:inputstream, os:outputstream): void
Converts the specified PDF to a single TIFF file and writes to the specified output stream. parameter is provides the contents of the PDF document, the parameter OS specifies the output stream.
Code pdf2tiffconstant
Package Cn.ljl.javaweb.demo.pdf2tiff; Public class pdf2tiffconstant { /***/public static Final String Img_format = "tiff"; /** * /publicstaticfinalint// pixel of picture }
Pdf2tiffutil
PackageCn.ljl.javaweb.demo.pdf2tiff;Import StaticCn.ljl.javaweb.demo.pdf2tiff.Pdf2TiffConstant.DPI;Import StaticCn.ljl.javaweb.demo.pdf2tiff.Pdf2TiffConstant.IMG_FORMAT;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.util.ArrayList;Importjava.util.List;ImportJavax.media.jai.JAI;ImportJavax.media.jai.PlanarImage;Importorg.apache.pdfbox.pdmodel.PDDocument;ImportOrg.apache.pdfbox.rendering.ImageType;ImportOrg.apache.pdfbox.rendering.PDFRenderer;ImportCom.sun.media.jai.codec.ImageCodec;ImportCom.sun.media.jai.codec.ImageEncoder;ImportCom.sun.media.jai.codec.TIFFEncodeParam; Public classPdf2tiffutil {/*** Read pdf from input stream, convert to TIFF and write output stream .<br/> * Reference list: * <ol> * <li><a href= * "Http://www.coderanch.com/t/497492/java/java/Convert-PDF-files-Tiff-files* >convert PDF files to Tiff files</a></li> * <li><a href= * "http://www.oracle.com/technetwork/cn/java/javaee/downloads/readme-1-1-2-137176.html* >java (TM) Advanced Imaging API readme</a></li> * </ol> * *@paramis * input stream, providing PFG content. * @paramOS * output stream. */ Public Static voidPdf2tiff (InputStream is, OutputStream os) {pddocument doc=NULL; Try{doc=Pddocument.load (IS); intPageCount =doc.getnumberofpages (); Pdfrenderer renderer=NewPdfrenderer (DOC);//Create a PDF renderer based on the PDDocument objectList<PlanarImage> pilist =NewArraylist<planarimage> (pageCount-1); for(inti = 0 + 1; i < PageCount; i++) {bufferedimage image=renderer.renderimagewithdpi (i, DPI, Imagetype.rgb); Planarimage pimg= Jai.create ("Mosaic", image); Pilist.add (PIMG); } tiffencodeparam param=NewTiffencodeparam ();//To create a TIFF encoding parameter classParam.setcompression (tiffencodeparam.compression_deflate);//Compression ParametersParam.setextraimages (Pilist.iterator ());//to set an iterator for a pictureBufferedImage fimg= renderer.renderimagewithdpi (0, DPI, Imagetype.rgb); Planarimage FPI= Jai.create ("Mosaic", fimg);//instantiate a Jai Picture object by using the Jai Create () methodImageencoder Enc=Imagecodec.createimageencoder (img_format, OS, param); Enc.encode (FPI);//Specifies the first Jai picture object to encode, and writes the output to a}Catch(IOException e) {e.printstacktrace (); } finally { Try { if(Doc! =NULL) Doc.close (); } Catch(IOException e) {e.printstacktrace (); } } }}
Reference index
- Convert PDF files to Tiff files
- Java (TM) Advanced Imaging API README
PDF to TIFF