
图表是组织和呈现结构化数据的有效方式. 通过使用 C# PDF 图表发行,您可以显著提高您的数据以结构化行和列显示的方式. 此指南将通过 创建和自定义图表在 PDF 文件中使用 C# 与 Aspose.PDF for .NET 图书馆 的过程。
主题覆盖:
C# Library to Create Tables in PDF
要在 C# 中创建 PDF 表,我们将使用 ASPOSE.PDF 为 .NET 这个强大的工具简化了 编程生成PDF表的过程,允许广泛的定制,包括边界风格,边缘调整和自动列匹配。
安装
要开始,请通过 NuGet 安装 Aspose.PDF for .NET 使用下列命令:
PM> Install-Package Aspose.PDF
在 PDF 文件中创建一个表
遵循以下步骤,以便在新或现有 PDF 文档中使用 C# 添加表格到 PDF 文档:
- 使用下载或创建 PDF 文件 文件 班级。
- 使用一张桌子 桌子 分类,并设置其行和列。
- 通过使用数据的图表 Rows.Add( ) 和 Cells.Add( ) 方法。
- ** 以 C# 创建动态 PDF 表** 通过将表添加到PDF 页面,使用 文件. 页面. 添加() 方法。
- 保存 PDF 文件以保存您的更改。
例子代码
// Create PDF document (to load the existing file, initialize Document object with file's path) | |
Document document = new Document(); | |
// Add page | |
Aspose.Pdf.Page page = document.Pages.Add(); | |
// Initializes a new instance of the Table | |
Aspose.Pdf.Table table = new Aspose.Pdf.Table(); | |
// Set the table border color as LightGray | |
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); | |
// Set the border for table cells | |
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); | |
// Create a loop to add 10 rows | |
for (int row_count = 1; row_count < 10; row_count++) | |
{ | |
// Add row to table | |
Aspose.Pdf.Row row = table.Rows.Add(); | |
// Add table cells | |
row.Cells.Add("Column (" + row_count + ", 1)"); | |
row.Cells.Add("Column (" + row_count + ", 2)"); | |
row.Cells.Add("Column (" + row_count + ", 3)"); | |
} | |
// Add table to the page | |
page.Paragraphs.Add(table); | |
// Save the PDF document | |
document.Save("Generated-PDF.pdf"); |
出口

定制桌面边界和边界
为了提高您的 PDF 表的视觉吸引力和专业性,请考虑应用自定义边界和边界:
例子代码
// Create PDF document (to load the existing file, initialize Document object with file's path) | |
Document document = new Document(); | |
// Add page | |
Aspose.Pdf.Page page = document.Pages.Add(); | |
// Initializes a new instance of the Table | |
Aspose.Pdf.Table table = new Aspose.Pdf.Table(); | |
// Set with column widths of the table | |
table.ColumnWidths = "50 50 50"; | |
// Set default cell border using BorderInfo object | |
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F); | |
// Set table border using another customized BorderInfo object | |
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F); | |
// Create MarginInfo object and set its left, bottom, right and top margins | |
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo(); | |
margin.Top = 5f; | |
margin.Left = 5f; | |
margin.Right = 5f; | |
margin.Bottom = 5f; | |
// Set the default cell padding to the MarginInfo object | |
table.DefaultCellPadding = margin; | |
// Create rows in the table and then cells in the rows | |
Aspose.Pdf.Row row1 = table.Rows.Add(); | |
row1.Cells.Add("col1"); | |
row1.Cells.Add("col2"); | |
row1.Cells.Add(); | |
TextFragment mytext = new TextFragment("col3 with large text string"); | |
// Row1.Cells.Add("col3 with large text string to be placed inside cell"); | |
row1.Cells[2].Paragraphs.Add(mytext); | |
row1.Cells[2].IsWordWrapped = false; | |
// Row1.Cells[2].Paragraphs[0].FixedWidth= 80; | |
Aspose.Pdf.Row row2 = table.Rows.Add(); | |
row2.Cells.Add("item1"); | |
row2.Cells.Add("item2"); | |
row2.Cells.Add("item3"); | |
// Add table to the page | |
page.Paragraphs.Add(table); | |
// Save the PDF document | |
document.Save("Generated-PDF.pdf"); |
出口

在 PDF 表中自定义列
使用 Aspose.PDF for .NET,您可以根据内容或可用的空间自动调整列宽。
- 使用它 列调整 财产设置选项如
AutoFitToContent
或AutoFitToWindow
, 使您能够有效地 **使用 C# 创建 PDF 表。
例子代码
// Create PDF document (to load the existing file, initialize Document object with file's path) | |
Document document = new Document(); | |
// Add page | |
Aspose.Pdf.Page page = document.Pages.Add(); | |
// Initializes a new instance of the Table | |
Aspose.Pdf.Table table = new Aspose.Pdf.Table(); | |
// Set with column widths of the table | |
table.ColumnWidths = "50 50 50"; | |
// Set column adjustment | |
table.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow; | |
// Set default cell border using BorderInfo object | |
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F); | |
// Set table border using another customized BorderInfo object | |
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F); | |
// Create MarginInfo object and set its left, bottom, right and top margins | |
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo(); | |
margin.Top = 5f; | |
margin.Left = 5f; | |
margin.Right = 5f; | |
margin.Bottom = 5f; | |
// Set the default cell padding to the MarginInfo object | |
table.DefaultCellPadding = margin; | |
// Create rows in the table and then cells in the rows | |
Aspose.Pdf.Row row1 = table.Rows.Add(); | |
row1.Cells.Add("col1"); | |
row1.Cells.Add("col2"); | |
row1.Cells.Add(); | |
TextFragment mytext = new TextFragment("col3 with large text string"); | |
// Row1.Cells.Add("col3 with large text string to be placed inside cell"); | |
row1.Cells[2].Paragraphs.Add(mytext); | |
row1.Cells[2].IsWordWrapped = false; | |
// Row1.Cells[2].Paragraphs[0].FixedWidth= 80; | |
Aspose.Pdf.Row row2 = table.Rows.Add(); | |
row2.Cells.Add("item1"); | |
row2.Cells.Add("item2"); | |
row2.Cells.Add("item3"); | |
// Add table to the page | |
page.Paragraphs.Add(table); | |
// Save the PDF document | |
document.Save("Generated-PDF.pdf"); |
免费试用和资源
你可以 获得免费的临时许可证 对于 Aspose.PDF 无限制的 .NET 图书馆。 人们在说什么或与我们联系在我们的 论坛 奉献的支持。
结论
在此指南中,您了解如何在C#中创建PDF表,并使用 .NET 图书馆的 Aspose.PDF 定制它们。 从应用边界和边界到调整列宽,您现在有工具可以轻松地将结构化数据添加到您的 PDF 文件中。