Java itext dynamic PDF generation
Document Table Chinese problems
Itext is a development source code project. You can use itext to conveniently implement PDF output.
1. Download itext:
You can view information about itext in the http://www.lowagie.com/iText/, including source code, documentation ..
1.
Itext-src-1.4.zip (source code)
Http://jaist.dl.sourceforge.net/sourceforge/itext/itext-src-1.4.zip
2. itext-1.4.jar (jar files that can be imported directly)
Http://jaist.dl.sourceforge.net/sourceforge/itext/itext-1.4.jar
3. Asian Language Pack
Http://itextdocs.lowagie.com/downloads/iTextAsian.jar
(Or)
Http://itextdocs.lowagie.com/downloads/iTextAsianCmaps.jar
Ii. Example program:
First, add the complete paths of these jar packages to the environment variables.
Classpath, and then the following program
Import the corresponding package
- /**
- * Helloworld. Java
- */
- Import java. Io. fileoutputstream;
- Import java. Io. ioexception;
- Import com. lowagie. Text .*;
- Import com.lowagie.text=. writable writer;
- Public class helloworld {
- Public static void main (string [] ARGs ){
- System. Out. println ("Hello World ");
- // Create a Document Object
- Document document = new document ();
- Try
- {
- // Generate a document named helloworldworkflow
- Using writer. getinstance (document, new fileoutputstream ("helloworldworkflow "));
- // Add some information about the PDF document
- Document. addtitle ("Hello world example ");
- Document. addauthor ("Bruno lowagie ");
- Document. addsubject ("this example explains how to add metadata .");
- Document. addkeywords ("itext, hello World, step 3, metadata ");
- Document. addcreator ("My program using itext ");
- // Open the document and write the content
- Document. open ();
- // Insert a paragraph
- Document. Add (new paragraph ("Hello world! "));
- }
- Catch (incluentexception de)
- {
- System. Err. println (De. getmessage ());
- }
- Catch (ioexception IOE)
- {
- System. Err. println (IOE. getmessage ());
- }
- // Close the opened document
- Document. Close ();
- }
- }
After compilation and operation, we can find the generated helloworldworkflow in the running directory, and open it to see the text we wrote:
Iii. Chinese problems:
Because itext does not support the East Asian language, we downloadedItextasian. JarLater, you can write Chinese Characters in PDF:
- /**
- * Asiantest. Java
- */
- Import java. Io. fileoutputstream;
- Import java. Io. ioexception;
- Import com. lowagie. Text .*;
- Import com.lowagie.text=. writable writer;
- Import com.lowagie.text=. basefont;
- Import com. lowagie. Text. Font;
- Import java. AWT. color;
- Public class asiantest {
- Public static void main (string [] ARGs ){
- // Create a Document Object
- Document document = new document ();
- Try
- {
- // Generate a document named asiantest.pdf
- Using writer. getinstance (document, new fileoutputstream ("asiantest.pdf "));
- /** Create a new font, itext Method
- * Stsongstd-light is a font and is suffixed with property in itextasian. jar.
- * The UniGB-UCS2-H is encoded and suffixed with cmap in itextasian. Jar
- * H indicates that the text layout is horizontal and the corresponding V indicates the vertical layout.
- */
- Basefont bfchinese = basefont. createfont ("stsongstd-light", "UniGB-UCS2-H", false );
- Font fontchinese = new font (bfchinese, 12, Font. Normal, color. Green );
- // Open the document and write the content
- Document. open ();
- // Insert a paragraph
- Paragraph par = new paragraph ("we", fontchinese );
- Document. Add (PAR );
- }
- Catch (incluentexception de)
- {
- System. Err. println (De. getmessage ());
- }
- Catch (ioexception IOE)
- {
- System. Err. println (IOE. getmessage ());
- }
- // Close the opened document
- Document. Close ();
- }
- }
Then you can display Chinese characters.
4. Other problems: (the corresponding package should be imported)
1.
Form feed:
- Document. Newpage ();
2. Table:
- // Set the table
- Table atable = new Table (3 );
- Int width [] = {25, 25, 50 };
- Atable. setwidths (width );
- Atable. setwidth (80); // 80% of the page width
- Atable. setdefaulthorizontalalignment (element. align_left );
- Atable. setdefaververticalalignment (element. align_middle );
- Atable. setautofillemptycells (true); // automatically fills up
- Atable. setpadding (1 );
- Atable. setspacing (1 );
- Atable. setdefaultcellborder (0 );
- Atable. setborder (0 );
- Cell cell = new cell (new phrase ("this is a test of 3*3 table data", fontchinese ));
- Cell. setverticalalignment (element. align_top );
- Cell. setrowspan (3 );
- Atable. addcell (cell );
- Atable. addcell (New Cell ("#1 "));
- Atable. addcell (New Cell ("#2 "));
- Atable. addcell (New Cell ("#3 "));
- Atable. addcell (New Cell ("#4 "));
- Atable. addcell (New Cell ("#5 "));
- Atable. addcell (New Cell ("#6 "));
- Document. Add (atable );
3. Image:
- // It can be an absolute path or a URL
- Image IMG = image. getinstance ("logo.gif ");
- // Image = image. getinstance (new URL (http://xxx.com/logo.jpg ));
- IMG. setabsoluteposition (0, 0 );
- Document. Add (IMG );
V. References: