Using Itext to generate a PDF with a basemap

Source: Internet
Author: User

 PackageCom.wzh.itext;ImportCom.itextpdf.text.Chunk;Importcom.itextpdf.text.Document;Importcom.itextpdf.text.DocumentException;Importcom.itextpdf.text.Element;ImportCom.itextpdf.text.Font;ImportCom.itextpdf.text.Image;Importcom.itextpdf.text.PageSize;Importcom.itextpdf.text.Paragraph;ImportCom.itextpdf.text.pdf.BaseFont;ImportCom.itextpdf.text.pdf.PdfPCell;Importcom.itextpdf.text.pdf.PdfPTable;ImportCom.itextpdf.text.pdf.PdfWriter;ImportJava.io.ByteArrayOutputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.util.logging.Level;ImportJava.util.logging.Logger;/*** @date 2015-3-17 12:48:24 *@authory * @desc*/ Public classItextutil {/*** Generate a PDF byte stream with a basemap *@paramSWDJZ *@return      */     Public Static byte[] createpdfbytes (Swdjz swdjz) {bytearrayoutputstream BAOs=NewBytearrayoutputstream (); byte[]content =NULL; Document Doc=NewDocument (PageSize.A4.rotate (), 50, 50, 50, 50);//Create document, A4 paper, horizontal screen                 Try{PDFWriter pw=pdfwriter.getinstance (Doc, BAOs);                        Doc.open (); //troubleshoot Chinese not displaying problemsBasefont Bfchinese = Basefont.createfont ("Stsong-light", "Unigb-ucs2-h", basefont.not_embedded); //Defining FontsFont FontChinaTitle14 =NewFont (Bfchinese, 14, Font.Bold); Font FontChina16=NewFont (Bfchinese, 16); Font fontChina14=NewFont (Bfchinese, 14); //load E-Tax registration certificate background ImageImage IMGBG =image.getinstance (Swdjz.getbgpath ()); Imgbg.setabsoluteposition (0, 0);//Set Picture PositionImgbg.scaleabsolute (842, 595);//Set Picture sizeDoc.add (IMGBG); //Blank LineParagraph blank =NewParagraph ("");            Doc.add (blank);            Doc.add (blank);            Doc.add (blank);            Doc.add (blank);                        Doc.add (blank); /*Upper right Corner information*/            //File NumberChunk C01 =NewChunk (Swdjz.getdanh (), fontChina16); Paragraph snoParagraph01=NewParagraph ();            Snoparagraph01.add (C01);            Snoparagraph01.setalignment (Element.align_right); Snoparagraph01.setindentationright (30); Snoparagraph01.setspacingbefore (10);            Doc.add (SNOPARAGRAPH01); //Taxpayer Identification NumberChunk C02 =NewChunk (SWDJZ.GETNSRSBH (), fontChina16); Paragraph snoParagraph02=NewParagraph ();            Snoparagraph02.add (C02);            Snoparagraph02.setalignment (Element.align_right); Snoparagraph02.setindentationright (28);                        Doc.add (SNOPARAGRAPH02);            Doc.add (blank);                        Doc.add (blank); /*Body*/            //Taxpayer nameChunk C03 =NewChunk (SWDJZ.GETNSRMC (), fontChina16); Paragraph snoParagraph03=NewParagraph ();            Snoparagraph03.add (C03); Snoparagraph03.setindentationleft (220);                        Doc.add (SNOPARAGRAPH03);                        Doc.add (blank); //legal representative.Chunk C04 =NewChunk (SWDJZ.GETFDDBR (), fontChina16); Paragraph snoParagraph04=NewParagraph ();            Snoparagraph04.add (C04); Snoparagraph04.setindentationleft (220);                        Doc.add (SNOPARAGRAPH04);                        Doc.add (blank); //AddressChunk C05 =NewChunk (Swdjz.getdz (), fontChina16); Paragraph snoParagraph05=NewParagraph ();            Snoparagraph05.add (C05); Snoparagraph05.setindentationleft (220); Snoparagraph05.setspacingbefore (3);                        Doc.add (SNOPARAGRAPH05);                        Doc.add (blank); //Register Registration TypeChunk C06 =NewChunk (SWDJZ.GETDJZCLX (), fontChina16); Paragraph snoParagraph06=NewParagraph ();            Snoparagraph06.add (C06); Snoparagraph06.setindentationleft (220); Snoparagraph06.setspacingbefore (4);                        Doc.add (SNOPARAGRAPH06);                        Doc.add (blank); //Business ScopeChunk c07 =NewChunk (SWDJZ.GETJYFW (), FONTCHINA14); Paragraph snoParagraph07=NewParagraph ();            Snoparagraph07.add (C07); Snoparagraph07.setindentationleft (220);                        Doc.add (snoParagraph07);                        Doc.add (blank); //approval of the establishment of institutionsChunk C08 =NewChunk (SWDJZ.GETPZSLJG (), fontChina16); Paragraph snoParagraph08=NewParagraph ();            Snoparagraph08.add (C08); if(SWDJZ.GETJYFW (). Length () >37) {Snoparagraph08.setspacingbefore (-10); }Else{Snoparagraph08.setspacingbefore (4); } snoparagraph08.setindentationleft (220);                        Doc.add (snoParagraph08);                        Doc.add (blank); //Withholding obligationsChunk c09 =NewChunk (Swdjz.getkjyw (), fontChina16); Paragraph snoParagraph09=NewParagraph ();            Snoparagraph09.add (C09); if(SWDJZ.GETJYFW (). Length () <38) {Snoparagraph09.setspacingbefore (4); } snoparagraph09.setindentationleft (220);                        Doc.add (snoParagraph09);            Doc.add (blank);            Doc.add (blank);                        Doc.add (blank); /*Lower right corner*/            //date of issuing the certificateChunk cyear =NewChunk ("13", FONTCHINA16); Chunk Cmonth=NewChunk ("Eight", FONTCHINA16); Chunk Cday=NewChunk ("12", FONTCHINA16); Paragraph SnoParagraph10=NewParagraph ();            Snoparagraph10.add (cyear);            Snoparagraph10.add (Cmonth);            Snoparagraph10.add (Cday); Snoparagraph10.setspacingbefore (4); Snoparagraph10.setindentationright (45);            Snoparagraph10.setalignment (Element.align_right);                                    Doc.add (SNOPARAGRAPH10);                        Doc.close (); Content=Baos.tobytearray (); } Catch(FileNotFoundException ex) {Logger.getlogger (itextutil.class. GetName ()). log (Level.severe,NULL, ex); } Catch(Documentexception ex) {Logger.getlogger (itextutil.class. GetName ()). log (Level.severe,NULL, ex); } Catch(IOException ex) {Logger.getlogger (itextutil.class. GetName ()). log (Level.severe,NULL, ex); }finally{            Try{baos.close (); } Catch(IOException ex) {Logger.getlogger (itextutil.class. GetName ()). log (Level.severe,NULL, ex); }        }                returncontent; }    }

In the Servlet:

Response.setcontenttype ("Application/pdf");                 = Response.getoutputstream ();                Response.setheader ("Content-disposition", "Attachment;filename="                        new String (SWDJZ.GETNSRMC (). GetBytes (), "iso-8859-1"));                 byte [] content = itextutil.createpdfbytes (SWDJZ);                 New Bufferedoutputstream (SOS);        Bos.write (content);                Bos.close ();

Using Itext to generate a PDF with a basemap

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.