This question is locked. New answers and comments are not allowed.
In our application we give the functionality to export some content to a docx file. Therefore we created a docx file to use as a template which is imported as described below to create a RadDocument:
This works fine. Additionally the template contains a footer with some predefined text which we want to be replaced on the export process. According to this thread we implemented a method to find and replace content of a RadDocument:
This works fine for content defined in the main text section.
But it does not work if the text to be replaced is part of the header or footer. It seems the DocumentTextSearch does not find strings inside headers or footers.
Is there some other way to replace text in headers or footers?
IDocumentFormatProvider provider =
new
DocxFormatProvider();
StreamResourceInfo info = Application.GetResourceStream(pathToFile, UriKind.RelativeOrAbsolute));
using
(Stream stream = info.Stream)
{
import word template
document = provider.Import(stream);
}
This works fine. Additionally the template contains a footer with some predefined text which we want to be replaced on the export process. According to this thread we implemented a method to find and replace content of a RadDocument:
DocumentTextSearch documentTextSearch =
new
DocumentTextSearch(document);
List<TextRange> rangesTrackingDocumentChanges =
new
List<TextRange>();
string
searchString = Regex.Escape(REPLACEMENT_STRING_DATE);
foreach
(var textRange
in
documentTextSearch.FindAll(searchString))
{
TextRange newRange =
new
TextRange(
new
DocumentPosition(textRange.StartPosition,
true
),
new
DocumentPosition(textRange.EndPosition,
true
));
rangesTrackingDocumentChanges.Add(newRange);
}
foreach
(var textRange
in
rangesTrackingDocumentChanges)
{
SpanLayoutBox currentSpanBox = document.CaretPosition.GetCurrentSpanBox();
Span currentSpan;
if
(currentSpanBox ==
null
)
{
currentSpan =
new
Span();
}
else
{
currentSpan = currentSpanBox.AssociatedSpan;
}
StyleDefinition style = currentSpan.ExtractStyleFromProperties();
document.CaretPosition.MoveToPosition(textRange.StartPosition);
document.DeleteRange(textRange.StartPosition, textRange.EndPosition);
document.Insert(DateTime.Now.Date.ToShortDateString(), style);
textRange.StartPosition.Dispose();
textRange.EndPosition.Dispose();
}
This works fine for content defined in the main text section.
But it does not work if the text to be replaced is part of the header or footer. It seems the DocumentTextSearch does not find strings inside headers or footers.
Is there some other way to replace text in headers or footers?