Footnotes in HTML

Hi,

I have some html which I’m inserting using InsertHtml method of DocumentBuilder. My HTML will have some footnotes which right now are represented by text (but I can change this format). Does the InsertHtml method support footnotes in any way. I can modify my HTML to suit any format if the InsertHtml method supports footnotes automatically.

Thanks

Hello,
Thank you for your request.
Unfortunately we do not have a such possibility.
As a workaround, you can use code like this:
test.html:

<html>
<head>
    <title>footnote test</title>
</head>
<body>
    Footnote reference:
    <sup>[<a name="id1" href="#ftn.id">*</a>]</sup>
    </br>
    </br>
    </br>
    </br>
    </br>
    <div>
        <p>
            <hr>
            Footnote:</br>
            <sup>[<a name="ftn.id" href="#id1">*</a>]</sup>Text of footnote ...
        </p>
    </div>
</body>
</html>

**C# code: **

Document doc = new Document("X:\\test.html");
doc.Save("x:\\out.doc");

Very difficult task to make a doc document completely similar to html (and vice versa).
I have added your request to the appropriate issue in our defect database. Once such functionality will be available, we will notify you immediately.

Hi
Thanks for your request. In case of using text in the output document you will get this text superscripted.
However, if you need a fully functional footnote in your output document, then there is no direct way to achieve this, as Viktor mentioned. But I am not sure that the HTML suggested y Viktor will fulfill your requirements. I think, in your case, you can put a placeholder in your HTML and then after importing HTML, you can use IReplacingCallback to replace this placeholder with full functional footnote. You can use code similar to one suggested here:
https://forum.aspose.com/t/merge-field-formatting/56930
But instead of inserting a merge field, you should insert a footnote as described here:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/insertfootnote/
Hope such technique could help you to achieve what you need.
Best regards,

AndreyN:
Hi Rajasthan

Hehe, that’s my state’s name not my name :slight_smile:

AndreyN:
I think, in your case, you can put a placeholder in your HTML and then after importing HTML, you can use IReplacingCallback to replace this placeholder with full functional footnote.

Yes I saw an example of highlighting here I just wanted to know if this is supported directly. Just another thing, lets say I insert some html in a long document using InsertHtml, which creates lets say 4 paragraphs. Is there a way to do a find and replace only on those 4 paragraphs (and not the whole document)? I’m not sure if the Range object in Aspose can be used that way i.e. having Range objects of multiple paragraphs at arbitrary locations. From what I’ve read, I can have range object of one paragraph, or a table cell, or a section in the document or the whole document. If there is any workaround to that let me know.
Thanks

Hi there,
Thanks for your inquiry.
You can find complete code for how to import footnotes from HTML avaliable in the samples pack here. You need to look at the “ImportFootnotesFromHtml” project. Hopefully this will do for now, we will provide an system to automatically import footnotes sometime in the future.
Regarding your second request if you are using a handler then I think you can use the code from here. The code will populate a NodeRange class which gives you the starting node and ending node of content inserted using an operation (in this case by InsertHtml). In your replace method you can then check if the match node comes after the start node and before the end node.
Please note sometime in the future we will make public a much improved Range class which will make these types of operations easier.
If you have any further queries, please feel free to ask.
Thanks,

Hi, Thanks for the detailed reply.

My concern is not that find-replace will find a match outside of the HTML that I inserted because it wont. My concern is performance. I’ll be performing an InsertHtml operation around 1000 to 1500 times in my document. I’m worried that if I do a find-replace on the Range object of the whole document that many times, it might effect the performance very badly. That is why I wanted a Range object of only the paragraphs that I inserted which from your reply I think is not possible as of now. Another way around would be I store all the footnotes that I find in the 1000 HTMLs into a collection and do a find-replace at the end. For that the find-replace will have to find matches in the correct order. So does the find-replace works in a linear fashion that is, from start of the document moving towards the end?

Hello
Thank you for additional information. Please see the following link to learn more about Ranges in Aspose.Words:
https://reference.aspose.com/words/net/aspose.words/range/
You can use a Range object of only the previously inserted paragraph. In this case your code should look like the following:

Document doc = new Document("C:\\Temp\\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml("<p>Test</p>");
// Search for previously inserted paragraph and replace "Test" with "Replacement"
Node currentNode = builder.CurrentParagraph.PreviousSibling;
while (currentNode != null && currentNode.NodeType != NodeType.Paragraph)
    currentNode = currentNode.PreviousPreOrder(doc);

if (currentNode != null)
{
    currentNode.Range.Replace("Test", "Replacement", false, false);
}
doc.Save("C:\\Temp\\out.doc");

Best regards,

Hi Andrey, thanks for your reply. The problem is the html that I’ll insert would be complex. It will contain bullets, numbered lists, tables etc. So I can’t just go to the previous paragraph and do a find-replace. So my problem is still that since Range object has some limitations, the solution that I think for footnotes is I’ll store all footnotes I encounter in a collection and then do a find and replace at the end of generating the document. So I just want to know if the behavior of find-replace is guaranteed to be linear and not random…

Hi there,
Thanks for this additional information.
Yes, the replacement algorthim will work from the start of the document to the end in a linear fashion by default. Please note that forward search and replace can sometimes cause problems, this may occur when a peice of text is replaced which then affects another peice of text being identified as a match. This can lead to some instances of the search string being skipped. For this reason you can use the third parameter of the Replace method which specifies if search is done forward or backwards.
In your case hopefully this won’t be a problem, please try using the method you have discussed and feel free to get back to us if you have any problems.
Thanks,