admin 管理员组

文章数量: 1184232

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

文本框是用于存储文本或图像的可移动、可调整大小的容器。在 Word 文档中,可以将文本框作为边栏插入,或者将焦点放在某些重要文本(如标题或引号)上。有时,您可能还需要删除一些错误添加的文本框。在本文中,您将学习如何使用 Spire.Doc for .NET 以编程方式 Word 文档中插入或删除文本框。

安装 Spire.Doc for .NET

首先,您需要将 Spire.Doc for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。

PM> Install-Package Spire.Doc

在 Word 文档中插入文本框

Spire.Doc for .NET 提供了 Paragraph.AppendTextBox(float width, float height) 方法来在指定段落中插入文本框。插入文本框后,Spire.Doc for .NET 还提供 TextBox 类,供用户通过设置文本框的属性(如格式、正文等)来设置文本框的格式。具体步骤如下。

  • 创建一个 文档 实例,然后使用 Document.LoadFromFile() 方法加载示例 Word 文档。
  • 使用 Document.Section[] 属性获取第一节,然后使用 Section.AddParagraph() 方法向该节添加一个段落。
  • 使用 Paragraph.AppendTextBox(浮点宽度、浮点高度) 方法向段落添加一个文本框。
  • 使用 TextBox.Format 属性获取文本框的格式,然后使用 TextBoxFormat 类的属性设置文本框的环绕类型、位置、边框颜色和填充颜色。
  • 使用文本框向 文本框 添加段落。 Body.AddParagraph() 方法,然后设置其对齐方式。
  • 使用 Paragraph.AppendPicture() 方法将图像插入段落,然后设置插入图像的大小。
  • 使用 Paragraph.AppendText() 方法将文本插入文本框,然后设置文本字体。
  • 使用 Document.SaveToFile() 方法将文档保存到另一个文件。

【C#】

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertTextbox
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a sample Word document
document.LoadFromFile("Ralph.docx");
//Insert a textbox and set its wrapping style
TextBox TB = document.Sections[0].AddParagraph().AppendTextBox(130, 320);
TB.Format.TextWrappingStyle = TextWrappingStyle.Square;
//Set the position of the textbox
TB.Format.HorizontalOrigin = HorizontalOrigin.RightMarginArea;
TB.Format.HorizontalPosition = -100;
TB.Format.VerticalOrigin = VerticalOrigin.Page;
TB.Format.VerticalPosition = 130f;
//Set the border style and fill color of the textbox
TB.Format.LineColor = Color.DarkBlue;
TB.Format.FillColor = Color.LightCyan;
//Insert an image to textbox as a paragraph
Paragraph para = TB.Body.AddParagraph();
DocPicture picture = para.AppendPicture(@"C:\Users\Administrator\Desktop\Ralph.jpg");
//Set alignment for the paragraph
para.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Set the size of the inserted image
picture.Height = 90;
picture.Width = 90;
//Insert text to textbox as the second paragraph
TextRange TR = para.AppendText("Emerson is truly the center of the American transcendental movement, "
+ "setting out most of its ideas and values in a little book, Nature, published in 1836, "
+ "that represented at least ten years of intense study in philosophy, religion, and literature.");
//Set alignment for the paragraph
para.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Set the font of the text
TR.CharacterFormat.FontName = "Times New Roman";
TR.CharacterFormat.FontSize = 12;
TR.CharacterFormat.Italic = true;
//Save the result file
document.SaveToFile("AddTextBox.docx", FileFormat.Docx);
}
}
}

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace InsertTextbox
Class Program
Private Shared Sub Main(ByVal args() As String)
'Create a Document instance
Dim document As Document = New Document
'Load a sample Word document
document.LoadFromFile("Ralph.docx")
'Insert a textbox and set its wrapping style
Dim TB As TextBox = document.Sections(0).AddParagraph.AppendTextBox(130, 320)
TB.Format.TextWrappingStyle = TextWrappingStyle.Square
'Set the position of the textbox
TB.Format.HorizontalOrigin = HorizontalOrigin.RightMarginArea
TB.Format.HorizontalPosition = -100
TB.Format.VerticalOrigin = VerticalOrigin.Page
TB.Format.VerticalPosition = 130
'Set the border style and fill color of the textbox
TB.Format.LineColor = Color.DarkBlue
TB.Format.FillColor = Color.LightCyan
'Insert an image to textbox as a paragraph
Dim para As Paragraph = TB.Body.AddParagraph
Dim picture As DocPicture = para.AppendPicture("C:\Users\Administrator\Desktop\Ralph.jpg")
'Set alignment for the paragraph
para.Format.HorizontalAlignment = HorizontalAlignment.Center
'Set the size of the inserted image
picture.Height = 90
picture.Width = 90
'Insert text to textbox as the second paragraph
Dim TR As TextRange = para.AppendText("Emerson is truly the center of the American transcendental movement, " & "setting out most of its ideas and values in a little book, Nature, published in 1836, " & "that represented at least ten years of intense study in philosophy, religion, and literature.")
'Set alignment for the paragraph
para.Format.HorizontalAlignment = HorizontalAlignment.Center
'Set the font of the text
TR.CharacterFormat.FontName = "Times New Roman"
TR.CharacterFormat.FontSize = 12
TR.CharacterFormat.Italic = True
'Save the result file
document.SaveToFile("AddTextBox.docx", FileFormat.Docx)
End Sub
End Class
End Namespace

从 Word 文档中删除文本框

Spire.Doc for .NET 提供了 文档 。TextBoxes.RemoveAt(int index) 方法删除指定的文本框。如果要从 Word 文档中删除所有文本框,可以使用 Document.TextBoxes.Clear() 方法。下面的示例演示如何从 Word 文档中删除第一个文本框。

  • 创建 文档 实例。
  • 使用 Document.LoadFromFile() 方法加载示例 Word 文档。
  • 使用 “文档” 删除第一个文本框 。TextBoxes.RemoveAt(int index) 方法。
  • 使用 Document.SaveToFile() 方法将文档保存到另一个文件。

【C#】

using Spire.Doc;
namespace Removetextbox
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document Doc = new Document();
//Load a sample Word document
Doc.LoadFromFile("TextBox.docx");
//Remove the first text box
Doc.TextBoxes.RemoveAt(0);
//Remove all text boxes
//Doc.TextBoxes.Clear();
//Save the result document
Doc.SaveToFile("RemoveTextbox.docx", FileFormat.Docx);
}
}
}

【VB.NET】

Imports Spire.Doc
Namespace Removetextbox
Class Program
Private Shared Sub Main(ByVal args() As String)
'Create a Document instance
Dim Doc As Document = New Document
'Load a sample Word document
Doc.LoadFromFile("TextBox.docx")
'Remove the first text box
Doc.TextBoxes.RemoveAt(0)
'Remove all text boxes
'Doc.TextBoxes.Clear();
'Save the result document
Doc.SaveToFile("RemoveTextbox.docx", FileFormat.Docx)
End Sub
End Class
End Namespace

以上便是如何在 Word 中插入或删除文本框的教程,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

本文标签: 使用 文档 编程