ITEXT5 Report __ Table Processing

Source: Internet
Author: User
1. Overview
Tables can be used for relatively simple table handling, but if you are dealing with complex tables, this requires pdfptable processing.
After you have created the table, you can set properties such as the border width, border color, padding (padding space is the spacing between cells), and so on. Here's a simple example of how to use a table with the following code:

2. Operation of the table
2.1 Initialization of the table
You can create pdftable in 3 different ways:
Pdfptable (float[] relativewidths);
pdfptable (int numcolumns);
Pdfptable (pdfptable table);
Example:
Create a table with 3 columns
pdfptable table = new pdfptable (3);

2.2 Width and height of table
There are two ways to set the width of a table, such as
Table.settotalwidth (float totalWidth); Set the total width of the table
Table.settotalwidth (float[] columnWidth); Set the width of each column of a table

Using the two functions above, you must lock the width using the following function.
Table.setlockedwidth (TRUE);

Set the height of a row
Cell.setminimumheight (60);

Code examples
pdfptable table = new pdfptable (3);
Table.settotalwidth (300);
Table.setlockedwidth (TRUE);
Table.settotalwidth (New float[]{144, 72, 72});
Table.setlockedwidth (TRUE);
2.3 Adding cells
Add the following 9 items sequentially to the table, and automatically wrap to the next line when the line is full
pdfptable table = new pdfptable (3);
Table.addcell ("1.1");
Table.addcell ("1.2");
Table.addcell ("1.3");
Table.addcell ("2.1");
Table.addcell ("2.2");
Table.addcell ("2.3");
The results of the above program will show a table of three rows and two columns.

The contents of the added cell can also be in the following ways.
public void Addcell (Pdfpcell cell);
public void Addcell (pdfptable table);
public void Addcell (Phrase Phrase);
public void Addcell (String text);

2.3 Merging cells
Itext the process of merging cells is as follows, first create a cell, set the span of this cell,
In the case of a horizontal merge, the
Cell.setcolspan (n); n represents the number of merged cells starting at the position of the current cell
If it is a vertical merge,
Cell.setrowspan (n);//n represents the number of merged cells, starting at the position of the current cell
Code examples
Create a table with 3 columns
pdfptable table = new pdfptable (3);
Define a table cell
Pdfpcell cell = new Pdfpcell (New Paragraph ("header with colspan 3");
Defines the span of a table cell
Cell.setcolspan (3);
Add the unit to the table
Table.addcell (cell);
The above code creates a table with one row, one row with 3 columns, and the result is merged with only 1 columns.

Code examples
Import Java.io.FileOutputStream;
Import java.io.IOException;

Import com.itextpdf.text.Document;
Import com.itextpdf.text.DocumentException;
Import Com.itextpdf.text.Phrase;
Import Com.itextpdf.text.Rectangle;
Import Com.itextpdf.text.pdf.PdfPCell;
Import com.itextpdf.text.pdf.PdfPTable;
Import Com.itextpdf.text.pdf.PdfWriter;

public class TableDemo2 {

public static final String RESULT = "C:\\tabledemo2.pdf";

public static void CreatePDF (String filename)
Throws IOException, Documentexception {
Step 1
Document document = new document ();
Step 2
Pdfwriter.getinstance (document, new FileOutputStream (filename));
Step 3
Document.open ();
Step 4
pdfptable table = CreateTable1 ();
Document.add (table);
Table = CreateTable2 ();
Table.setspacingbefore (5);
Table.setspacingafter (5);
Document.add (table);
Table = CreateTable3 ();
Document.add (table);
Table = CreateTable4 ();
Table.setspacingbefore (5);
Table.setspacingafter (5);

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.