翻譯|使用教程|編輯:胡濤|2023-02-01 10:36:55.933|閱讀 160 次
概述:本文介紹如何通過 Spire.Doc 將表格在 Word 文檔中的位置設(shè)置為外部,歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進(jìn)行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
word文檔中的表格可以讓你的數(shù)據(jù)更有條理,更整潔,這篇文章講的是通過Spire.Doc在word文檔中設(shè)置表格的絕對位置。這里嘗試實(shí)現(xiàn)在header上的圖片右側(cè)放置一個table。
以下是步驟:
第 1 步:創(chuàng)建一個新的 word 文檔并添加新的部分。
Document doc = new Document(); Section sec = doc.AddSection();
第 2 步:在 Section[0] 上創(chuàng)建標(biāo)題。
HeaderFooter header = doc.Sections[0].HeadersFooters.Header;
第 3 步:在頁眉上添加新段落,并將段落的HorizontalAlignment設(shè)置為左邊。
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 步:將表格的位置設(shè)置在圖像的右側(cè)。設(shè)置WrapTextAround為true,HorizPositionAbs為外,VertRelationTo為margin,VertPosition為43以適應(yīng)圖片高度。
table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin; table.TableFormat.Positioning.VertPosition = 43;
第 7 步:然后為表格添加內(nèi)容,第一列對齊設(shè)置為左對齊,第二列對齊設(shè)置為右對齊。
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 文檔中的位置設(shè)置為外部,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn