Keeping Footnotes on their original place after editing runs

Hello,

could you please help us with the following problem.

  1. Word Text is read in and we work with paragraphs and runs
  2. We can edit the runs and ideally the references and footnotes remain as they were

Is the second step possible?

If yes, how should we do it?

@Wilczynskk97 MS Word documents are flows documents by their nature, so when you edit the document, the remaining content is reflowed appropriately. Footnotes are the same nodes in MS Word documents and are placed on the same level as Run notes:

So if you edit the run before footnote, the footnote will remain on the same position in the Document Object Model, but it might be shifted visually due to changes in the run’s text or formatting.

Thank you!
But our problem is now the following: We extract plain text from a word file, edit it and then insert it in a word file, while all formatting and the overall structure is kept the same.

However once we edited even just one charackter the footnote of a specific sentence jumps to the end of the paragraph. What should we do?

And is this possible for the Aspose Words API?

@Wilczynskk97 Could you please attach your input, output and expected output documents along with simple code that will allow us to reproduce the issue? We will check your scenario and provide you more information.

Hello,

thank you for your answer! So attached you find two documents. The original file is called Aspose_Text. This File has a paragraph with two footnotes. However when we extract the text, edit it slighltly (“multilateral”), then the footnotes move to the end of the paragraph, although the first footnote should come after the first sentence.

How can we solve this problem with the Aspose software API?
Controlling Seminar Final_Test_ASPOSE.docx (23,4 KB)
Controlling Seminar Final_Test_modified_2.docx (24,2 KB)

Do you have an idea?

@Wilczynskk97 Could you please share the code you use to extract and put the text back into the document? It is not quite clear how the modified document has been produced.

@alexey.noskov so right now we are using the python docx library, but we want to use or thinking to use the aspose API or SDK. So the process looks the following:

  1. Text from Word file gets extracted into a txt file.
  2. This txt gets edited.
  3. The edited Text is converted to a word file. Here the problem is that the footnotes are not in the same place.

Can this process be done with the Aspose?

@Wilczynskk97 Thank you for additional information. TXT documents itself does not have footnotes, they are simple text. So to keep the footnotes after DOCX->TXT->DOCX roundtrip it is required to perform some pre/post-processing of the files. To keep footnotes on their original places it is required to place some placeholders into the TXT document so these placeholders can be later be replaced with footnotes in DOCX document. You can achieve this using Aspose.Words. For example the following code replaces footnotes in the document with placeholders before converting the document to TXT, then after loading TXT document, the placeholders are replaced with footnotes:

placeholder_text = "[FOOTNOTE]"

doc = aw.Document("C:\\Temp\\in.docx")

# footnotes will be stored separately so we could restore them later.
footnotes = []
# replace footnotes with placeholder text.
for n in doc.get_child_nodes(aw.NodeType.FOOTNOTE, True) :
    n.parent_node.insert_after(aw.Run(doc, placeholder_text), n)
    footnotes.append(n.to_string(aw.SaveFormat.TEXT).strip())
    n.remove()

# Save the document as TXT.
doc.save("C:\\Temp\\tmp.txt")

# Here the TXT document might be edited.
# ......................

# Open document from txt
doc_from_txt = aw.Document("C:\\Temp\\tmp.txt")
builder = aw.DocumentBuilder(doc_from_txt)

# Replace placeholders with themsefs to make them to be represented as a separate Run.
doc_from_txt.range.replace(placeholder_text, placeholder_text);

# Loop through runs and put footnotes where required. 
index = 0;
for r in doc_from_txt.get_child_nodes(aw.NodeType.RUN, True) :
    run = r.as_run()
    if(run.text == placeholder_text) :
        builder.move_to(run)
        builder.insert_footnote(aw.notes.FootnoteType.FOOTNOTE, footnotes[index])
        index = index + 1
        run.remove()

doc_from_txt.save("C:\\Temp\\out.docx")

As you can see footnotes are restored on the same places they are in the original document.
docs.zip (21.9 KB)

Thank you very much! That helps us a lot. We will try to implement it today/tomorrow.

One more Question: Is this also possible through the Aspose Words API or only sdk?

@Wilczynskk97 Do you mean Aspose.Words Cloud API? If so, it would be better to ask in the Aspose.Words for Cloud support forum.

Yes, i meant Aspose Word API.

1 Like

But is there generally a difference between Aspose CLOUD API and SDK?

@Wilczynskk97 Aspose.Words for .NET is the main product, Aspose.Words for Cloud is based on this product, i.e. it used Aspose.Words for .NET as backend. So theoretically, everything that can be achieved using Aspose.Words for .NET can be also achieved using Aspose.Words for Cloud.

@alexey.noskov Just one more question: What if the txt gets edited and new words get added or deleted? Will it work then also? The txt will have e.g. one word more in a specific sentence that has a footnote.

@Wilczynskk97 Yes, the approach will still work as long the placeholders where footnotes are to be inserted are there.