翻譯|使用教程|編輯:李顯亮|2019-07-24 10:53:30.187|閱讀 1526 次
概述:Aspose.Words For .Net是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。接下來我們將進入“使用格式”的介紹,其中包括應用格式、介紹和創建表、添加和拆分表以及使用列和行。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words For .Net是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
【下載Aspose.Words for .NET最新試用版】
接下來我們將進入“使用格式”的介紹,其中包括應用格式、介紹和創建表、添加和拆分表以及使用列和行。
表的每個元素都可以應用不同的格式。例如,表格格式將應用于整個表格,而行格式化僅影響特定行等。Aspose.Words提供了豐富的API來檢索和應用格式化表格。您可以使用Table,RowFormat和CellFormat節點來設置格式。
要將格式應用于表,可以使用相應表節點上的可用屬性。下面給出了Microsoft Word中表格格式功能的可視化視圖及其在Aspose.Words中的相應屬性。
下面的示例顯示如何將輪廓邊框應用于表:
Document doc = new Document(dataDir + "Table.EmptyTable.doc"); Table table = (Table)doc.GetChild(NodeType.Table, 0, true); //將表格對齊到頁面的中心。 table.Alignment = TableAlignment.Center; // Clear any existing borders from the table. table.ClearBorders(); //在桌子周圍設置一個綠色的邊框,但不要在里面。 table.SetBorder(BorderType.Left, LineStyle.Single, 1.5, Color.Green, true); table.SetBorder(BorderType.Right, LineStyle.Single, 1.5, Color.Green, true); table.SetBorder(BorderType.Top, LineStyle.Single, 1.5, Color.Green, true); table.SetBorder(BorderType.Bottom, LineStyle.Single, 1.5, Color.Green, true); // 用淡綠色填充單元格。 table.SetShading(TextureIndex.TextureSolid, Color.LightGreen, Color.Empty); dataDir = dataDir + "Table.SetOutlineBorders_out.doc"; // 將文檔保存到磁盤。 doc.Save(dataDir);
下面的示例展示了如何構建一個啟用所有邊框(網格)的表:
Document doc = new Document(dataDir + "Table.EmptyTable.doc"); Table table = (Table)doc.GetChild(NodeType.Table, 0, true); //清除表中任何現有的邊框。 table.ClearBorders(); //在桌子周圍和里面設置一個綠色邊框。 table.SetBorders(LineStyle.Single, 1.5, Color.Green); dataDir = dataDir + "Table.SetAllBorders_out.doc"; //將文檔保存到磁盤。 doc.Save(dataDir);
可以使用Row 的RowFormat屬性控制行級別的格式。
下面的示例演示如何修改表格行的格式:
Document doc = new Document(dataDir + "Table.Document.doc"); Table table = (Table)doc.GetChild(NodeType.Table, 0, true); // 檢索表中的第一個單元格。 Cell firstCell = table.FirstRow.FirstCell; //修改一些單元級屬性。 firstCell.CellFormat.Width = 30; // In points firstCell.CellFormat.Orientation = TextOrientation.Downward; firstCell.CellFormat.Shading.ForegroundPatternColor = Color.LightGreen;
使用Cell 的CellFormat屬性控制單元級別的格式。
下面的示例演示如何修改表格單元格的格式:
Document doc = new Document(dataDir + "Table.Document.doc"); Table table = (Table)doc.GetChild(NodeType.Table, 0, true); //檢索表中的第一個單元格。 Cell firstCell = table.FirstRow.FirstCell; //修改一些單元級屬性。 firstCell.CellFormat.Width = 30; // In points firstCell.CellFormat.Orientation = TextOrientation.Downward; firstCell.CellFormat.Shading.ForegroundPatternColor = Color.LightGreen;
下面的代碼示例顯示了如何設置要添加到單元格內容的左/上/右/下的空間量(以磅為單位):
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.StartTable(); builder.InsertCell(); //設置要添加到單元格內容的左側/頂部/右側/底部的空間量(以點為單位)。 builder.CellFormat.SetPaddings(30, 50, 30, 50); builder.Writeln("I'm a wonderful formatted cell."); builder.EndRow(); builder.EndTable(); dataDir = dataDir + "Table.SetCellPadding_out.doc"; //將文檔保存到磁盤。 doc.Save(dataDir);
使用高度和高度規則屬性控制表格行的高度。 這些可以針對表中的每一行進行不同的設置,以允許對每行的高度進行廣泛控制。 在Aspose.Words中,這些由給定Row的RowFormat.Height和RowFormat.HeightRule屬性表示。
高度價值 | 描述 |
Auto | 這是給予新行的默認高度規則。 從技術上講,這意味著沒有定義高度規則。 該行的大小適合該行單元格中的最大內容。 |
At Least | 使用此設置,行的高度將增大以適應行的內容,但永遠不會小于RowFormat.Height中的指定大小。 |
Exactly | 行的大小完全設置為RowFormat.Height中找到的值,并且不會增長到適合內容。 |
下面的示例顯示如何創建包含單個單元格的表并應用行格式化:
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.StartTable(); builder.InsertCell(); //設置行格式 RowFormat rowFormat = builder.RowFormat; rowFormat.Height = 100; rowFormat.HeightRule = HeightRule.Exactly; //這些格式化屬性設置在表上,并應用于表中的所有行。 table.LeftPadding = 30; table.RightPadding = 30; table.TopPadding = 30; table.BottomPadding = 30; builder.Writeln("I'm a wonderful formatted row."); builder.EndRow(); builder.EndTable(); dataDir = dataDir + "Table.ApplyRowFormatting_out.doc"; //將文檔保存到磁盤。 doc.Save(dataDir);
邊框和著色可以使用Table.SetBorder,Table.SetBorders和Table.SetShading在表格范圍內應用,也可以僅使用CellFormat.Borders和CellFormat.Shading應用于特定單元格。 另外,可以使用RowFormat.Borders在一行上設置邊框,但是不能以這種方式應用著色。
下面的示例演示如何使用不同的邊框和陰影格式化表格和單元格。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.StartTable(); builder.InsertCell(); // 為整個表設置邊框。 table.SetBorders(LineStyle.Single, 2.0, Color.Black); //為這個單元格設置單元格陰影。 builder.CellFormat.Shading.BackgroundPatternColor = Color.Red; builder.Writeln("Cell #1"); builder.InsertCell(); //為第二個單元格指定不同的單元格著色。 builder.CellFormat.Shading.BackgroundPatternColor = Color.Green; builder.Writeln("Cell #2"); //結束這一行。 builder.EndRow(); //清除以前操作中的單元格格式。 builder.CellFormat.ClearFormatting(); // 創建第二行。 builder.InsertCell(); //為該行的第一個單元格創建更大的邊框。這將是不同的。 //與為表設置的邊框相比。 builder.CellFormat.Borders.Left.LineWidth = 4.0; builder.CellFormat.Borders.Right.LineWidth = 4.0; builder.CellFormat.Borders.Top.LineWidth = 4.0; builder.CellFormat.Borders.Bottom.LineWidth = 4.0; builder.Writeln("Cell #3"); builder.InsertCell(); //清除前一個單元格的單元格格式。 builder.CellFormat.ClearFormatting(); builder.Writeln("Cell #4"); //保存完成文檔。 doc.Save(dataDir + "Table.SetBordersAndShading_out.doc");
*想要獲取Aspose.Words正版授權可聯系哦~
ASPOSE技術交流QQ群已開通,各類資源及時分享,歡迎交流討論!(掃描下方二維碼加入群聊)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn