[JAVA100 example]039, PDF file generation

Source: Internet
Author: User
Tags 0xc0 mail zip

Import com.lowagie.text.*;


import com.lowagie.text.pdf.*;


import java.io.*;


import java.util.*;


import Java.awt.Color;


/**


* <p>title: Generate PDF file </p>


* <p>description: This example generates a PDF file of a table by using the Itext package </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:myPDF.java</p>


* @version 1.0


*/


public class mypdf{


/**


*<br> Method Description: Write PDF file


*<br> input Parameters:


*<br> return type:


*/


public void Write () {


try{


Document Document=new document (PAGESIZE.A4, 50, 50, 100, 50);


Rectangle pagerect=document.getpagesize ();


pdfwriter.getinstance (document, New FileOutputStream ("Tables.pdf"));


Create Kanji Font


Basefont Bfsong = Basefont.createfont ("Stsong-light", "Unigb-ucs2-h", false);


font fontsong = new Font (Bfsong, font.normal);


//Add a watermark


try {


watermark watermark = new Watermark (image.getinstance ("Test.jpg"), Pagerect.left () +50,pagerect.top ()-85);


watermark.scalepercent (50);


Document.add (watermark);


}catch (Exception e) {


System.err.println ("Please check the file" Test.jpg "in the correct position?");


   }


 


//Add header information to the page


HeaderFooter Header = new HeaderFooter (New Phrase ("Java instance 100", Fontsong), false);


Header.setborder (2);


header.setalignment (element.align_right);


Document.setheader (header);


 


//Add footer information to the page


HeaderFooter footer = new HeaderFooter (New Phrase ("First", Fontsong), New Phrase ("page", Fontsong));


footer.setalignment (Element.align_center);


Footer.setborder (1);


document.setfooter (footer);


//Open Document


Document.open ();


//Construction table


table = new Table (4);


table.setdefaultverticalalignment (Element.align_middle);


Table.setborder (Rectangle.no_border);


int hws[] = {10, 20, 10, 20,};


table.setwidths (HWS);


table.setwidth (100);


//table header information


cell cellmain = new cell (New Phrase ("User Information", New Font (Bfsong, Font.bold,new Color (0,0,255)));


cellmain.sethorizontalalignment (Element.align_center);


Cellmain.setcolspan (4);


Cellmain.setborder (Rectangle.no_border);


Cellmain.setbackgroundcolor (New Color (0xc0, 0xc0, 0xc0));


Table.addcell (Cellmain);


//table header information


cell cellleft= New cell (New Phrase ("Consignee info", New Font (Bfsong, Font.italic,new Color (0,0,255)));


Cellleft.setcolspan (2);


cellleft.sethorizontalalignment (Element.align_center);


Table.addcell (Cellleft);


cell cellright= New cell (New Phrase ("Order person information", New Font (Bfsong, Font.italic,new Color (0,0,255)));


Cellright.setcolspan (2);


cellright.sethorizontalalignment (Element.align_center);


Table.addcell (cellright);


 


//Receiving and ordering person information, table body content


Table.addcell (New Phrase ("name", Fontsong));


Table.addcell (New Phrase ("John", Fontsong));


Table.addcell (New Phrase ("name", Fontsong));


Table.addcell (New Phrase ("Dick", Fontsong));


Table.addcell (New Phrase ("Telephone", Fontsong));


Table.addcell (New Phrase ("23456789", Fontsong));


Table.addcell (New Phrase ("Telephone", Fontsong));


Table.addcell (New Phrase ("9876543", Fontsong));


Table.addcell (New Phrase ("Zip code", Fontsong));


Table.addcell (New Phrase ("100002", Fontsong));


Table.addcell (New Phrase ("Zip code", Fontsong));


Table.addcell (New Phrase ("200001", Fontsong));


Table.addcell (New Phrase ("Address", Fontsong));


Table.addcell (New Phrase ("Xicheng area xx road xx", Fontsong));


Table.addcell (New Phrase ("Address", Fontsong));


Table.addcell ("Shanghai Lujiazui District XX Road xx", Fontsong));


Table.addcell (New Phrase ("E-mail", Fontsong));


Table.addcell (New Phrase ("zh_san@hotmail.com", Fontsong));


Table.addcell (New Phrase ("E-mail", Fontsong));


Table.addcell (New Phrase ("li_si@hotmail.com", Fontsong));


//Add a table to the text


document.add (table);


//Close text, releasing resources


Document.close ();





}catch (Exception e) {


System.out.println (e);


  }


 }


/**


*<br> Method Description: Main method


*<br> input Parameters:


*<br> return type:


*/


public static void Main (string[] arg) {


mypdf p = new Mypdf ();


P.write ();


 }


}

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.