PDF管理控件Aspose.PDF for .Net使用教程(三十七):设置表格的边框样式、边距和填充

在本系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何设置表格的边框样式,边距和填充。

Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用AdobeAcrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。

在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何设置表格的边框样式,边距和填充。

>>Aspose.PDF for .NET更新至最新版v20.5,欢迎下载体验。


Aspose.PDF for .NET允许开发人员在PDF文档中创建表格。而且,它们可以将边框样式,边距和单元格填充等效果应用于表格。在深入了解技术细节之前,重要的是要了解下图所示的边框,边距和填充的概念:

边框,边距和填充

PDF管理控件Aspose.PDF for .Net使用教程:设置表格的边框样式、边距和填充

在上图中,可以看到表格,行和单元格的边界重叠。使用Aspose.PDF for .NET,表格可以具有边距和单元格填充。要设置单元格的边距,我们必须设置单元格填充。

边框

要设置的边界Table,Row和Cell对象,请使用Table.Border,Row.Border和Cell.Border性能。也可以使用Table或Row类的DefaultCellBorder属性来设置单元格边框。上面讨论的所有与边框相关的属性都分配了Row该类的实例,该类是通过调用其构造函数创建的。本Row类有需要的几乎所有以自定义边框所需的参数很多重载。

边距或填充

单元格填充可以使用Tableclass的DefaultCellPaddingproperty 进行管理。所有的填充相关的属性分配的一个实例,MarginInfo大约需要的信息类Left,Right,Top和Bottom参数来创建自定义边距。在下面的示例中,单元格边框的宽度设置为0.1点,表边框的宽度设置为1点,单元格填充设置为5点。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Tables();    // Instntiate the Document object by calling its empty constructorDocument doc = new Document();Page page = doc.Pages.Add();// Instantiate a table objectAspose.Pdf.Table tab1 = new Aspose.Pdf.Table();// Add the table in paragraphs collection of the desired sectionpage.Paragraphs.Add(tab1);// Set with column widths of the tabletab1.ColumnWidths = "50 50 50";// Set default cell border using BorderInfo objecttab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);// Set table border using another customized BorderInfo objecttab1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);// Create MarginInfo object and set its left, bottom, right and top marginsAspose.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 objecttab1.DefaultCellPadding = margin;// Create rows in the table and then cells in the rowsAspose.Pdf.Row row1 = tab1.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 = tab1.Rows.Add();row2.Cells.Add("item1");row2.Cells.Add("item2");row2.Cells.Add("item3");dataDir = dataDir + "MarginsOrPadding_out.pdf";// Save the Pdfdoc.Save(dataDir);

要创建带有圆角的表,请使用BorderInfo类的RoundedBorderRadius值并将表的角样式设置为圆形。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Tables();Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();GraphInfo graph = new GraphInfo();graph.Color = Aspose.Pdf.Color.Red;// Create a blank BorderInfo objectBorderInfo bInfo = new BorderInfo(BorderSide.All, graph);// Set the border a rounder border where radius of round is 15bInfo.RoundedBorderRadius = 15;// Set the table Corner style as Round.tab1.CornerStyle = Aspose.Pdf.BorderCornerStyle.Round;// Set the table border informationtab1.Border = bInfo;

双边框

如上所述,边框可以加入Table或Cell对象。我们的用户要求我们添加一项功能,允许他们在Table和Cell对象周围添加双边框。NET 8.8.0的Aspose.PDF中添加了此功能。下面的代码段显示了如何实现此要求。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Tables();   // Instantiate Document objectDocument doc = new Document();// Add page to PDF documentPage page = doc.Pages.Add();// Create BorderInfo objectAspose.Pdf.BorderInfo border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All);// Specify that Top border will be doubleborder.Top.IsDoubled = true;// Specify that bottom border will be doubleborder.Bottom.IsDoubled = true;// Instantiate Table objectAspose.Pdf.Table table = new Aspose.Pdf.Table();// Specify Columns width informationtable.ColumnWidths = "100";// Create Row objectAspose.Pdf.Row row = table.Rows.Add();// Add a Table cell to cells collection of rowAspose.Pdf.Cell cell = row.Cells.Add("some text");// Set the border for cell object (double border)cell.Border = border;// Add table to paragraphs collection of Pagepage.Paragraphs.Add(table);dataDir = dataDir + "TableBorderTest_out.pdf";// Save the PDF documentdoc.Save(dataDir);


还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年4月25日
下一篇 2020年4月25日

相关推荐

发表回复

登录后才能评论