Manipulating Pdf–itextsharp in ASP.-Working with Tables

Source: Internet
Author: User

The most common elements used to generate PDFs using ASP should be tables, which can help make documents such as orders or invoice types more formatted and aesthetically pleasing. This article does not delve into the table, just provides a way to create a table using Itextsharp

Using Itextsharp to manipulate tables is a simple matter, especially in itextsharp, where table elements are named and HTML is very similar to CSS. Itextsharp provides a number of classes for creating tables, so I use pdfptable, a class that specifically creates tables in PDFs, that shows how to create a table and add it to a PDF:

pdfptable table = new pdfptable (3); Pdfpcell cell = new Pdfpcell (New Phrase ("Header spanning 3 columns"); Cell. Colspan = 3; Cell. HorizontalAlignment = 1; 0=left, 1=centre, 2=right table. Addcell (cell); Table. Addcell ("Col 1 Row 1"); Table. Addcell ("Col 2 Row 1"); Table. Addcell ("Col 3 Row 1"); Table. Addcell ("Col 1 Row 2"); Table. Addcell ("Col 2 Row 2"); Table. Addcell ("Col 3 Row 2"); Doc. ADD (table);

By passing the integer 3 to the Pdfptable constructor, Pdfptable is initialized to a three-column table. There are several ways to add cells to pdfptabled, the first cell is added by Pdfpcell object, the Pdfpcell constructor takes a phrase object as a parameter, and then sets the colspan of the cell to 3 So that this cell occupies the entire row. Like in HTML, the horizontal alignment of a cell uses one of three values (translator: Left, center, right), and the three values I added to the comment. I've added the following cells through the Addcell method, and the final document works as follows:

The following code extracts the values from the database and inserts the data into the Itextsharp-generated table, and the following code sets up how some tables are presented:

pdfptable table = new pdfptable (2); Actual width of table in points table. TotalWidth = 216f; Fix the absolute width of the table table.   Lockedwidth = true; Relative col widths in PROPORTIONS-1/3 and 2/3 float[] widths = new float[] {1f, 2f}; Table. Setwidths (widths); Table. HorizontalAlignment = 0; Leave a gap before and after the table table. Spacingbefore = 20f; Table.   Spacingafter = 30f; Pdfpcell cell = new Pdfpcell (new Phrase ("Products")); Cell. Colspan = 2; Cell. Border = 0; Cell. HorizontalAlignment = 1; Table. Addcell (cell); String connect = "Server=.\\sqlexpress;database=northwind; Trusted_connection=true; ";   using (SqlConnection conn = new SqlConnection (connect)) {string query = "Select ProductID, ProductName from Products";   SqlCommand cmd = new SqlCommand (query, conn); try {Conn.     Open (); using (SqlDataReader rdr = cmd. ExecuteReader ()) {while (rdr. Read ()) {table. Addcell (Rdr[0].         ToString ()); Table. Addcell (Rdr[1]. ToString ()); }}} catch (Exception ex) {Response.Write (ex).   Message); } doc. ADD (table); }


The table is initially initialized to a two-column table, then sets the fixed width of the table, and then sets the relative width of each column to One-third and two-thirds for the entire table. If you want to set the width to one of 5 and 5, you only need to change the parameters to 1f and 4f respectively. If you want to set the absolute width of each column, simply pass in the column width and the total width of the table, for example:

Float[] widths = new float[] {100f, 116f};

By setting the Spacingbefore and Spacingafter properties of the table, you can set the distance of the table head from the previous element, respectively, and the distance from the next element to the end of the table. This feature is especially effective when there are several tables next to each other in the document. If you don't set the above attribute, the distance between the tables is just like a carriage return in Word, which is as thin as a needle. Next we set the first cell's border to 0,colspan as the number of columns, centering it like a table header. The next step is to programmatically add the data read from the SqlDataReader dynamically to the cell and finally join the table:

The next code shows some options for formatting cells, and as you can see, Itextsharp's authors follow the CSS naming conventions to set cell options to make formatting cells easier (and, of course, I assume you understand CSS ...). ):

pdfptable table = new pdfptable (3); Table. Addcell ("Cell 1"); Pdfpcell cell = new Pdfpcell (new Phrase ("Cell 2", New Font (Font.helvetica, 8f, Font.normal, Color.yellow))); Cell. BackgroundColor = new Color (0, 150, 0); Cell. BorderColor = new Color (255,242,0); Cell. Border = Rectangle.bottom_border | Rectangle.top_border; Cell. Borderwidthbottom = 3f; Cell. Borderwidthtop = 3f; Cell. Paddingbottom = 10f; Cell. Paddingleft = 20f; Cell. Paddingtop = 4f; Table. Addcell (cell); Table. Addcell ("Cell 3"); Doc. ADD (table);

It is not difficult to see in the above code that it is easy to set colspan to make a cell cross multiple lines horizontally. What if you're vertically making the cells span multiple lines? In HTML, you can use the RowSpan property, but there is no rowspan property in Itextsharp. So the only way to achieve this is to nest tables. The following code creates a four-column table, and the lower-right table spans three columns, spanning three rows vertically. Of course, this is what it looks like, but in fact, by nesting a three row-by-column sub-table in the lower-left cell of the table, we set the padding of the cell nested in the lower-left corner to 0 so that the embedded sub-table occupies the entire lower-left cell:

pdfptable table = new pdfptable (4); Table. TotalWidth = 400f; Table. Lockedwidth = true; Pdfpcell Header = new Pdfpcell (New Phrase ("header")); Header. Colspan = 4; Table. Addcell (header); Table. Addcell ("Cell 1"); Table. Addcell ("Cell 2"); Table. Addcell ("Cell 3"); Table. Addcell ("Cell 4"); pdfptable nested = new pdfptable (1); Nested. Addcell ("Nested Row 1"); Nested. Addcell ("Nested Row 2"); Nested. Addcell ("Nested Row 3"); Pdfpcell nesthousing = new Pdfpcell (nested); Nesthousing. Padding = 0f; Table. Addcell (nesthousing); Pdfpcell bottom = new Pdfpcell (New Phrase ("Bottom")); Bottom. Colspan = 3; Table. Addcell (bottom); Doc. ADD (table);

Finally, at the end of this article that illustrates the use of tables, let's look at how to rotate the text in a cell:

pdfptable table = new pdfptable (3); Table. TotalWidth = 144f; Table. Lockedwidth = true; Table. HorizontalAlignment = 0; Pdfpcell left = new Pdfpcell (new Paragraph ("rotated")); Left. Rotation = 90; Table. Addcell (left); Pdfpcell middle = new Pdfpcell (new Paragraph ("rotated")); Middle. Rotation =-90; Table. Addcell (middle); Table. Addcell ("not rotated"); Doc. ADD (table);


The rotation property must be set to a multiple of 90, otherwise an error is raised, and the middle cell's rotation is set to 90 and 270, which by default is counter-clockwise:

In fact, ITEXTSHARP can operate the table is very powerful, in future articles I will be more detailed elaboration. At the same time, you can use Visual Studio's IntelliSense and Object Browser to fully exploit the potential of itextsharp and see how the results are ultimately generated.

Manipulating Pdf–itextsharp in ASP.-Working with Tables

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.