Word控件Spire.Doc 【文本】教程(13) ;如何在 C#、VB.NET 中的确切位置将文本插入 Word

在本文中,我们将介绍如何在 C#、VB.NET 中通过文本框在 Word 中定位文本

我们的一位客户要求将文本插入到 Word 文档的准确位置(水平和垂直坐标)。通常,人们使用表格和文本框等其他工具在 Word 文档中定位文本,因为表格和文本框的定位更容易控制。在本文中,我们将介绍如何在 C#、VB.NET 中通过文本框在 Word 中定位文本

首先下载 Spire.Doc for .NET 并将 dll 文件引用到您的项目中。在开始编码之前,我们还需要额外添加以下命名空间。

using System.Drawing;using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using Spire.Doc.Formatting;

代码片段:

第 1 步:创建一个新的 Word 文档,为其添加一个节,然后在该节上添加一个段落。

Document doc = new Document();Section sec = doc.AddSection();Paragraph par = sec.AddParagraph();

第 2 步:在段落中添加一个新的文本框。这里我们在逻辑上将VerticalOriginHorizo ntalOrigin 设置为Margin因为水平和垂直坐标是相对于他的参考对象的值。通过给变量VerticalPositionHorizontalPosition一个特定的值,文本框将固定在该位置。

TextBox textBox = par.AppendTextBox(180, 30);textBox.Format.VerticalOrigin = VerticalOrigin.Margin;textBox.Format.VerticalPosition = 100;textBox.Format.HorizontalOrigin = HorizontalOrigin.Margin;textBox.Format.HorizontalPosition = 50;textBox.Format.NoLine = true;

第 3 步:定义新的格式样式。

CharacterFormat format = new CharacterFormat(doc);format.FontName = "Calibri";format.FontSize = 15;format.Bold = true;

第 4 步:将文本添加到文本框中,并将预设格式应用于文本。

Paragraph par1 = textBox.Body.AddParagraph();par1.AppendText("This is my new string").ApplyCharacterFormat(format);

第 5 步:保存文件。

doc.SaveToFile("result.docx", FileFormat.Docx);

结果

文本框的边框线已设置为不可见,因此我们只能看到在指定的水平和垂直坐标处添加的文本。

如何在 C#、VB.NET 中的确切位置将文本插入 Word

完整代码

[C#]

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using Spire.Doc.Formatting;using System.Collections.Generic;namespace InsertText{class Program{static void Main(string[] args){Document doc = new Document();Section sec = doc.AddSection();Paragraph par = sec.AddParagraph();TextBox textBox = par.AppendTextBox(180, 30);textBox.Format.VerticalOrigin = VerticalOrigin.Margin;textBox.Format.VerticalPosition = 100;textBox.Format.HorizontalOrigin = HorizontalOrigin.Margin;textBox.Format.HorizontalPosition = 50;textBox.Format.NoLine = true;CharacterFormat format = new CharacterFormat(doc);format.FontName = "Calibri";format.FontSize = 15;format.Bold = true;Paragraph par1 = textBox.Body.AddParagraph();par1.AppendText("This is my new string").ApplyCharacterFormat(format);doc.SaveToFile("result.docx", FileFormat.Docx);}}}

[VB.NET]

Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports Spire.Doc.FormattingImports System.Collections.GenericNamespace InsertTextClass ProgramPrivate Shared Sub Main(args As String())Dim doc As New Document()Dim sec As Section = doc.AddSection()Dim par As Paragraph = sec.AddParagraph()Dim textBox As TextBox = par.AppendTextBox(180, 30)textBox.Format.VerticalOrigin = VerticalOrigin.MargintextBox.Format.VerticalPosition = 100textBox.Format.HorizontalOrigin = HorizontalOrigin.MargintextBox.Format.HorizontalPosition = 50textBox.Format.NoLine = TrueDim format As New CharacterFormat(doc)format.FontName = "Calibri"format.FontSize = 15format.Bold = TrueDim par1 As Paragraph = textBox.Body.AddParagraph()par1.AppendText("This is my new string").ApplyCharacterFormat(format)doc.SaveToFile("result.docx", FileFormat.Docx)End SubEnd ClassEnd Namespace


欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年7月9日
下一篇 2022年7月9日

相关推荐

发表回复

登录后才能评论