I'm generating a Word document by replacing some placeholder text in the template document with my values. For this, I'm using GemBox.Document, more specifically, this code from Find and Replace example:
var document = DocumentModel.Load("input.docx");
var firstPlaceholder = document.Content.Find("%Text1%").First();
firstPlaceholder.LoadText("Value 1");
var secondPlaceholder = document.Content.Find("%Text2%").First();
firstPlaceholder.LoadText("Value 2");
document.Save("output.docx");
That works fine.
But now I have a scenario in which the values that will replace the placeholders depend on their location, more specifically, does the placeholder appear before or after some specific paragraph in the document.
I did try using something like this:
Paragraph separator = ...
string firstPlaceholderText = "%Text1%";
string separatorText = seperator.Content.ToString();
string wholeDocumentText = document.Content.ToString();
if (wholeDocumentText.IndexOf(firstPlaceholderText) < wholeDocumentText.IndexOf(separatorText))
{
// The placeholder is before the separator...
}
else
{
// The placeholder is after the separator...
}
However, that same separatorText
value might occur in multiple places in the document so string.IndexOf()
is not a viable solution for me.
Is there another way how I could make this comparison, or another way how I could determine the location of some placeholder compared to some other document element?