Aspose.Pdf for .NET可创建表单对象并且将表单对象置于PDF文档中。添加表单对象前,我们需要指定列宽信息,单元格中也被加入指定信息。但是, 我们可以在运行时指定列宽信息。当表的宽高比例为100%时,列宽的调整便基于一行中单元格的数量。

  请大家看看下面的一段代码,在下面的代码示例中,我们已经在Section宽度的基础上调整了单元格的宽度:

[C#]

// create Pdf Instance

Pdf pdf = new Pdf();

        // create section object and add it to pdf object

        Section sec1 = pdf.Sections.Add();

        // create a table instance

        Table mytable = new Table();

        // specify the default border for table object and set its color as Navy

        mytable.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 1f, new Aspose.Pdf.Color("Navy"));

        // specify the border information for table object

        mytable.Border= new BorderInfo((int)BorderSide.All, 1f, new Aspose.Pdf.Color("Navy"));

        // define initial width information for table cells

        mytable.ColumnWidths = "100 100";

        // create a row object and add it to table

        Row row1 = mytable.Rows.Add();

        // create loop to add specify the number of columns to be added to table row

        for (int Column_Counter = 1; Column_Counter <=7; Column_Counter++)

        {

    // create a cell object and add it to table row

    Cell Cell = row1.Cells.Add("Cell (1,"+ Column_Counter.ToString() +")");

        }

        // define the variable to keep record of number of columns in table

        int TableColumn = 0;

        // traverse through each table cell in row object

        foreach (Cell InnerCell in row1.Cells)

        {

        // specify the width information for each column based on section objects margin and width values

        // set the width value as, get the total width of section and subtract left and right margin and divide

        // it with total number of cells in a particular table row

        mytable.SetColumnWidth(TableColumn, (sec1.PageInfo.PageWidth - sec1.PageInfo.Margin.Left - sec1.PageInfo.Margin.Right)/ row1.Cells.Count);

        // increase the value of variable holding the column count information

        TableColumn += 1;

        }

// add table to paragraphs collection of section

        sec1.Paragraphs.Add(mytable);

        // save the resultant PDF document

        pdf.Save("d:/pdftest/DynamicTableWidth.pdf");

[VB.NET]

' create Pdf Instance

        Dim pdf As Pdf = New Pdf()

        ' create section object and add it to pdf object

        Dim sec1 As Aspose.Pdf.Section = pdf.Sections.Add()

        ' create a table instance

        Dim mytable As Table = New Table()

        ' specify the default border for table object and set its color as Navy

        mytable.DefaultCellBorder = New BorderInfo(BorderSide.All, 1.0F, New Aspose.Pdf.Color("Navy"))

        ' specify the border information for table object

        mytable.Border = New BorderInfo(BorderSide.All, 1.0F, New Aspose.Pdf.Color("Navy"))

        ' define initial width information for table cells

        mytable.ColumnWidths = "100 100"

        ' create a row object and add it to table

        Dim row1 As Row = mytable.Rows.Add()

        ' create loop to add specify the number of columns to be added to table row

        For Column_Counter As Integer = 1 To 5 Step 1

            ' create a cell object and add it to table row

            Dim Cell As Cell = row1.Cells.Add("Cell (1," + Column_Counter.ToString() + ")")

        Next

        ' define the variable to keep record of number of columns in table

        Dim TableColumn As Integer = 0

        ' create a cell object

        Dim InnerCell As Cell

        ' traverse through each table cell in row object

        For Each InnerCell In row1.Cells

            ' specify the width information for each column based on section objects margin and width values

            ' set the width value as, get the total width of section and subtract left and right margin and divide

            ' it with total number of cells in a particular table row

            mytable.SetColumnWidth(TableColumn, (sec1.PageInfo.PageWidth - sec1.PageInfo.Margin.Left - sec1.PageInfo.Margin.Right) / row1.Cells.Count)

            ' increase the value of variable holding the column count information

            TableColumn += 1

        Next

        ' add table to paragraphs collection of section

        sec1.Paragraphs.Add(mytable)

        ' save the resultant PDF document

        pdf.Save("d:/pdftest/DynamicTableWidth.pdf")

作者: zhuhm 发表于 2011-08-05 10:55 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"