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>
* @author Dujiang
* @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 a Chinese character 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 see if the file" Test.jpg "is in the correct place?);
}
Add header information to a 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 a 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 ();
Structuring tables
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);
Sub-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 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);
Receipt and order person information, table body contents
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 ("postcode", Fontsong));
Table.addcell (New Phrase ("100002", Fontsong));
Table.addcell (New Phrase ("postcode", Fontsong));
Table.addcell (New Phrase ("200001", Fontsong));
Table.addcell (New Phrase ("Address", Fontsong));
Table.addcell (New Phrase ("Xicheng xx road xx", Fontsong));
Table.addcell (New Phrase ("Address", Fontsong));
Table.addcell (New Phrase ("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 table to text
document.add (table);
//Closing text, releasing resources
document.close ();
}catch (Exception e) {
System.out.println ( e);
}
}
/**
*<br> Method Description: Main method
*<br> input parameter:
& nbsp;*<br> return type:
*/
public static void main (string[] arg) {
mypdf p = new Mypdf ();
p.write ();
}
}