Printed series -- Java itext dynamic generation of PDF document tables

Source: Internet
Author: User

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

  1. /**
  2. * Helloworld. Java
  3. */
  4. Import java. Io. fileoutputstream;
  5. Import java. Io. ioexception;
  6. Import com. lowagie. Text .*;
  7. Import com.lowagie.text=. writable writer;
  8. Public class helloworld {
  9. Public static void main (string [] ARGs ){
  10. System. Out. println ("Hello World ");
  11. // Create a Document Object
  12. Document document = new document ();
  13. Try
  14. {
  15. // Generate a document named helloworldworkflow
  16. Using writer. getinstance (document, new fileoutputstream ("helloworldworkflow "));
  17. // Add some information about the PDF document
  18. Document. addtitle ("Hello world example ");
  19. Document. addauthor ("Bruno lowagie ");
  20. Document. addsubject ("this example explains how to add metadata .");
  21. Document. addkeywords ("itext, hello World, step 3, metadata ");
  22. Document. addcreator ("My program using itext ");
  23. // Open the document and write the content
  24. Document. open ();
  25. // Insert a paragraph
  26. Document. Add (new paragraph ("Hello world! "));
  27. }
  28. Catch (incluentexception de)
  29. {
  30. System. Err. println (De. getmessage ());
  31. }
  32. Catch (ioexception IOE)
  33. {
  34. System. Err. println (IOE. getmessage ());
  35. }
  36. // Close the opened document
  37. Document. Close ();
  38. }
  39. }

After compilation and operation, we can find the generated helloworldworkflow in the running directory, and open it to see the text we wrote:

Hello World

Iii. Chinese problems:


Because itext does not support the East Asian language, we downloadedItextasian. JarLater, you can write Chinese Characters in PDF:

  1. /**
  2. * Asiantest. Java
  3. */
  4. Import java. Io. fileoutputstream;
  5. Import java. Io. ioexception;
  6. Import com. lowagie. Text .*;
  7. Import com.lowagie.text=. writable writer;
  8. Import com.lowagie.text=. basefont;
  9. Import com. lowagie. Text. Font;
  10. Import java. AWT. color;
  11. Public class asiantest {
  12. Public static void main (string [] ARGs ){
  13. // Create a Document Object
  14. Document document = new document ();
  15. Try
  16. {
  17. // Generate a document named asiantest.pdf
  18. Using writer. getinstance (document, new fileoutputstream ("asiantest.pdf "));
  19. /** Create a new font, itext Method
  20. * Stsongstd-light is a font and is suffixed with property in itextasian. jar.
  21. * The UniGB-UCS2-H is encoded and suffixed with cmap in itextasian. Jar
  22. * H indicates that the text layout is horizontal and the corresponding V indicates the vertical layout.
  23. */
  24. Basefont bfchinese = basefont. createfont ("stsongstd-light", "UniGB-UCS2-H", false );
  25. Font fontchinese = new font (bfchinese, 12, Font. Normal, color. Green );
  26. // Open the document and write the content
  27. Document. open ();
  28. // Insert a paragraph
  29. Paragraph par = new paragraph ("we", fontchinese );
  30. Document. Add (PAR );
  31. }
  32. Catch (incluentexception de)
  33. {
  34. System. Err. println (De. getmessage ());
  35. }
  36. Catch (ioexception IOE)
  37. {
  38. System. Err. println (IOE. getmessage ());
  39. }
  40. // Close the opened document
  41. Document. Close ();
  42. }
  43. }

Then you can display Chinese characters.

4. Other problems: (the corresponding package should be imported)

1.
Form feed:

  1. Document. Newpage ();


2. Table:

  1. // Set the table
  2. Table atable = new Table (3 );
  3. Int width [] = {25, 25, 50 };
  4. Atable. setwidths (width );
  5. Atable. setwidth (80); // 80% of the page width
  6. Atable. setdefaulthorizontalalignment (element. align_left );
  7. Atable. setdefaververticalalignment (element. align_middle );
  8. Atable. setautofillemptycells (true); // automatically fills up
  9. Atable. setpadding (1 );
  10. Atable. setspacing (1 );
  11. Atable. setdefaultcellborder (0 );
  12. Atable. setborder (0 );
  13. Cell cell = new cell (new phrase ("this is a test of 3*3 table data", fontchinese ));
  14. Cell. setverticalalignment (element. align_top );
  15. Cell. setrowspan (3 );
  16. Atable. addcell (cell );
  17. Atable. addcell (New Cell ("#1 "));
  18. Atable. addcell (New Cell ("#2 "));
  19. Atable. addcell (New Cell ("#3 "));
  20. Atable. addcell (New Cell ("#4 "));
  21. Atable. addcell (New Cell ("#5 "));
  22. Atable. addcell (New Cell ("#6 "));
  23. Document. Add (atable );


3. Image:

  1. // It can be an absolute path or a URL
  2. Image IMG = image. getinstance ("logo.gif ");
  3. // Image = image. getinstance (new URL (http://xxx.com/logo.jpg ));
  4. IMG. setabsoluteposition (0, 0 );
  5. Document. Add (IMG );

V. References:

  • Itext
    Http://www.lowagie.com/iText/
  • Itext
    API:
    Http://itext.sourceforge.net/docs/

    Http://www.sentom.net/list.asp? Id = 42

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.