Step by step ITextSharp Table use

A table is an element that we often use when we create a document. The control of the layout is very precise. The table object in ITextSharp is the following two elements:

PdfTable,PdfCell

Let's take a piece of code from ITextSharp In Action:

 

From the code, you can see that the constructor of PdfTable, passing in a column number as a parameter, indicates how many columns in this table, adding PdfCell to the table, if the number of cells added is more than one row, it will automatically wrap. There is a setColspan function in the cell (note: in the C# version, the property Colspan), used to set a cell across multiple columns. Similarly, if you want to span multiple lines, there is also a property (C#) RolSpan. The above demo code execution results are as follows:

 

The PdfTable object has a function that sets the table area and the width of each column, as follows:

public void SetTotalWidth(float[] columnWidth);
        public void SetWidthPercentage(float[] columnWidth, Rectangle pageSize);

Property: HorizontalAlignment Sets the alignment of the table

HeaderRows represents the first few lines as the table header

FooterRows represents the first few lines as the end of the table

SplitLate indicates whether the cell is displayed across pages

SplitRows indicates whether the line is displayed across pages

 

The sample code is as follows:

   1: public class PdfPTableDemo : TestBase 
   2:     { 
   3:         protected override void Opening(Document document, PdfWriter writer) 
   4:         { 
   5:             document.SetPageSize(PageSize.A4.Rotate()); 
   6:             base.Opening(document, writer); 
   7:         } 
   8:         protected override void WriteDocument(Document document, PdfWriter writer) 
   9:         { 
  10:             PdfPTable table = CreateTable(); 
  11:             //table.WidthPercentage = 80;//Set the width of the table, percentage 
  12:             //table.TotalWidth = 200; / / set the width of the table, the unit points 
  13:             //table.SetTotalWidth(); 
  14:             //table.SetWidthPercentage(); 
  15:             table.HorizontalAlignment = Element.ALIGN_LEFT; 
  16:             document.Add(table);
  17:  
  18:             table = CreateTable(); 
  19:             table.HorizontalAlignment = Element.ALIGN_RIGHT; 
  20:             table.SpacingBefore = (5); 
  21:             table.SpacingAfter = (5); 
  22:             Rectangle rect = new Rectangle(523, 770); 
  23:             table.SetWidthPercentage( 
  24:               new float[] { 50, 25, 25 }, rect); 
  25:             document.Add(table); 
  26:             table = CreateTable(); 
  27:             table.HorizontalAlignment = Element.ALIGN_CENTER; 
  28:             table.SetTotalWidth(new float[] { 144, 72, 72 }); 
  29:             table.LockedWidth = (true); 
  30:             document.Add(table); 
  31:             table = CreateTable(); 
  32:             table.SpacingBefore = (15); 
  33:             table.SpacingAfter = (15); 
  34:             document.Add(table);
  35:  
  36:             table = new PdfPTable(3); 
  37:             PdfPCell cell 
  38:   = new PdfPCell(new Phrase("Head test", Normal)); 
  39:             cell.BackgroundColor = (BaseColor.YELLOW); 
  40:             cell.HorizontalAlignment = (Element.ALIGN_CENTER); 
  41:             cell.Colspan = (7); 
  42:             table.AddCell(cell);
  43:  
  44:             cell 
  45:   = new PdfPCell(new Phrase("Tail test", Normal)); 
  46:             cell.BackgroundColor = (BaseColor.YELLOW); 
  47:             cell.HorizontalAlignment = (Element.ALIGN_CENTER); 
  48:             cell.Colspan = (7); 
  49:             table.AddCell(cell);
  50:  
  51:             table.DefaultCell.BackgroundColor = (BaseColor.LIGHT_GRAY); 
  52:             for (int i = 0; i < 100; i++) 
  53:             { 
  54:                 table.AddCell("Location"); 
  55:                 table.AddCell("Time"); 
  56:                 table.AddCell("Run Length"); 
  57:                 table.AddCell("Title"); 
  58:                 table.AddCell("Year"); 
  59:                 table.AddCell("Directors"); 
  60:                 table.AddCell("Countries"); 
  61:             } 
  62:             table.DefaultCell.BackgroundColor = (null); 
  63:             table.HeaderRows = (2); 
  64:             table.FooterRows = (1); 
  65:             document.Add(table);
  66:  
  67:         }
  68:  
  69:         private PdfPTable CreateTable() 
  70:         { 
  71:             PdfPTable table = new PdfPTable(3); 
  72:             table.TableEvent = new AlternatingBackground(); 
  73:             //table.WidthPercentage = 80;//Set the width of the table, percentage 
  74:             //table.TotalWidth = 200; / / set the width of the table, the unit points 
  75:             //table.SetTotalWidth(); 
  76:             //table.SetWidthPercentage(); 
  77:             PdfPCell cell; 
  78:             cell = new PdfPCell(new Phrase("Cell with colspan 3")); 
  79:             cell.Colspan = (3); 
  80:             table.AddCell(cell); 
  81:             cell = new PdfPCell(new Phrase("Cell with rowspan 2")); 
  82:             cell.Rowspan = (2); 
  83:             table.AddCell(cell); 
  84:             table.AddCell("row 1; cell 1"); 
  85:             table.AddCell("row 1; cell 2"); 
  86:             table.AddCell("row 2; cell 1"); 
  87:             table.AddCell("row 2; cell 2");
  88:  
  89:             return table; 
  90:         } 
  91:     }
  92:  

2011-07-14

Add one more thing: When setting the alignment of cells, you should choose to set the alignment and then add content.

   1: cell = new PdfPCell(new Chunk("Chinese people",Normal));
   2:                 cell.UseAscender = (true);
   3:                 cell.UseDescender = (true);
   4:                 cell.VerticalAlignment = Element.ALIGN_MIDDLE;
   5:                 cell.HorizontalAlignment = Element.ALIGN_CENTER;
   7:                 table.AddCell(cell);

 

Step by step IText.Sharp

Reprinted at: https://www.cnblogs.com/LifelongLearning/archive/2011/05/16/2048116.html

Intelligent Recommendation

Step sqlite3 database table

Enter the command line...

Step 1: Database table

Step 1: Database table In this step, you will create a database table. This program requires only one database table and only supports SQLite. You only need to copy the following code to a file named ...

Step-by-step learning linear table of data structures

First, the mind map Second, the basic concept of linear tables 1. Explanation of terms: **Linear table: ** A finite sequence of n elements with the same data characteristics, both sequential storage a...

Step by step to write data structure (chain table)

From now on, carefully write the data structure step by step, and write the linked list today. The overall difficulty of the linked list is that the key point is the creation of the linked list. The d...

Step by step to write lua interpreter - implementation of table

The table in lua looks like the only data structure in lua. It can be used as an array or as a map. Combining the two into a data structure is enough to illustrate the simplicity and ease of use of lu...

More Recommendation

[Mysql] step by step, learning to modify the data table

Adding single Adding multiple columns Remove Columns Add a primary key constraint Delete the primary key constraint Adding a unique constraint Drop the unique constraints Note: index_name presence her...

Step by step to camp table travel algorithm

Zigui from me: CSDN is inconvenient to insert video, simply recall the process. Recently, I found a game algorithm written by the 6-year-old smart car competition. This is the first time I have writte...

The step of use of the ALLPARIS tool of the orthogonal judgment of the Table method

1. Download the Allpairs file package and decompress it. (I decompress it to the D drive, create a new folder and name it allpairs. The folder is named to take one you like, just be happy ... haha) 2....

How to use step breaks under the code block for step-by-step debugging

1, the establishment of the project, looks like a single file, there is no way to make the Debug column become colored, single file debugging is gray. 2. Breakpoints where Debug is needed 3, constantl...

Step by step to explore how to use TensorFlow efficiently

Summary:Learning TensorFlow, the utilization efficiency is not high enough? Do not understand the mystery inside TensorFlow? See how God can teach you how to use TensorFlow step by step! Tensorflow ba...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top