文檔金喜正規買球>>Spire.Doc系列教程>>Word .NET庫組件Spire.Doc系列教程(39):在 Word 中創建文本框
Word .NET庫組件Spire.Doc系列教程(39):在 Word 中創建文本框
Spire.Doc for .NET是一個專業的Word .NET庫,設計用于幫助開發人員高效地開發創建、閱讀、編寫、轉換和打印任何來自.NET( C#, VB.NET, ASP.NET)平臺的Word文檔文件的功能。
本系列教程將為大家帶來Spire.Doc for .NET在使用過程中的各類實際操作,word文檔中經常會使用腳注和尾注來為文檔添加說明。本文主要描述如何使用C#在word中創建文本框。
11月優惠進行時,消費滿額即享折上豪禮,想買Spire.Doc的朋友趕快吧!
推薦閱讀:【想要快速完成文檔格式轉換嗎?Spire系列組件格式轉換完整攻略來啦!】
C# 在 Word 中創建文本框
創建只含文字的文本框
//實例化Document對象 Document doc = new Document(); //添加section和段落 Section section = doc.AddSection(); Paragraph paragraph = section.AddParagraph(); //在段落上添加文本框 TextBox tb = paragraph.AppendTextBox(120, 50); //設置文本框相對頁邊距的位置 tb.Format.HorizontalOrigin = HorizontalOrigin.Margin; tb.Format.HorizontalPosition = 0; tb.Format.VerticalOrigin = VerticalOrigin.Margin; tb.Format.VerticalPosition = 50; //設置文本框填充色、邊框顏色及樣式 tb.Format.LineColor = Color.DarkBlue; tb.Format.LineStyle = TextBoxLineStyle.Simple; tb.Format.FillColor = Color.LightGreen; //在文本框中添加段落及文字 Paragraph para = tb.Body.AddParagraph(); TextRange tr = para.AppendText("Spire.Doc是一款用于處理Word文檔的.NET組件"); //設置文字格式 tr.CharacterFormat.FontName = "黑體"; tr.CharacterFormat.FontSize = 10; tr.CharacterFormat.TextColor = Color.Black; //設置段落對齊方式 para.Format.HorizontalAlignment = HorizontalAlignment.Left; //保存文檔 doc.SaveToFile("添加文本框.docx", FileFormat.Docx);
在文本框中同時添加圖片和文字
//實例化Document對象 Document doc = new Document(); //添加section和段落 Section section = doc.AddSection(); Paragraph paragraph = section.AddParagraph(); //在段落上添加文本框 TextBox tb = paragraph.AppendTextBox(140, 250); //設置文本框相對頁邊距的位置 tb.Format.HorizontalOrigin = HorizontalOrigin.Margin; tb.Format.HorizontalPosition = 0; tb.Format.VerticalOrigin = VerticalOrigin.Margin; tb.Format.VerticalPosition = 20; //在文本框中添加段落一,并在段落一插入圖片 Paragraph para1 = tb.Body.AddParagraph(); Image image = Image.FromFile("hualuogeng.png"); DocPicture picture = para1.AppendPicture(image); //設置段落格式 para1.Format.HorizontalAlignment = HorizontalAlignment.Center; para1.Format.AfterSpacing = 8; //在文本框中添加段落二,添加文本到段落二 Paragraph para2 = tb.Body.AddParagraph(); TextRange textRange = para2.AppendText("華羅庚(1910.11.12—1985.6.12),出生于江蘇常州金壇區,祖籍江蘇丹陽。數學家,中國科學院院士,美國國家科學院外籍院士,第三世界科學院院士,聯邦德國巴伐利亞科學院院士。"); textRange.CharacterFormat.FontName = "黑體"; textRange.CharacterFormat.FontSize = 9; //設置段落格式 para2.Format.HorizontalAlignment = HorizontalAlignment.Left; para2.Format.LineSpacing = 15; //保存文檔 doc.SaveToFile("插入圖片及文字.docx", FileFormat.Docx2013);