C#添加、讀取Word腳注尾注的方法
本文實(shí)例為大家分享了C#添加讀取Word腳注尾注的具體代碼,供大家參考,具體內(nèi)容如下
腳注和尾注是對(duì)文本的補(bǔ)充說(shuō)明。腳注一般位于頁(yè)面的底部,可以作為文檔某處內(nèi)容的注釋?zhuān)晃沧⒁话阄挥谖臋n的末尾,列出引文 的出處等。在本示例中將介紹如何來(lái)添加或刪除Word腳注。
工具使用: Free Spire. Doc for .NET(免費(fèi)版)
第一步:dll引用
第二步:添加Word腳注、尾注
【C#】
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace InsertFootnote_Doc { class Program { static void Main(string[] args) { //新建一個(gè)word文檔對(duì)象并加載需要添加腳注尾注的word文檔 Document document = new Document(); document.LoadFromFile("sample.docx", FileFormat.Docx2010); //獲取第3個(gè)段落 Paragraph paragraph = document.Sections[0].Paragraphs[2]; //添加腳注 Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote); //在第一段里查找指定字符串,并添加腳注 DocumentObject obj = null; for (int i = 0; i < paragraph.ChildObjects.Count; i++) { obj = paragraph.ChildObjects[i]; if (obj.DocumentObjectType == DocumentObjectType.TextRange) { TextRange textRange = obj as TextRange; if (textRange.Text == "中國(guó)——東盟自貿(mào)區(qū)框架") { //為添加腳注的字符串設(shè)置加粗格式 textRange.CharacterFormat.Bold = true; //插入腳注 paragraph.ChildObjects.Insert(i + 1, footnote); break; } } } //添加腳注內(nèi)容被設(shè)置字體格式 TextRange text = footnote.TextBody.AddParagraph().AppendText("2002年11月4日,朱镕基總理和東盟10國(guó)領(lǐng)導(dǎo)人共同簽署了《中國(guó)-東盟全面經(jīng)濟(jì)合作框架協(xié)議》,這標(biāo)志著中國(guó)與東盟的經(jīng)貿(mào)合作進(jìn)入了一個(gè)新的歷史階段。"); text.CharacterFormat.FontName = "Arial Black"; text.CharacterFormat.FontSize = 9; text.CharacterFormat.TextColor = Color.DarkGray; footnote.MarkerCharacterFormat.FontName = "Calibri"; footnote.MarkerCharacterFormat.FontSize = 12; footnote.MarkerCharacterFormat.Bold = true; footnote.MarkerCharacterFormat.TextColor = Color.DarkGreen; //獲取第5段落 Paragraph paragraph2 = document.Sections[0].Paragraphs[4]; //添加尾注并設(shè)置尾注和格式 Footnote endnote = paragraph2.AppendFootnote(FootnoteType.Endnote); TextRange text2 = endnote.TextBody.AddParagraph().AppendText("黨的十七大報(bào)告明確指出:" +"“堅(jiān)持對(duì)外開(kāi)放的基本國(guó)策,把‘引進(jìn)來(lái)'和‘走出去'更好地結(jié)合起來(lái)," +"擴(kuò)大開(kāi)放領(lǐng)域,優(yōu)化開(kāi)放結(jié)構(gòu),提高開(kāi)放質(zhì)量,完善內(nèi)外聯(lián)動(dòng)," +"互利共贏(yíng)、安全高效的開(kāi)放型經(jīng)濟(jì)體系,形成經(jīng)濟(jì)全球化條件下參與國(guó)際經(jīng)濟(jì)合作和競(jìng)爭(zhēng)的新優(yōu)勢(shì)。"); text2.CharacterFormat.FontName = "Arial Black"; text2.CharacterFormat.FontSize = 9; text2.CharacterFormat.TextColor = Color.Black; endnote.MarkerCharacterFormat.FontName = "Calibri"; endnote.MarkerCharacterFormat.FontSize = 12; endnote.MarkerCharacterFormat.Bold = false; endnote.MarkerCharacterFormat.TextColor = Color.DarkGreen; //保存并打開(kāi)文檔 document.SaveToFile("添加腳注尾注.docx", FileFormat.Docx2010); System.Diagnostics.Process.Start("添加腳注尾注.docx"); } } }
測(cè)試結(jié)果:
第三步 :讀取腳注/尾注
【C#】
//創(chuàng)建Document類(lèi)對(duì)象,加載需要測(cè)試的文檔 Document document = new Document(); document.LoadFromFile("添加腳注尾注.docx"); //獲取文檔第一個(gè)section Section section = document.Sections[0]; //實(shí)例化StringBuilder類(lèi) StringBuilder sb = new StringBuilder(); //遍歷文檔中所有段落 foreach (Paragraph paragraph in section.Paragraphs) { for (int i = 0, cnt = paragraph.ChildObjects.Count; i < cnt; i++) { ParagraphBase pBase = paragraph.ChildObjects[i] as ParagraphBase; if (pBase is Footnote) { //若需要讀取尾注,將此處FootnoteType.Footnote改成 FootnoteType.Endnote即可 if ((pBase as Footnote).FootnoteType == FootnoteType.Footnote) { foreach (Paragraph footPara in (pBase as Footnote).TextBody.Paragraphs) { sb.Append(footPara.Text); } } } } } //將讀取內(nèi)容寫(xiě)入文本并保存 File.WriteAllText("FootNotes.txt", sb.ToString()); //打開(kāi)文檔 System.Diagnostics.Process.Start("FootNotes.txt");
讀取結(jié)果:
腳注讀取結(jié)果:
尾注讀取結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#設(shè)置程序開(kāi)機(jī)啟動(dòng)的實(shí)現(xiàn)示例
本文主要介紹了C#設(shè)置程序開(kāi)機(jī)啟動(dòng)的實(shí)現(xiàn)示例,可以通過(guò)修改注冊(cè)表將啟動(dòng)信息寫(xiě)入注冊(cè)表來(lái)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01基于C#實(shí)現(xiàn)電腦系統(tǒng)掛機(jī)鎖
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)電腦系統(tǒng)掛機(jī)鎖,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C#訪(fǎng)問(wèn)PostGreSQL數(shù)據(jù)庫(kù)的方法
這次的項(xiàng)目中的一個(gè)環(huán)節(jié)要求我把PostGreSQL數(shù)據(jù)取出來(lái),然后放到SqlServer里,再去處理分析。2013-04-04