本篇文章主要探討使用C#或VB.NET給Word文檔添加/撤銷(xiāo)書(shū)簽的方法。文中使用代碼講解,有需要的朋友可以參考一下,跟隨小編一起來(lái)看解決步驟吧。
創(chuàng)新互聯(lián)公司專注于五華企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,電子商務(wù)商城網(wǎng)站建設(shè)。五華網(wǎng)站建設(shè)公司,為五華等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)步驟一:初始化Document實(shí)例并加載Word文檔
Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\中國(guó)夢(mèng).docx ");
步驟二:于第七段末和第八段間插入書(shū)簽,命名書(shū)簽為“C#.bookmark”
Section section = document.Sections[0]; section.Paragraphs[7].AppendBookmarkStart("C#.bookmark"); section.Paragraphs[8].AppendBookmarkEnd("C#.bookmark ");
步驟三:保存文件
document.SaveToFile("Bookmark.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Bookmark.docx");
完成后以上步驟后,文檔中查找定位即可,文檔自動(dòng)定位到當(dāng)前所設(shè)書(shū)簽位置。
以上簡(jiǎn)單三個(gè)步驟即可完成對(duì)word文檔書(shū)簽插入。
完整代碼如下,供參考:
C#
using System;using Spire.Doc;using Spire.Doc.Documents;namespace WordBookmark { class Bookmark { static void Main(string[] args) { //Load Document Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\中國(guó)夢(mèng).docx "); //Insert Bookmark Section section = document.Sections[0]; section.Paragraphs[7].AppendBookmarkStart(".NETFramework"); section.Paragraphs[8].AppendBookmarkEnd(".NETFramework"); //Save and Launch document.SaveToFile("Bookmark.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Bookmark.docx"); } } }
VB.NET:
Imports System Imports Spire.Doc Imports Spire.Doc.Documents Namespace WordBookmark Class Bookmark Private Shared Sub Main(ByVal args() As String) 'Load Document Dim document As Document = New Document document.LoadFromFile("C:\Users\Administrator\Desktop\中國(guó)夢(mèng).docx ") 'Insert Bookmark Dim section As Section = document.Sections(0) section.Paragraphs(7).AppendBookmarkStart(".NETFramework") section.Paragraphs(8).AppendBookmarkEnd(".NETFramework") 'Save and Launch document.SaveToFile("Bookmark.docx", FileFormat.Docx) System.Diagnostics.Process.Start("Bookmark.docx") End Sub End Class End Namespace
同樣的,撤銷(xiāo)書(shū)簽也可以參考執(zhí)行我下面的操作
步驟一:加載需要撤銷(xiāo)書(shū)簽的Word文檔
Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\中國(guó)夢(mèng)(書(shū)簽).docx");
步驟二:撤銷(xiāo)已有書(shū)簽
doc.Bookmarks.RemoveAt(0);
步驟三:保存文件
doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Remove Bookmark.docx");
撤銷(xiāo)書(shū)簽后,得到以下文檔效果
如圖,原本插入書(shū)簽的段落已撤銷(xiāo)書(shū)簽
完整代碼如下
C#:
using Spire.Doc;namespace Removing { class Program { static void Main(string[] args) { //Load Document Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\中國(guó)夢(mèng)(書(shū)簽).docx "); //Remove Bookmark doc.Bookmarks.RemoveAt(0); //Save and Launch doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Remove Bookmark.docx"); } } }
VB.NET:
Imports Spire.Doc Namespace Removing Class Program Private Shared Sub Main(ByVal args() As String) 'Load Document Dim doc As Document = New Document doc.LoadFromFile("C:\Users\Administrator\Desktop\中國(guó)夢(mèng)(書(shū)簽).docx ") 'Remove Bookmark doc.Bookmarks.RemoveAt(0) 'Save and Launch doc.SaveToFile("Remove Bookmark.docx", FileFormat.Docx) System.Diagnostics.Process.Start("Remove Bookmark.docx") End Sub End Class End Namespace
看完上述內(nèi)容,你們掌握使用C#或VB.NET給Word文檔添加/撤銷(xiāo)書(shū)簽的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁(yè)名稱:如何使用C#或VB.NET給Word文檔添加/撤銷(xiāo)書(shū)簽-創(chuàng)新互聯(lián)
文章地址:http://www.chinadenli.net/article6/dhsiog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、服務(wù)器托管、搜索引擎優(yōu)化、網(wǎng)站改版、外貿(mào)建站、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容