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:
[Java]
Public void convertToTiff (String pdfFilePath, String character filename)
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.jpg 2tif ("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 ");
}
}
[Java]
/**
* Convert jpg to tif.
* @ Param srcFile the source file to be replaced
* @ Param descFile: the stored file after replacement
* @ 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_assist_ttn2 );
ImageEncoder encoder = ImageCodec. createImageEncoder ("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 );
} Www.2cto.com
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 ();
}
}
Author: JasonChris