How to identify the footnote in table and copy that to last row of the table. Kindly help me asap.
Please find the below mentioned input and expected words document.
Input_Word_document_footnote.docx (17.3 KB)
Expeceted_output_Footnote.docx (15.9 KB)
@Princeshivananjappa You can use code like the following to achieve this:
Document doc = new Document(@"C:\Temp\Input_Word_document_footnote.docx");
foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
// process only top level tables.
if (table.ParentNode.NodeType != NodeType.Body)
continue;
NodeCollection notes = table.GetChildNodes(NodeType.Footnote, true);
// Skip the table if it does not have footnotes.
if (notes.Count == 0)
continue;
// Create a row for footnotes.
Row footnotesRow = (Row)table.LastRow.Clone(true);
table.AppendChild(footnotesRow);
footnotesRow.GetChildNodes(NodeType.Paragraph, true).Clear();
if (footnotesRow.Cells.Count > 1)
{
footnotesRow.FirstCell.CellFormat.HorizontalMerge = CellMerge.First;
for (int i = 1; i < footnotesRow.Cells.Count; i++)
footnotesRow.Cells[i].CellFormat.HorizontalMerge = CellMerge.Previous;
}
// Copy content of footnotes into the newly created row.
int counter = 1;
foreach (Footnote note in notes)
{
foreach (Paragraph para in note.Paragraphs)
{
Paragraph clone = (Paragraph)para.Clone(true);
// Footnote starts with a special character, which is updated by MS Word to footnote number.
// Remove it and replace with a simple text, youo can use also list.
if (clone.FirstChild.NodeType == NodeType.SpecialChar)
{
clone.FirstChild.Remove();
Run marker = new Run(doc, counter.ToString());
marker.Font.Superscript = true;
clone.PrependChild(marker);
counter++;
}
footnotesRow.FirstCell.AppendChild(clone);
}
}
}
doc.Save(@"C:\Temp\out.docx");
@alexey.noskov Thanks for the quick reply. The above code is working fine but once I copied the footnote to the last row of the table I need to remove the footnote on the last page.is it possible to remove it? Kindly help me asap.
@Princeshivananjappa Sure you can use simply remove all footnotes. Just modify the code like the following:
// Copy content of footnotes into the newly created row.
int counter = 1;
foreach (Footnote note in notes)
{
Run marker = new Run(doc, counter.ToString());
marker.Font.Superscript = true;
counter++;
foreach (Paragraph para in note.Paragraphs)
{
Paragraph clone = (Paragraph)para.Clone(true);
// Footnote starts with a special character, which is updated by MS Word to footnote number.
// Remove it and replace with a simple text, youo can use also list.
if (clone.FirstChild.NodeType == NodeType.SpecialChar)
{
clone.FirstChild.Remove();
marker.Font.Superscript = true;
clone.PrependChild(marker.Clone(true));
}
footnotesRow.FirstCell.AppendChild(clone);
}
note.ParentNode.InsertBefore(marker.Clone(true), note);
}
notes.Clear();