翻譯|使用教程|編輯:顏馨|2023-05-12 10:24:23.760|閱讀 156 次
概述:本章介紹如何在 Word 中插入或刪除文本框,歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發(fā),不依賴第三方軟件,不受其他國家的技術或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
技術交流Q群(767755948)
文本框是用于存儲文本或圖像的可移動、可調整大小的容器。在 Word 文檔中,可以將文本框作為邊欄插入,或者將焦點放在某些重要文本(如標題或引號)上。有時,您可能還需要刪除一些錯誤添加的文本框。在本文中,您將學習如何使用 Spire.Doc for .NET 以編程方式在 Word 文檔中插入或刪除文本框。
首先,您需要將 Spire.Doc for .NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。
PM> Install-Package Spire.Doc
Spire.Doc for .NET 提供了 Paragraph.AppendTextBox(float width, float height) 方法來在指定段落中插入文本框。插入文本框后,Spire.Doc for .NET 還提供 TextBox 類,供用戶通過設置文本框的屬性(如格式、正文等)來設置文本框的格式。具體步驟如下。
【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
Spire.Doc for .NET 提供了文檔。TextBoxes.RemoveAt(int index) 方法刪除指定的文本框。如果要從 Word 文檔中刪除所有文本框,可以使用 Document.TextBoxes.Clear() 方法。下面的示例演示如何從 Word 文檔中刪除第一個文本框。
【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 中插入或刪除文本框的教程,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產(chǎn)品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn