EvotecIT / OfficeIMO

Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
263 stars 47 forks source link

Get Footnotes from Document #152

Closed A10s closed 1 year ago

A10s commented 1 year ago

How can I find the footnotes of a Word document?

PrzemyslawKlys commented 1 year ago

I don't think it's currently supported. Neither are endnotes.

image

A10s commented 1 year ago

thanks for the quick response

PrzemyslawKlys commented 1 year ago

By not supported I mean they are not directly exposed in easy to use way - but with some effort you could get them. Probaby easier to add this functionality into officeimo for others to use.

A10s commented 1 year ago

Can you make a small example, how I get the footnotes from the document? I currently need to replace all hyperlinks in a project, including those in footnotes.

PrzemyslawKlys commented 1 year ago

It's probably easier to add some support for it. Not sure if i will have time, but I'll play with it soon

PrzemyslawKlys commented 1 year ago

@A10s while I am not done with the PR, maybe this is something that will get you started already. You can add, find foot notes and modify them for now. If you're willing to compile your own version it may be just what you need for now. I'll probably finish it in few days if i get enough time. End notes are not done either.

A10s commented 1 year ago

Thanks for the answer. I've already found how to get to the footnotes and the hyperlinks they contain

A10s commented 1 year ago

my code:

var doc=WordDocument.Load("test.docx");
var footnotesPart = doc._document.MainDocumentPart?.FootnotesPart;
if (footnotesPart != null)
   {
     var footnotes = footnotesPart.Footnotes.Elements<Footnote>();
     foreach (var f in footnotes)
        {
          var hyperlinks = f.Descendants<Hyperlink>();
               foreach (var hyp in hyperlinks)
                    {
                       var id=hyp.Id;
                       var hrs=footnotesPart .HyperlinkRelationships.Single(q => q.Id == id);
                       var uri= hrs.Uri;
                       ........
                    }
   }}
PrzemyslawKlys commented 1 year ago

As long as it works for you ;) My implementation is a bit more complex, but it just simplifies things.

using (WordDocument document = WordDocument.Load(filePath)) {
    foreach (var footNote in document.FootNotes) {
        foreach (var paragraph1 in footNote.Paragraphs) {
            if (paragraph1.IsHyperLink) {
                paragraph1.Hyperlink.Text = "xxx";
            }
        }
    }
}
A10s commented 1 year ago

Thank you for your support!