Word控件Spire.Doc 【文本框】教程(3):如何在 Word 中插入或删除文本框

news2024/10/5 14:27:43

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

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

Spire.Doc for.NET 最新下载(qun:767755948)icon-default.png?t=N3I4https://www.evget.com/product/3368/download

文本框是用于存储文本或图像的可移动、可调整大小的容器。在 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 中插入或删除文本框的教程,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/517208.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

基于VC + MSSQL实现的县级医院医学影像PACS

一、概述: 基于VC MSSQL实现的一套三甲医院医学影像PACS源码,集成3D后处理功能,包括三维多平面重建、三维容积重建、三维表面重建、三维虚拟内窥镜、最大/小密度投影、心脏动脉钙化分析等功能。 二、医学影像PACS实现功能: 1、…

漏洞分析丨CVE-2012-1873

一、漏洞简述 cve-2012-1873同样是一个著名的堆溢出漏洞,他是IE6-8中MSHTL.dll中的CTableLayout::CalculateMinMax函数里,程序在执行时会以HTML代码中的元素span属性作为循环控制次数向堆中写入数据。第一次会优先根据span申请堆空间,当我们…

【企业信息化】第3集 免费开源ERP: Odoo 16 POS终端管理系统

文章目录 前言一、概览二、硬件三、使用功能 前言 世界排名第一的免费开源ERP: Odoo 16 POS终端管理系统。几分钟内完成设置,几秒内完成销售。 一、概览 Odoo POS 基于智能界面,任何零售公司均可毫不费力地使用 因为其极具灵活性,您可配置 …

2023最全 Java 高频面试合集,掌握这些你也能进大厂!

进大厂是大部分程序员的梦想,而进大厂的门槛也是比较高的,所以这里整理了一份阿里、美团、滴滴、头条等大厂面试大全,对于 Java 后端的朋友来说应该是最全面最完整的面试备战仓库,为了更好地整理每个模块,我也参考了很…

版本控制器git

目录 一、版本控制系统 二、工作流程和使用命令 (1)工作流程 (2)一次完整流程的相关命令 1.初始化1个空的本地仓库 2.克隆方式1个远程仓库到本地仓库 3.新文件添加到暂存区 4.查看仓库状态,显示有变更的文件 5…

Java实现杨辉三角

