C #/vb. NET Add, read Word footnote/endnote

Source: Internet
Author: User
Tags save file

Objective

Footnotes and endnotes are supplemental notes to the text. Footnotes are generally located at the bottom of the page, and can be used as a comment for a document's content; Endnotes are usually at the end of the document, listing citations, and so on. In this example, you will learn how to add or remove word footnotes.

Tool use

Free Spire. Doc for. NET

First Step DLL Reference

Step two add word footnotes, endnotes

"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) {//Create a new Word Document object and load a Word document that needs to add a footnote endnote            Document document = new document (); Document.            LoadFromFile ("Sample.docx", fileformat.docx2010); Gets the 3rd paragraph of Paragraph Paragraph = document. Sections[0].            PARAGRAPHS[2]; Add footnote Footnote Footnote = paragraph.            Appendfootnote (Footnotetype.footnote);            Finds the specified string in the first paragraph and adds a 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 textr                    Ange if (TextRange.Text = = "China-ASEAN FTA Framework") {//for adding footnotes to the string setting plusCoarse format textRange.CharacterFormat.Bold = true; Insert footnote paragraph.                        Childobjects.insert (i + 1, footnote);                    Break }}}//Add footer content is formatted TextRange text = footnote. Textbody.addparagraph (). AppendText ("November 4, 2002, Premier Zhu Rongji and the leaders of 10 ASEAN countries signed the" China-ASEAN Comprehensive Economic Cooperation Framework Agreement ", which marks the economic and trade cooperation between China and ASEAN has entered a new historical stage.            "); 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; Gets the 5th paragraph Paragraph paragraph2 = document. Sections[0].            PARAGRAPHS[4]; Add endnotes and set endnotes and formats Footnote endnote = Paragraph2. AppendFootnote (Footnotetype.endnote); TextRange Text2 = Endnote. Textbody.addparagraph (). AppendText ("The 17 major report of the party clearly stated:" Adhere to the opening-up of the national policy, the ' introduction ' and ' going out ' to better combine, expand the open field, optimize the open structure, improve the open quality, improve the internal and external linkage, mutual benefit and win, safe and efficient open economy system, To form the new advantages of participating in international economic cooperation and competition under the condition of economic globalization.            "); 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; Save and open documents document.            SaveToFile ("Add footnote endnote. docx", fileformat.docx2010);        System.Diagnostics.Process.Start ("Add footnote endnote. docx"); }    }}

Test results:

"VB.net"

Imports spire.docimports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports system.drawingnamespace insertfootnote_ Doc Class program Private Shared Sub Main (ByVal args as String ()) Dim document as document = New DOCU ment () document. LoadFromFile ("Sample.docx", fileformat.docx2010) Dim paragraph as paragraph = document. Sections (0). Paragraphs (2) Dim footnote as footnote = paragraph. Appendfootnote (footnotetype.footnote) Dim obj as DocumentObject = Nothing for i as Integer = 0 to PA Ragraph. Childobjects.count-1 obj = paragraph. Childobjects (i) If obj. Documentobjecttype = Documentobjecttype.textrange then Dim TextRange as TextRange = TryCast (obj, TextRa nge) If TextRange.Text = "China-ASEAN FTA Framework" then TextRange.CharacterFormat.Bold = Tru e paragraph. Childobjects.insert (i + 1, footnote) ExiT for End If End if Next Dim text as TextRange = footnote. Textbody.addparagraph (). AppendText ("November 4, 2002, Premier Zhu Rongji and the leaders of 10 ASEAN countries signed the" China-ASEAN Comprehensive Economic Cooperation Framework Agreement ", which marks the economic and trade cooperation between China and ASEAN has entered a new historical stage. ") text. Characterformat.fontname = "Arial Black" text. Characterformat.fontsize = 9 text. Characterformat.textcolor = Color.darkgray footnote. Markercharacterformat.fontname = "Calibri" footnote. Markercharacterformat.fontsize = footnote. Markercharacterformat.bold = True footnote. Markercharacterformat.textcolor = Color.darkgreen Dim paragraph2 as Paragraph = document. Sections (0). Paragraphs (4) Dim endnote as Footnote = Paragraph2. Appendfootnote (footnotetype.endnote) Dim text2 as TextRange = Endnote. Textbody.addparagraph (). AppendText ("The 17 major report of the party clearly stated:" Adhere to the opening-up of the national policy, the ' introduction ' and ' going out ' to better combine, expand the open field, optimize the open structure, improve the open quality, improve the internal and external linkage, mutual benefit and win, safe and efficient open economy system, To form the new advantages of participating in international economic cooperation and competition under the condition of economic globalization.         ")   Text2. Characterformat.fontname = "Arial Black" Text2. Characterformat.fontsize = 9 Text2. Characterformat.textcolor = Color.Black Endnote. Markercharacterformat.fontname = "Calibri" endnote. Markercharacterformat.fontsize = Endnote. Markercharacterformat.bold = False Endnote. Markercharacterformat.textcolor = Color.darkgreen document.    SaveToFile ("Add footnote endnote. docx", fileformat.docx2010) System.Diagnostics.Process.Start ("Add footnote endnote. docx") End Sub End Classend Namespace
The third step reads the footer endnote

"C #"

Using spire.doc;using spire.doc.documents;using spire.doc.fields;using system.io;using System.Text;namespace Readfootnote_doc{class Program {static void Main (string[] args) {//Create document class object, load required test            Document file = new document (); Document.            LoadFromFile ("Add footnote endnote. docx"); Gets the document first section part section = document.            Sections[0];            Instantiate the StringBuilder class StringBuilder SB = new StringBuilder (); Traverses all paragraphs in the document 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) {//If you need to read the endnote, change the footnotetype.footnote here to Footnotetype.endno Te can be if (pbase as Footnote). Footnotetype = = Footnotetype.fOotnote) {foreach (Paragraph Footpara in (pbase as Footnote). textbody.paragraphs) {sb.                            Append (Footpara.text); }}}}}//write read content to text and save FILE.WR Itealltext ("FootNotes.txt", sb.)            ToString ());        Open Document System.Diagnostics.Process.Start ("FootNotes.txt"); }    }}

To read a footnote:

Read endnote:

"VB.net"

Imports spire.docimports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports system.ioimports system.textnamespace Readfootnote_doc Class program Private Shared Sub Main (ByVal args as String ()) Dim document as Docume NT = New document () document. LoadFromFile ("Add footnote endnote. docx") Dim sections as section = document. Sections (0) Dim sb as StringBuilder = New StringBuilder () for each paragraph as paragraph in section . Paragraphs while I < CNT Dim pbase as Paragraphbase = TryCast (paragraph. Childobjects (i), paragraphbase) if TypeOf pbase is Footnote then If (TryCast (PBAs E, Footnote)). Footnotetype = Footnotetype.footnote Then for each Footpara as Paragraph in (TryCast (PBase, Foot Note)). Textbody.paragraphs sb.                 Append (footpara.text) Next End If   End If i + = 1 End while Next file.writealltext ("FootNotes.txt", Sb. ToString ()) System.Diagnostics.Process.Start ("FootNotes.txt") End Sub End Classend Namespace

The end of this article

C #/vb. NET Add, read Word footnote/endnote

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.