【译】在Asp.Net中操做PDF – iTextSharp - 使用表格

原文 【译】在Asp.Net中操做PDF – iTextSharp - 使用表格html

  使用Asp.Net生成PDF最经常使用的元素应该是表格,表格能够帮助好比订单或者发票类型的文档更加格式化和美观。本篇文章并不会深刻探讨表格,仅仅是提供一个使用iTextSharp生成表格的方法介绍,本文须要阅读我以前iTextSharp系列文章做为基础:web

 

    在ASP.NET中建立PDF-iTextSharp起步数据库

    在Asp.Net中操做PDF - iTextSharp - 使用字体编程

    在Asp.Net中操做PDF – iTextSharp -利用块,短语,段落添加文本浏览器

    在Asp.Net中操做PDF – iTextSharp-列表 
    在Asp.Net中操做PDF – iTextSharp - 使用连接和书签函数

 

    使用iTextSharp来操做表格是一件简单的事,尤为是iTextSharp中表格元素的命名方式和HTML与CSS中很是相似。iTextSharp提供了多个类用于建立表格,为了避免让读者产生混淆,这里我使用PdfPTable这个专门为在PDF中建立表格的类,下面代码展现了如何建立一个表格并将其加入PDF中:post

 

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);

 

   经过为pdfpTable的构造函数传入整数3,pdfpTable被初始化为一个三列的表格.为pdfpTabled添加单元格有多种方式,第一个单元格是经过PdfPCell对象添加进去的,PdfPCell的构造函数接受一个Phrase对象做为参数,而后将Cell的colspan设置为3,这样这个单元格占了整个一行.就像HTML中表格那样,单元格的水平对齐方式使用了三个值中的一个(译者:左对齐,居中,右对齐),这三个值我加在了注释中。后面的单元格我都经过AddCell方法加入,最后文档的效果以下:字体

    1

     

    下面代码从数据库抽取值,并将数据插入到iTextSharp生成的表格中,下面代码还设置了一些表格的展示方式:url

 

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);
 
}

 

   这个表格一开始被初始化为两列的表格,而后设置了表格的固定宽度,而后对每一列设置相对宽度为别为整个表格的三分之一和三分之二。若是你想将宽度设置为5分之一和是5分之四,只须要将参数分别改成1f和4f.若是你想设置每列的绝对宽度,只须要将列宽度和表格的总宽度传入,例如:spa

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

 

   经过设置表格的SpacingBefore和SpacingAfter属性,能够分别设置表格头部离上一个元素的距离以及表格结束离下一个元素的距离.在文档中有几个表格紧挨着时,这个功能尤为有效。若是不设置上述属性,那表格之间的距离就像在word中一个回车的距离同样,那会和针同样细。接下来咱们经过设置第一个单元格的边框为0,colspan为列数,居中使其像表格的标题同样。接下来就是咱们用编程的方式将从SqlDataReader读取到的数据动态的添加到单元格中最后加入表格:

     2

 

   接下来的代码展现了格式化单元格的一些选项,正如你所见,iTextSharp的做者遵循CSS的命名规则来设置单元格的选项使格式化单元格更加容易(固然,我假设你了解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);

 

3

   上面代码中不难看出,经过设置colspan来让一个单元格在水平上跨多行十分容易。那若是是在垂直上使单元格跨越多行呢?在HTML中,你可使用Rowspan属性,可是在iTextSharp中并无Rowspan属性。因此达到这个目的的方法只有嵌套表格。下面代码建立了一个四列的表格,右下的表格横跨三列,竖跨三行。固然,这是表面看起来这样,但其实是经过在表格左下角的单元格中嵌套一个三行一列的子表格,咱们将左下角嵌套子表格的单元格的padding所有设置为0使被嵌入的子表格占据了整个左下单元格:

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);

 

    4   

    最后,在这篇阐述使用表格的文章末尾,咱们来看看如何将一个单元格中的文本进行旋转:

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);

 

   Rotation属性必须设置成90的倍数,不然就会引起错误,middle单元格的Rotation在这里设置成-90和270效果同样,这个度数默认是按逆时针算的:

  

    5

 

   实际上iTextSharp能够操做表格的功能很是强大,在将来的文章中我会更加详细的阐述。于此同时,你们可使用Visual Studio的智能感知和对象浏览器充分挖掘iTextSharp的潜力,并看看最终生成的结果如何.

 

--------------------------------

原文连接:iTextSharp-Introducing-Tables

translated by CareySon

相关文章
相关标签/搜索