Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
Spire.Doc for.NET 最新下载(qun:767755948)https://www.evget.com/product/3368/download
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 文档中的位置设置为外部,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。