翻譯|使用教程|編輯:胡濤|2023-02-01 10:36:55.933|閱讀 160 次
概述:本文介紹如何通過 Spire.Doc 將表格在 Word 文檔中的位置設置為外部,歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
word文檔中的表格可以讓你的數據更有條理,更整潔,這篇文章講的是通過Spire.Doc在word文檔中設置表格的絕對位置。這里嘗試實現在header上的圖片右側放置一個table。
以下是步驟:
第 1 步:創建一個新的 word 文檔并添加新的部分。
Document doc = new Document(); Section sec = doc.AddSection();
第 2 步:在 Section[0] 上創建標題。
HeaderFooter header = doc.Sections[0].HeadersFooters.Header;
第 3 步:在頁眉上添加新段落,并將段落的HorizontalAlignment設置為左邊。
Paragraph paragraph = header.AddParagraph(); paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
第 4 步:為段落加載圖像。
DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"1.png"));
第 5 步:添加一個 4 行 2 列的表格。
Table table = header.AddTable(); table.ResetCells(4, 2);
第 6 步:將表格的位置設置在圖像的右側。設置WrapTextAround為true,HorizPositionAbs為外,VertRelationTo為margin,VertPosition為43以適應圖片高度。
table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin; table.TableFormat.Positioning.VertPosition = 43;
第 7 步:然后為表格添加內容,第一列對齊設置為左對齊,第二列對齊設置為右對齊。
String[][] data = { new string[] {"Spire.Doc.left","Spire XLS.right"}, new string[] {"Spire.Presentatio.left","Spire.PDF.right"}, new string[] {"Spire.DataExport.left","Spire.PDFViewe.right"}, new string []{"Spire.DocViewer.left","Spire.BarCode.right"} }; for (int r = 0; r < 4; r++) { TableRow dataRow = table.Rows[r]; for (int c = 0; c < 2; c++) { if (c == 0) { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); par.Format.HorizontalAlignment = HorizontalAlignment.Left; dataRow.Cells[c].Width = 180; } else { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); par.Format.HorizontalAlignment = HorizontalAlignment.Right; dataRow.Cells[c].Width = 180; } } }
第 8 步:保存文件并查看。
table.TableFormat.WrapTextAround = true; table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside; table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin; table.TableFormat.Positioning.VertPosition = 43;
doc.SaveToFile("result.docx", FileFormat.Docx); System.Diagnostics.Process.Start("result.docx");
這是屏幕截圖:
完整代碼:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Drawing; namespace SetPosition { class Program { static void Main(string[] args) { Document doc = new Document(); Section sec = doc.AddSection(); HeaderFooter header = doc.Sections[0].HeadersFooters.Header; Paragraph paragraph = header.AddParagraph(); paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left; DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"1.png")); Table table = header.AddTable(); table.ResetCells(4, 2); table.TableFormat.WrapTextAround = true; table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside; table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin; table.TableFormat.Positioning.VertPosition = 43; String[][] data = { new string[] {"Spire.Doc.left","Spire XLS.right"}, new string[] {"Spire.Presentatio.left","Spire.PDF.right"}, new string[] {"Spire.DataExport.left","Spire.PDFViewe.right"}, new string []{"Spire.DocViewer.left","Spire.BarCode.right"} }; for (int r = 0; r < 4; r++) { TableRow dataRow = table.Rows[r]; for (int c = 0; c < 2; c++) { if (c == 0) { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); par.Format.HorizontalAlignment = HorizontalAlignment.Left; dataRow.Cells[c].Width = 180; } else { Paragraph par = dataRow.Cells[c].AddParagraph(); par.AppendText(data[r][c]); par.Format.HorizontalAlignment = HorizontalAlignment.Right; dataRow.Cells[c].Width = 180; } } } doc.SaveToFile("result.docx",Spire.Doc.FileFormat.Docx); System.Diagnostics.Process.Start("result.docx"); } } }
以上便是如何通過 Spire.Doc 將表格在 Word 文檔中的位置設置為外部,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn