(Implemented in Java) solutions for converting HTML to JPG, TIFF, and other image formats and combining TIFF images.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.