How to access paragraph with footnote?

Hi there,

I have a footnote and i need to identify the paragraph to get its content and also to remove it.

How to do that?

regards

@ibox You can get the paragraph by the bookmark and then remove it. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
// get the bookmark.
Bookmark bk = doc.Range.Bookmarks["footnotes_entry_9145"];
// Get the paragraph where the bookmakr starts.
Paragraph para = (Paragraph)bk.BookmarkStart.GetAncestor(NodeType.Paragraph);
// Remove the paragraph.
para.Remove();
doc.Save(@"C:\Temp\out.docx");

Thank you Sir,

It works.
Can we get the paragraph and do replace in the doc template?

I have a page reference and want the paragraph to be moved to the page. I attach a picture.

regards

move_bookmarks_to_reference page

@ibox Unfortunately, your requirements are not quite clear by screenshot. If possible please attach your input and expected output documents here for our reference.

Thank u, Sir

I attach the template and also the output doc. For the output doc, it is on page 12 and 14. The bookmarks is on page 12 and it should be deleted and move to page 14.

regards
DISP SRA (30).docx (293.8 KB)

BeautySectara05.docx (73.6 KB)

Thank you, Sir

If i delete more than one bookmark, using this code:

foreach (Bookmark bk in doc.Range.Bookmarks)
{
    string name = bk.Name;
    if(name.Length >= 16)
    {
        if(name.Substring(0, 16)== "footnotes_entry_")
        {
            // Get the paragraph where the bookmakr starts.
            Paragraph para = (Paragraph)bk.BookmarkStart.GetAncestor(NodeType.Paragraph);
            // Remove the paragraph.
            para.Remove();
        }
    }
}

why only the first one deleted, but the rest are not deleted?

regards
first_bookmark_only deleted

@ibox Please try using the following code to remove the paragraphs with bookmarks:

Document doc = new Document(@"C:\Temp\in.docx");

List<Bookmark> bookmakrs = doc.Range.Bookmarks.Where(b => b.Name.StartsWith("footnotes_entry_")).ToList();
foreach (Bookmark bk in bookmakrs)
{
    // Get the paragraph where the bookmakr starts or ends.
    Paragraph para = (Paragraph)(bk.BookmarkStart.GetAncestor(NodeType.Paragraph) ?? bk.BookmarkEnd.GetAncestor(NodeType.Paragraph));
    // Remove the paragraph.
    if (para != null)
        para.Remove();
}

doc.Save(@"C:\Temp\out.docx");

Thank you, Sir.

It works.

I still have a “line” in the document before the bookmarks. Is it a shape? How to remove it?

I attach the picture.

regards

@ibox Yes, the line is shape. In your case you can simply remove the paragraph before the paragraph with bookmark. Please see Node.PreviousSibling property.

Thank you, Sir.

I can remove it:

bool firstbookmark = true;
List<Bookmark> bookmakrs = doc.Range.Bookmarks.Where(b => b.Name.StartsWith("footnotes_entry_")).ToList();
foreach (Bookmark bk in bookmakrs)
{
    // Get the paragraph where the bookmakr starts or ends.
    Paragraph para = (Paragraph)(bk.BookmarkStart.GetAncestor(NodeType.Paragraph) ?? bk.BookmarkEnd.GetAncestor(NodeType.Paragraph));

    if (firstbookmark)
    {
        firstbookmark = false;
        Paragraph para_line = (Paragraph)para.PreviousSibling;
        if (para_line != null)
            para_line.Remove();
    }
    // Remove the paragraph.
    if (para != null)
        para.Remove();
}

regards

@ibox It is perfect that you managed to achieve what you need.