Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Sep 10, 2014 3:15 pm

Hi,
The code provided in the following tutorial is not working for me. It never adds footnote right next to a particular word in a paragraph. It always adds the footnote at the end of the paragraph. Is the code provided in the tutorial below correct or do we need to modify it?

"e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/Footnote/Insert-a-footnote-in-a-paragraph.html"

Here is my code.
Code: Select all
Document document1 = new Document();
            document1.LoadFromFile("C:\\PDF\\SampleDoc.docx", FileFormat.Docx2010);
            Paragraph paragraph1 = document1.Sections[0].Paragraphs[0];
            Footnote footnote1 = paragraph1.AppendFootnote(FootnoteType.Footnote);
           
            DocumentObject obj=null;
           
            for (int i = 0; i < paragraph1.ChildObjects.Count; i++)
            {
                obj=paragraph1.ChildObjects[0];
                if (obj.DocumentObjectType == DocumentObjectType.TextRange)
                {
                    TextRange textRange = obj as TextRange;
                    // Find the word "Spire.Doc" in paragraph1
                    if (textRange.Text == "Spire.Doc")
                    {
                    //Set bold format for the word "Spire.Doc"
                    textRange.CharacterFormat.Bold = true;
                    //Insert footnote1 after the word "Spire.Doc"
                    paragraph1.ChildObjects.Insert(i + 1, footnote1);
                    break;
                    }
                }
            }

            TextRange text = footnote1.TextBody.AddParagraph().AppendText("Welcome to evaluate Spire.Doc");
            text.CharacterFormat.FontName = "Arial Black";
            text.CharacterFormat.FontSize = 10;
            text.CharacterFormat.TextColor = Color.DarkGray;

            footnote1.MarkerCharacterFormat.FontName = "Calibri";
            footnote1.MarkerCharacterFormat.FontSize = 12;
            footnote1.MarkerCharacterFormat.Bold = true;
            footnote1.MarkerCharacterFormat.TextColor = Color.DarkGreen;
            document1.SaveToFile("C:\\PDF\\SampleDocNew.docx", FileFormat.Docx2010);


Here is the text from the updated word doc with footnote. FootNote was added at the end of the paragraph and not after the word "Spire.Doc" as intended.

Evaluation Warning : The document was created with Spire.Doc for .NET.
Spire.Doc clone a finalized loan write-up so that appears as a new editable loan write-up, but all previously entered information from the original write-up that was cloned has been pre-populated including all footnotes and excluding ancillary documents.

akhan2003
 
Posts: 11
Joined: Tue Sep 09, 2014 6:47 pm

Wed Sep 10, 2014 3:26 pm

I figured it out. I needed to account for an extra space after word "Spire.Doc ".

akhan2003
 
Posts: 11
Joined: Tue Sep 09, 2014 6:47 pm

Wed Sep 10, 2014 4:08 pm

How Do I add footnotes when I have data in a table? I need to add a footnote next to some selected data elements in a table?

Comments

Category Description and/or Model Serial Number Quantity Purchase Status Condition
Printer hp 1 Purchase Good
Laptop BB 2356 1 Purchase Good

akhan2003
 
Posts: 11
Joined: Tue Sep 09, 2014 6:47 pm

Thu Sep 11, 2014 5:17 am

Hello,

There are some codes for your reference.
Code: Select all
   
            Document doc = new Document();
            doc.LoadFromFile("test.docx");
            Table table = doc.Sections[0].Tables[0] as Table;
            Paragraph paragraph = table.Rows[0].Cells[0].Paragraphs[0];
            Footnote footnote = paragraph.AppendFootnote(FootnoteType.Footnote);
            footnote.TextBody.AddParagraph().AppendText("Welcome to evaluate Spire.Doc");
            TextSelection ts = paragraph.Find("footnote", false, true);
            TextRange tr = ts.GetAsOneRange();
            int index = paragraph.ChildObjects.IndexOf(tr);
            paragraph.ChildObjects.Insert(index + 1, footnote);
            doc.SaveToFile("result.docx", FileFormat.Docx);

If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.Doc