C#向Word插入排版精良的TextBox
Text Box(文本框)是Word排版的工具之一。在Word文檔正文的任何地方插入文本框,可添加補(bǔ)充信息,放在合適的位置,也不會(huì)影響正文的連續(xù)性。我們可以設(shè)置文本框的大小,線型,內(nèi)部邊距,背景填充等效果。文本框內(nèi)可以圖文混排,設(shè)置字體,字號(hào),圖片大小等。 在日常使用中,我們很容易忽略這些元素,僅僅插入一個(gè)黑色單線,僅含文字的文本框。因而,我覺(jué)得有必要向大家介紹并制作一個(gè)版式精良的文本框,拋磚引玉。
本篇博文主要介紹,如何使用C#在Word文檔的特定位置,插入一個(gè)有圖片填充,內(nèi)部邊距,圖文混排,線型精致的文本框。感興趣的博友請(qǐng)從E-iceblue下載Free Spire.Doc,并添加為Visual Studio引用。
需要用的命名空間:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spire.Doc; using Spire.Doc.Fields; using Spire.Doc.Documents; using System.Drawing;
步驟詳解:
步驟一:加載一個(gè)只含有文本的Word文檔,如下圖。
Document document = new Document();
document.LoadFromFile("李白生平.docx");
步驟二:在加載的Word文檔中添加一個(gè)文本框,并設(shè)定其具體位置。這里需要考慮兩點(diǎn):插入的頁(yè)和頁(yè)面的位置。即:在哪一頁(yè)插入這個(gè)文本框,文本框在該頁(yè)的位置。只有定位好這兩點(diǎn),文本框的位置才能具體確認(rèn)。此外,還需考慮文本框和文本的位置關(guān)系,即設(shè)置位置和自動(dòng)換行(text wrapping)。所以,以下代碼,通過(guò)設(shè)定文本框在哪一段落,相較于頁(yè)面的位置和自動(dòng)換行,來(lái)確定其位置。
TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(150, 300); TB.Format.HorizontalOrigin = HorizontalOrigin.Page; TB.Format.HorizontalPosition = 370; TB.Format.VerticalOrigin = VerticalOrigin.Page; TB.Format.VerticalPosition = 155; TB.Format.TextWrappingStyle = TextWrappingStyle.Square; TB.Format.TextWrappingType = TextWrappingType.Both;
步驟三:設(shè)置文本框框的顏色,內(nèi)部邊距,圖片填充。
TB.Format.LineStyle = TextBoxLineStyle.Double; TB.Format.LineColor = Color.LightGoldenrodYellow; TB.Format.LineDashing = LineDashing.Solid; TB.Format.LineWidth = 3; TB.Format.InternalMargin.Top = 12; TB.Format.InternalMargin.Bottom = 8; TB.Format.InternalMargin.Left = 12; TB.Format.InternalMargin.Right = 12; TB.Format.FillEfects.Type = BackgroundType.Picture; TB.Format.FillEfects.Picture = Image.FromFile("2.jpg");
步驟四:在文本框內(nèi)添加段落文本,圖片,設(shè)置字體,字體顏色,行間距,段后距,對(duì)齊方式等。然后保存文檔,打開(kāi)查看效果。
Paragraph para1 = TB.Body.AddParagraph(); para1.Format.AfterSpacing = 6; para1.Format.HorizontalAlignment = HorizontalAlignment.Center; TextRange TR1 = para1.AppendText("李白"); TR1.CharacterFormat.FontName = "華文新魏"; TR1.CharacterFormat.FontSize = 16; TR1.CharacterFormat.Bold = true; Paragraph para2 = TB.Body.AddParagraph(); Image image = Image.FromFile("李白.jpg"); DocPicture picture = para2.AppendPicture(image); picture.Width = 120; picture.Height = 160; para2.Format.AfterSpacing = 8; para2.Format.HorizontalAlignment = HorizontalAlignment.Center; Paragraph para3 = TB.Body.AddParagraph(); TextRange TR2 = para3.AppendText("盛唐最杰出的詩(shī)人,中國(guó)歷史最偉大的浪漫主義詩(shī)人杜甫贊其文章“筆落驚風(fēng)雨,詩(shī)成泣鬼神”"); TR2.CharacterFormat.FontName = "華文新魏"; TR2.CharacterFormat.FontSize = 11; para3.Format.LineSpacing = 15; para3.Format.HorizontalAlignment = HorizontalAlignment.Left; para3.Format.SuppressAutoHyphens = true; document.SaveToFile("R1.docx"); System.Diagnostics.Process.Start("R1.docx");
效果圖:
完整代碼示例:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spire.Doc; using Spire.Doc.Fields; using Spire.Doc.Documents; using System.Drawing; namespace textbox { class Program { static void Main(string[] args) { Document document = new Document(); document.LoadFromFile("李白生平.docx"); TextBox TB = document.Sections[0].Paragraphs[0].AppendTextBox(150, 300); TB.Format.HorizontalOrigin = HorizontalOrigin.Page; TB.Format.HorizontalPosition = 370; TB.Format.VerticalOrigin = VerticalOrigin.Page; TB.Format.VerticalPosition = 155; TB.Format.TextWrappingStyle = TextWrappingStyle.Square; TB.Format.TextWrappingType = TextWrappingType.Both; TB.Format.LineStyle = TextBoxLineStyle.Double; TB.Format.LineColor = Color.LightGoldenrodYellow; TB.Format.LineDashing = LineDashing.Solid; TB.Format.LineWidth = 3; TB.Format.InternalMargin.Top = 12; TB.Format.InternalMargin.Bottom = 8; TB.Format.InternalMargin.Left = 12; TB.Format.InternalMargin.Right = 12; TB.Format.FillEfects.Type = BackgroundType.Picture; TB.Format.FillEfects.Picture = Image.FromFile("2.jpg"); Paragraph para1 = TB.Body.AddParagraph(); para1.Format.AfterSpacing = 6; para1.Format.HorizontalAlignment = HorizontalAlignment.Center; TextRange TR1 = para1.AppendText("李白"); TR1.CharacterFormat.FontName = "華文新魏"; TR1.CharacterFormat.FontSize = 16; TR1.CharacterFormat.Bold = true; Paragraph para2 = TB.Body.AddParagraph(); Image image = Image.FromFile("李白.jpg"); DocPicture picture = para2.AppendPicture(image); picture.Width = 120; picture.Height = 160; para2.Format.AfterSpacing = 8; para2.Format.HorizontalAlignment = HorizontalAlignment.Center; Paragraph para3 = TB.Body.AddParagraph(); TextRange TR2 = para3.AppendText("盛唐最杰出的詩(shī)人,中國(guó)歷史最偉大的浪漫主義詩(shī)人杜甫贊其文章“筆落驚風(fēng)雨,詩(shī)成泣鬼神”"); TR2.CharacterFormat.FontName = "華文新魏"; TR2.CharacterFormat.FontSize = 11; para3.Format.LineSpacing = 15; para3.Format.HorizontalAlignment = HorizontalAlignment.Left; para3.Format.SuppressAutoHyphens = true; document.SaveToFile("R1.docx"); System.Diagnostics.Process.Start("R1.docx"); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#操作XML通用方法匯總
欄 目:C#教程
下一篇:C# 復(fù)制指定節(jié)點(diǎn)的所有子孫節(jié)點(diǎn)到新建的節(jié)點(diǎn)下
本文標(biāo)題:C#向Word插入排版精良的TextBox
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6231.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10C#實(shí)現(xiàn)簡(jiǎn)單合并word文檔的方法
- 01-10C#簡(jiǎn)單實(shí)現(xiàn)子窗體向父窗體傳值的方法
- 01-10C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
- 01-10C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中
- 01-10C#實(shí)現(xiàn)向多線程傳參的三種方式實(shí)例分析
- 01-10C#編程實(shí)現(xiàn)四舍五入、向上及下取整的方法
- 01-10C#數(shù)據(jù)結(jié)構(gòu)之雙向鏈表(DbLinkList)實(shí)例詳解
- 01-10解析C#面向?qū)ο缶幊讨蟹椒ǎ╩ethod)的使用
- 01-10如何使用C#從word文檔中提取圖片


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文