1 问题 实现杨辉三角。 2 方法 public class textttt01 { public static void main(String[] args) { //定义了一个长度为10&#xff0c;高度为10的二维数组&#xff0c;数组中的值都为0&#xff1b; int[][] arrnew int[10][10]; for (int i0;i<ar…

开源轻量级 IM 框架 MobileIMSDK 的Uniapp客户端库已发布

一、基本介绍 MobileIMSDK-Uniapp端是一套基于Uniapp跨端框架的即时通讯库&#xff1a; 1&#xff09;超轻量级、无任何第3方库依赖&#xff08;开箱即用&#xff09;&#xff1b;2&#xff09;纯JS编写、ES6语法、高度提炼&#xff0c;简单易用&#xff1b;3&#xff09;基于…

html实现经典捕鱼达人小游戏

文章目录 1.设计来源1.1 游戏界面 2.效果和源码2.1 动态效果2.2 源代码 源码下载 作者&#xff1a;xcLeigh 文章地址&#xff1a;https://blog.csdn.net/weixin_43151418/article/details/130638634 html实现经典捕鱼达人小游戏源码 &#xff0c;99.99%的还原实物&#xff0c;起…

【办公】解决京瓷打印机总是出现烦人的“在手送纸盘中装纸 彩色纸”的问题

问题 打印机是日常办公的常见工具&#xff0c;京瓷是著名的打印机品牌&#xff0c;而且是很多事业单位首选的打印机品牌。然而在日常使用中京瓷打印机总是会出现烦人的“在手送纸盘中装纸 彩色纸”的问题&#xff0c;如下图所示&#xff1a; 一旦出现该问题&#xff0c;就需要…

给XZZ准备的小攻略(私人向)

定时发送邮件功能&#xff1a; 定时发送邮件的功能位于 homework 的 views.py 中 使用的模块是 apscheduler &#xff08;我读作ap司改就&#xff09; 准备的部分&#xff1a;&#xff08;了解即可&#xff09; 安装好 django-apscheduler 后&#xff0c;在 setting.py 中添…

python 调用golang 注意事项

1.调用编译后的动态库文件&#xff0c;报头文件错误 原因&#xff1a; 不同平台下编译的add.so 不能通用&#xff0c;Windows下可以运行的so文件&#xff0c;linux下就不能运行&#xff0c;需要重新编译linux的so文件&#xff1b; 该报错可能就是跨平台使用动态库文件了&…

yolov5爬坑小作文

第一坑 做完训练集&#xff0c;配置要yaml文件后&#xff0c;笔者启动了训练命令 python train.py --data 我的yaml位置 --batch-size 我的每次进行一次反向传播之前需要前向计算的图片张数 --device 我的GPU编号 之后报错 OSError: [WinError 1455] 页面文件太小,无法完成…

检测数据类型

//typeof() 对于基本数据类型没问题&#xff0c;遇到引用数据类型不管用 console.log(typeof 666) //number console.log(typeof [1,2,3]) //object //instanceof() 只能判断引用数据类型&#xff0c;不能判断基本数据类型 console.log( [] instanceof Array) //true …

Acunetix 15.6 (Linux, Windows) - Web 应用程序安全测试

Acunetix 15.6 (Linux, Windows) - Web 应用程序安全测试 请访问原文链接&#xff1a;https://sysin.org/blog/acunetix-15/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org Acunetix 漏洞扫描器&#xff0c;管理您的网络安全。…

电脑待机怎么设置?分享4个宝藏方法!

案例&#xff1a;电脑待机怎么设置 【有时候我使用电脑时可能因为各种事而被打断&#xff0c;但是不是很想让电脑关机&#xff0c;请问大家电脑待机时间应该怎么设置呀&#xff1f;】 有时候我们在使用电脑时可能需要做别的一些事&#xff0c;这时我们的电脑会进入待机状态。…

裸奔时代,隐私何处寻?

随着互联网的普及&#xff0c;人工智能时代的大幕初启&#xff0c;数据作为人工智能的重要支撑&#xff0c;数据之争成为“兵家必争之地”&#xff0c;随之而来的就是&#xff0c;各种花式手段“收割”个人信息&#xff0c;用户隐私暴露程度越来越高&#xff0c;隐私保护早已成…

空间权重矩阵与相关性检验(Stata)

空间权重矩阵与相关性检验(Stata) 文章目录 空间权重矩阵与相关性检验(Stata)[toc]1 空间相关性检验1.1 全局空间相关性检验1.2 局部空间自相关检验1.3 散点图 2 权重矩阵2.1 截断距离权重矩阵2.2 反距离权重矩阵 1 空间相关性检验 cd "D:\Allcode\Stata\Spatial-Econome…

如何在知行之桥EDI系统中修改ICN?

EDI电子数据交换中的ICN是什么&#xff1f; 在EDI术语中&#xff0c;ICN# 的全称为Interchange Control Number&#xff0c;是文件的发送方分配的唯一标识符&#xff0c;可以识别每一个文件。 我们常见的符合X12和EDIFACT报文标准的文件中&#xff0c;ICN#分别指的是ISA13和U…

泼辣修图app下载2024最新版修图滤镜

泼辣修图专业版是一款强大的专业修图软件&#xff0c;拥有上百款调色工具还有丰富的图层素材&#xff0c; 更有智能的人像修饰面板&#xff0c;具备物体识别的智能蒙板&#xff0c;高效的滤镜管理系统和强大的文字工具&#xff0c;支持批量处理。一切围绕摄影&#xff0c;无论是…

如何在 Windows 10 上安装 WSL

第 1 步&#xff0c;启用 WSL 不管您想要使用哪个版本的 WSL&#xff0c;都首先需要启用它。为此&#xff0c;请以管理员身份打开 PowerShell 工具并运行以下命令。小心不要在命令中输入错误或遗漏任何字符&#xff1a; dism.exe /online /enable-feature /featurename:Micro…