Java Multi-image conversion pdf

Source: Internet
Author: User

The company made a small optimization: Because some pictures are uploaded separately (more than 10), and then need to print, you need to provide a one-time direct printing, without having to save each picture in the print (the uploader and the print staff is not the same dial), so you want to be able to convert multiple images in a PDF file.

A tool class is used here:

<dependency>    <groupId>com.itextpdf</groupId>    <artifactid>itextpdf</ Artifactid>    <version>5.5.10</version></dependency>

Overall, it's relatively simple:

 PackageCom.example.zgz.demo;Importcom.itextpdf.text.Document;ImportCom.itextpdf.text.Image;ImportCom.itextpdf.text.Rectangle;ImportCom.itextpdf.text.pdf.PdfWriter;ImportJava.io.File;ImportJava.io.FileOutputStream;/** * @authorZhangguangze *@versionv1.0 * @project: Zgz * @description: This describes the use of classes * @copyright:? 2018 * @company: * @date 2018/5/16 14:23*/ Public classPdfutil {Private StaticString FILEPATH = "f:\\testpdf\\pdf\\"; /**     *     * @paramfileName * Generate PDF file *@paramImagespath * An array of picture paths that need to be converted*/     Public Static voidimagestopdf (String fileName, String imagespath) {Try{fileName= filepath+filename+ ". pdf"; File File=NewFile (fileName); //First step: Create a Document object. Document document =NewDocument (); Document.setmargins (0, 0, 0, 0); //Step Two://Create a PDFWriter instance,Pdfwriter.getinstance (document,Newfileoutputstream (file)); //Step Three: Open the document. Document.open (); //Fourth Step: Add a picture to the document. File files =NewFile (Imagespath); string[] Images=files.list (); intLen =images.length;  for(inti = 0; i < Len; i++)            {                if(Images[i].tolowercase (). EndsWith (". bmp")                        || Images[i].tolowercase (). EndsWith (". jpg")                        || Images[i].tolowercase (). EndsWith (". jpeg")                        || Images[i].tolowercase (). EndsWith (". gif")                        || Images[i].tolowercase (). EndsWith (". png") ) {String temp= Imagespath + "\ \" +Images[i]; Image img=image.getinstance (temp);                    Img.setalignment (Image.align_center); //according to the picture Size setting page, must first set up the page, then NewPage (), otherwise invalidDocument.setpagesize (NewRectangle (Img.getwidth (), Img.getheight ()));                    Document.newpage ();                Document.add (IMG); }            }            //Fifth Step: Close the document. Document.close (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }     Public Static voidMain (string[] args) {String name= "20001543"; String Imagespath= "F:\\testpdf\\image";    Imagestopdf (name, Imagespath); }}

Java Multi-image conversion pdf

Related Article

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.