PDF管理控件Aspose.PDF for .Net使用教程(四十一):在帶標(biāo)簽的PDF中使用表格
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護(hù)和打印PDF文檔,而無(wú)需使用Adobe Acrobat。此外,API還提供PDF壓縮選項(xiàng),表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴(kuò)展的安全控制和自定義字體處理。
在接下來(lái)的系列教程中,將為開(kāi)發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹如何在帶標(biāo)簽的PDF中使用表格。包括:
- 在帶標(biāo)簽的PDF中創(chuàng)建表格
- 樣式表元素
- 樣式表行
- 樣式表單元
>>Aspose.PDF for .NET更新至最新版v20.7,歡迎下載體驗(yàn)。
在帶標(biāo)簽的PDF中創(chuàng)建表格
用于.NET的Aspose.PDF允許在帶標(biāo)簽的PDF文檔中創(chuàng)建表格。為了處理表,API提供了TableElement類。為了創(chuàng)建表,可以使用ITaggedContent接口的CreateTableElement()方法。此外,可以使用 TableElement類的CreateTHead(),CreateTBody()和CreateTFoot()方法分別創(chuàng)建Table Head,Table Body和Table Foot。若要?jiǎng)?chuàng)建表行,可以使用TableRowCollectionElement的 CreateTR()方法 類。您還可以使用Document類的Validate()方法檢查創(chuàng)建的PDF文檔是否符合PDF / UA。以下代碼段顯示了如何在“帶標(biāo)簽的PDF”文檔中創(chuàng)建表格:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); // Create document Document document = new Document(); ITaggedContent taggedContent = document.TaggedContent; taggedContent.SetTitle("Example table"); taggedContent.SetLanguage("en-US"); // Get root structure element StructureElement rootElement = taggedContent.RootElement; TableElement tableElement = taggedContent.CreateTableElement(); rootElement.AppendChild(tableElement); tableElement.Border = new BorderInfo(BorderSide.All, 1.2F, Color.DarkBlue); TableTHeadElement tableTHeadElement = tableElement.CreateTHead(); TableTBodyElement tableTBodyElement = tableElement.CreateTBody(); TableTFootElement tableTFootElement = tableElement.CreateTFoot(); int rowCount = 50; int colCount = 4; int rowIndex; int colIndex; TableTRElement headTrElement = tableTHeadElement.CreateTR(); headTrElement.AlternativeText = "Head Row"; headTrElement.BackgroundColor = Color.LightGray; for (colIndex = 0; colIndex < colCount; colIndex++) { TableTHElement thElement = headTrElement.CreateTH(); thElement.SetText(String.Format("Head {0}", colIndex)); thElement.BackgroundColor = Color.GreenYellow; thElement.Border = new BorderInfo(BorderSide.All, 4.0F, Color.Gray); thElement.IsNoBorder = true; thElement.Margin = new MarginInfo(16.0, 2.0, 8.0, 2.0); thElement.Alignment = HorizontalAlignment.Right; } for (rowIndex = 0; rowIndex < rowCount; rowIndex++) { TableTRElement trElement = tableTBodyElement.CreateTR(); trElement.AlternativeText = String.Format("Row {0}", rowIndex); for (colIndex = 0; colIndex < colCount; colIndex++) { int colSpan = 1; int rowSpan = 1; if (colIndex == 1 && rowIndex == 1) { colSpan = 2; rowSpan = 2; } else if (colIndex == 2 && (rowIndex == 1 || rowIndex == 2)) { continue; } else if (rowIndex == 2 && (colIndex == 1 || colIndex == 2)) { continue; } TableTDElement tdElement = trElement.CreateTD(); tdElement.SetText(String.Format("Cell [{0}, {1}]", rowIndex, colIndex)); tdElement.BackgroundColor = Color.Yellow; tdElement.Border = new BorderInfo(BorderSide.All, 4.0F, Color.Gray); tdElement.IsNoBorder = false; tdElement.Margin = new MarginInfo(8.0, 2.0, 8.0, 2.0); tdElement.Alignment = HorizontalAlignment.Center; TextState cellTextState = new TextState(); cellTextState.ForegroundColor = Color.DarkBlue; cellTextState.FontSize = 7.5F; cellTextState.FontStyle = FontStyles.Bold; cellTextState.Font = FontRepository.FindFont("Arial"); tdElement.DefaultCellTextState = cellTextState; tdElement.IsWordWrapped = true; tdElement.VerticalAlignment = VerticalAlignment.Center; tdElement.ColSpan = colSpan; tdElement.RowSpan = rowSpan; } } TableTRElement footTrElement = tableTFootElement.CreateTR(); footTrElement.AlternativeText = "Foot Row"; footTrElement.BackgroundColor = Color.LightSeaGreen; for (colIndex = 0; colIndex < colCount; colIndex++) { TableTDElement tdElement = footTrElement.CreateTD(); tdElement.SetText(String.Format("Foot {0}", colIndex)); tdElement.Alignment = HorizontalAlignment.Center; tdElement.StructureTextState.FontSize = 7F; tdElement.StructureTextState.FontStyle = FontStyles.Bold; } StructureAttributes tableAttributes = tableElement.Attributes.GetAttributes(AttributeOwnerStandard.Table); StructureAttribute summaryAttribute = new StructureAttribute(AttributeKey.Summary); summaryAttribute.SetStringValue("The summary text for table"); tableAttributes.SetAttribute(summaryAttribute); // Save Tagged Pdf Document document.Save(dataDir + "CreateTableElement.pdf"); // Checking PDF/UA compliance document = new Document(dataDir + "CreateTableElement.pdf"); bool isPdfUaCompliance = document.Validate(dataDir + "table.xml", PdfFormat.PDF_UA_1); Console.WriteLine(String.Format("PDF/UA compliance: {0}", isPdfUaCompliance));
樣式表元素
用于.NET的Aspose.PDF允許樣式化Tagged PDF文檔中的表格。為了給表設(shè)置樣式,可以使用ITaggedContent 接口的CreateTableElement() 方法 創(chuàng)建表, 并使用TableElement 類的屬性設(shè)置樣式表。以下是可用于設(shè)置表格樣式的列表屬性:
- BackgroundColor
- Border
- Alignment
- CornerStyle
- Broken
- ColumnAdjustment
- ColumnWidths
- DefaultCellBorder
- DefaultCellPadding
- DefaultCellTextState
- DefaultColumnWidth
- IsBroken
- IsBordersIncluded
- Left
- Top
以下代碼段顯示了如何在“帶標(biāo)簽的PDF”文檔中設(shè)置表格樣式:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); // Create document Document document = new Document(); ITaggedContent taggedContent = document.TaggedContent; taggedContent.SetTitle("Example table style"); taggedContent.SetLanguage("en-US"); // Get root structure element StructureElement rootElement = taggedContent.RootElement; // Create table structure element TableElement tableElement = taggedContent.CreateTableElement(); rootElement.AppendChild(tableElement); tableElement.BackgroundColor = Color.Beige; tableElement.Border = new BorderInfo(BorderSide.All, 0.80F, Color.Gray); tableElement.Alignment = HorizontalAlignment.Center; tableElement.Broken = TableBroken.Vertical; tableElement.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow; tableElement.ColumnWidths = "80 80 80 80 80"; tableElement.DefaultCellBorder = new BorderInfo(BorderSide.All, 0.50F, Color.DarkBlue); tableElement.DefaultCellPadding = new MarginInfo(16.0, 2.0, 8.0, 2.0); tableElement.DefaultCellTextState.ForegroundColor = Color.DarkCyan; tableElement.DefaultCellTextState.FontSize = 8F; tableElement.DefaultColumnWidth = "70"; tableElement.IsBroken = false; tableElement.IsBordersIncluded = true; tableElement.Left = 0F; tableElement.Top = 40F; tableElement.RepeatingColumnsCount = 2; tableElement.RepeatingRowsCount = 3; TextState rowStyle = new TextState(); rowStyle.BackgroundColor = Color.LightCoral; tableElement.RepeatingRowsStyle = rowStyle; TableTHeadElement tableTHeadElement = tableElement.CreateTHead(); TableTBodyElement tableTBodyElement = tableElement.CreateTBody(); TableTFootElement tableTFootElement = tableElement.CreateTFoot(); int rowCount = 10; int colCount = 5; int rowIndex; int colIndex; TableTRElement headTrElement = tableTHeadElement.CreateTR(); headTrElement.AlternativeText = "Head Row"; for (colIndex = 0; colIndex < colCount; colIndex++) { TableTHElement thElement = headTrElement.CreateTH(); thElement.SetText(String.Format("Head {0}", colIndex)); } for (rowIndex = 0; rowIndex < rowCount; rowIndex++) { TableTRElement trElement = tableTBodyElement.CreateTR(); trElement.AlternativeText = String.Format("Row {0}", rowIndex); for (colIndex = 0; colIndex < colCount; colIndex++) { TableTDElement tdElement = trElement.CreateTD(); tdElement.SetText(String.Format("Cell [{0}, {1}]", rowIndex, colIndex)); } } TableTRElement footTrElement = tableTFootElement.CreateTR(); footTrElement.AlternativeText = "Foot Row"; for (colIndex = 0; colIndex < colCount; colIndex++) { TableTDElement tdElement = footTrElement.CreateTD(); tdElement.SetText(String.Format("Foot {0}", colIndex)); } // Save Tagged Pdf Document document.Save(dataDir + "StyleTableElement.pdf"); // Checking PDF/UA compliance document = new Document(dataDir + "StyleTableElement.pdf"); bool isPdfUaCompliance = document.Validate(dataDir + "StyleTableElement.xml", PdfFormat.PDF_UA_1); Console.WriteLine(String.Format("PDF/UA compliance: {0}", isPdfUaCompliance));
樣式表行
用于.NET的Aspose.PDF允許在標(biāo)記的PDF文檔中設(shè)置表格行的樣式。為了設(shè)置表格行的樣式,可以使用TableTRElement 類的屬性。以下是可用于設(shè)置表格行樣式的列表屬性:
- BackgroundColor
- Border
- DefaultCellBorder
- MinRowHeight
- FixedRowHeight
- IsInNewPage
- IsRowBroken
- DefaultCellTextState
- DefaultCellPadding
- VerticalAlignment
以下代碼段顯示了如何在“標(biāo)記的PDF”文檔中設(shè)置表格行的樣式:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); // Create document Document document = new Document(); ITaggedContent taggedContent = document.TaggedContent; taggedContent.SetTitle("Example table row style"); taggedContent.SetLanguage("en-US"); // Get root structure element StructureElement rootElement = taggedContent.RootElement; // Create table structure element TableElement tableElement = taggedContent.CreateTableElement(); rootElement.AppendChild(tableElement); TableTHeadElement tableTHeadElement = tableElement.CreateTHead(); TableTBodyElement tableTBodyElement = tableElement.CreateTBody(); TableTFootElement tableTFootElement = tableElement.CreateTFoot(); int rowCount = 7; int colCount = 3; int rowIndex; int colIndex; TableTRElement headTrElement = tableTHeadElement.CreateTR(); headTrElement.AlternativeText = "Head Row"; for (colIndex = 0; colIndex < colCount; colIndex++) { TableTHElement thElement = headTrElement.CreateTH(); thElement.SetText(String.Format("Head {0}", colIndex)); } for (rowIndex = 0; rowIndex < rowCount; rowIndex++) { TableTRElement trElement = tableTBodyElement.CreateTR(); trElement.AlternativeText = String.Format("Row {0}", rowIndex); trElement.BackgroundColor = Color.LightGoldenrodYellow; trElement.Border = new BorderInfo(BorderSide.All, 0.75F, Color.DarkGray); trElement.DefaultCellBorder = new BorderInfo(BorderSide.All, 0.50F, Color.Blue); trElement.MinRowHeight = 100.0; trElement.FixedRowHeight = 120.0; trElement.IsInNewPage = (rowIndex % 3 == 1); trElement.IsRowBroken = true; TextState cellTextState = new TextState(); cellTextState.ForegroundColor = Color.Red; trElement.DefaultCellTextState = cellTextState; trElement.DefaultCellPadding = new MarginInfo(16.0, 2.0, 8.0, 2.0); trElement.VerticalAlignment = VerticalAlignment.Bottom; for (colIndex = 0; colIndex < colCount; colIndex++) { TableTDElement tdElement = trElement.CreateTD(); tdElement.SetText(String.Format("Cell [{0}, {1}]", rowIndex, colIndex)); } } TableTRElement footTrElement = tableTFootElement.CreateTR(); footTrElement.AlternativeText = "Foot Row"; for (colIndex = 0; colIndex < colCount; colIndex++) { TableTDElement tdElement = footTrElement.CreateTD(); tdElement.SetText(String.Format("Foot {0}", colIndex)); } // Save Tagged Pdf Document document.Save(dataDir + "StyleTableRow.pdf"); // Checking PDF/UA compliance document = new Document(dataDir + "StyleTableRow.pdf"); bool isPdfUaCompliance = document.Validate(dataDir + "StyleTableRow.xml", PdfFormat.PDF_UA_1); Console.WriteLine(String.Format("PDF/UA compliance: {0}", isPdfUaCompliance));
樣式表單元
用于.NET的Aspose.PDF允許樣式化Tagged PDF文檔中的表格單元格。為了設(shè)置表格單元格的樣式,可以使用TableCellElement類的屬性。以下是可用于設(shè)置表格單元格樣式的列表屬性:
- BackgroundColor
- Border
- IsNoBorder
- Margin
- Alignment
- DefaultCellTextState
- IsWordWrapped
- VerticalAlignment
- ColSpan
- RowSpan
以下代碼段顯示了如何在“標(biāo)記的PDF”文檔中設(shè)置表格單元的樣式:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); // Create document Document document = new Document(); ITaggedContent taggedContent = document.TaggedContent; taggedContent.SetTitle("Example table cell style"); taggedContent.SetLanguage("en-US"); // Get root structure element StructureElement rootElement = taggedContent.RootElement; // Create table structure element TableElement tableElement = taggedContent.CreateTableElement(); rootElement.AppendChild(tableElement); TableTHeadElement tableTHeadElement = tableElement.CreateTHead(); TableTBodyElement tableTBodyElement = tableElement.CreateTBody(); TableTFootElement tableTFootElement = tableElement.CreateTFoot(); int rowCount = 4; int colCount = 4; int rowIndex; int colIndex; TableTRElement headTrElement = tableTHeadElement.CreateTR(); headTrElement.AlternativeText = "Head Row"; for (colIndex = 0; colIndex < colCount; colIndex++) { TableTHElement thElement = headTrElement.CreateTH(); thElement.SetText(String.Format("Head {0}", colIndex)); thElement.BackgroundColor = Color.GreenYellow; thElement.Border = new BorderInfo(BorderSide.All, 4.0F, Color.Gray); thElement.IsNoBorder = true; thElement.Margin = new MarginInfo(16.0, 2.0, 8.0, 2.0); thElement.Alignment = HorizontalAlignment.Right; } for (rowIndex = 0; rowIndex < rowCount; rowIndex++) { TableTRElement trElement = tableTBodyElement.CreateTR(); trElement.AlternativeText = String.Format("Row {0}", rowIndex); for (colIndex = 0; colIndex < colCount; colIndex++) { int colSpan = 1; int rowSpan = 1; if (colIndex == 1 && rowIndex == 1) { colSpan = 2; rowSpan = 2; } else if (colIndex == 2 && (rowIndex == 1 || rowIndex == 2)) { continue; } else if (rowIndex == 2 && (colIndex == 1 || colIndex == 2)) { continue; } TableTDElement tdElement = trElement.CreateTD(); tdElement.SetText(String.Format("Cell [{0}, {1}]", rowIndex, colIndex)); tdElement.BackgroundColor = Color.Yellow; tdElement.Border = new BorderInfo(BorderSide.All, 4.0F, Color.Gray); tdElement.IsNoBorder = false; tdElement.Margin = new MarginInfo(8.0, 2.0, 8.0, 2.0); tdElement.Alignment = HorizontalAlignment.Center; TextState cellTextState = new TextState(); cellTextState.ForegroundColor = Color.DarkBlue; cellTextState.FontSize = 7.5F; cellTextState.FontStyle = FontStyles.Bold; cellTextState.Font = FontRepository.FindFont("Arial"); tdElement.DefaultCellTextState = cellTextState; tdElement.IsWordWrapped = true; tdElement.VerticalAlignment = VerticalAlignment.Center; tdElement.ColSpan = colSpan; tdElement.RowSpan = rowSpan; } } TableTRElement footTrElement = tableTFootElement.CreateTR(); footTrElement.AlternativeText = "Foot Row"; for (colIndex = 0; colIndex < colCount; colIndex++) { TableTDElement tdElement = footTrElement.CreateTD(); tdElement.SetText(String.Format("Foot {0}", colIndex)); } // Save Tagged Pdf Document document.Save(dataDir + "StyleTableCell.pdf"); // Checking PDF/UA compliance document = new Document(dataDir + "StyleTableCell.pdf"); bool isPdfUaCompliance = document.Validate(dataDir + "StyleTableCell.xml", PdfFormat.PDF_UA_1); Console.WriteLine(String.Format("PDF/UA compliance: {0}", isPdfUaCompliance));
還想要更多嗎?您可以點(diǎn)擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問(wèn)或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。