C # Add, read word footer endnotes

Source: Internet
Author: User

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 (Free edition)

First Step: DLL reference

Step Two: Add word footnotes, endnotes

"C #"

usingSpire.doc;usingSpire.Doc.Documents;usingSpire.Doc.Fields;usingSystem.Drawing;namespaceinsertfootnote_doc{classProgram {Static voidMain (string[] args) {            //Create a new Word Document object and load the Word document that you want to add a footnote endnote toDocument document =NewDocument (); Document. LoadFromFile ("Sample.docx", fileformat.docx2010); //get a 3rd paragraphParagraph Paragraph = document. sections[0]. paragraphs[2]; //Add footnotesFootnote Footnote =paragraph.            Appendfootnote (Footnotetype.footnote); //find the specified string in the first paragraph and add a footnoteDocumentObject obj =NULL;  for(inti =0; I < paragraph. Childobjects.count; i++) {obj=paragraph.                Childobjects[i]; if(obj. Documentobjecttype = =documentobjecttype.textrange) {TextRange TextRange= obj asTextRange; if(TextRange.Text = ="The framework of China-ASEAN self-trade zone")                    {                        //formatting a string that adds footnotesTextRange.CharacterFormat.Bold =true; //Insert FootnoteParagraph. Childobjects.insert (i +1, footnote);  Break; }                }            }            //Add footer content is formatted with fontTextRange Text = footnote. Textbody.addparagraph (). AppendText ("On November 4, 2002, Premier Zhu Rongji and the leaders of 10 ASEAN countries signed the "China-ASEAN Comprehensive Economic Cooperation Framework agreement", which marked the economic and trade cooperation between China and ASEAN 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= A; Footnote. Markercharacterformat.bold=true; Footnote. Markercharacterformat.textcolor=Color.darkgreen; //get the 5th paragraphParagraph PARAGRAPH2 = document. sections[0]. paragraphs[4]; //add endnotes and set endnotes and formattingFootnote endnote =PARAGRAPH2.            Appendfootnote (Footnotetype.endnote); TextRange Text2= Endnote. Textbody.addparagraph (). AppendText ("The 17 major reports of the party clearly state:"+""Adhering to the basic national policy of opening up to the outside, bringing ' introduction ' and ' going out ' to better combine"+"Expand open areas, optimize open structure, improve open quality, improve internal and external linkage,"+"The open economy, which is mutually beneficial, safe and efficient, forms the new advantage 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= A; Endnote. Markercharacterformat.bold=false; Endnote. Markercharacterformat.textcolor=Color.darkgreen; //Save and open a documentDocument. SaveToFile ("add footnote endnotes. docx", fileformat.docx2010); System.Diagnostics.Process.Start ("add footnote endnotes. docx"); }    }}

Test results:

Step three: Read footer endnotes

"C #"

usingSpire.doc;usingSpire.Doc.Documents;usingSpire.Doc.Fields;usingSystem.IO;usingSystem.Text;namespacereadfootnote_doc{classProgram {Static voidMain (string[] args) {            //Create a document class object to load the documents that need to be testedDocument document =NewDocument (); Document. LoadFromFile ("add footnote endnotes. docx"); //get the first section of the documentSection section = document. sections[0]; //instantiate the StringBuilder classStringBuilder SB =NewStringBuilder (); //traverse all paragraphs in a document            foreach(Paragraph ParagraphinchSection . Paragraphs) { for(inti =0, CNT = paragraph. Childobjects.count; I < CNT; i++) {paragraphbase pbase= paragraph. Childobjects[i] asparagraphbase; if(PBase isFootnote) {                        foreach(Paragraph Footparainch(PBase asFootnote). textbody.paragraphs) {sb.                        Append (Footpara.text); }                    }                }            }            //writes read content to text and savesFile.writealltext ("FootNotes.txt", sb.            ToString ()); //Open DocumentSystem.Diagnostics.Process.Start ("FootNotes.txt"); }    }}

Read Result:

C # Add, read word footer endnotes

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.