- Load Docx works fine
- Find string company is not found
- Insert text with style stops the program
// Load Docx
RadDocument document = null;
IDocumentFormatProvider providerDocx = new DocxFormatProvider();
using (FileStream stream = File.Open(@"C:\Test.docx", FileMode.Open))
{
document = providerDocx.Import(stream);
}
// Change Document
document.Selection.Clear();
DocumentTextSearch search = new DocumentTextSearch(document);
foreach (var textRange in search.FindAll("Company"))
{
document.Selection.AddSelectionStart(textRange.StartPosition);
//document.Selection.AddSelectionEnd(textRange.EndPosition); // Necessary?
}
StyleDefinition style= new StyleDefinition();document.Insert("My Business", style); // Company should be changed to My Business
// Save Pdf
PdfFormatProvider providerPdf = new PdfFormatProvider();
using (Stream output = File.Open(@"C:\Test.pdf", FileMode.Create))
{
providerPdf.Export(document, output);
}
Any comments?
Thankx, Harry
5 Answers, 1 is accepted
Adding selection end is necessary and you cannot skip that step. What gets selected is the text between the selection start and the selection end. When one of these is missing, nothing gets selected.
You can refer to this help article, which deals with what I believe is the same scenario.
If you are not showing the document in a RadRichTextBox, you can use the InsertInline() method of RadDocument like this:
private
void
ReplaceAllMatches(RadDocument document,
string
toSearch,
string
toReplaceWith)
{
DocumentTextSearch search =
new
DocumentTextSearch(document);
List<TextRange> rangesTrackingDocumentChanges =
new
List<TextRange>();
foreach
(var textRange
in
search.FindAll(toSearch))
{
TextRange newRange =
new
TextRange(
new
DocumentPosition(textRange.StartPosition,
true
),
new
DocumentPosition(textRange.EndPosition,
true
));
rangesTrackingDocumentChanges.Add(newRange);
}
foreach
(var textRange
in
rangesTrackingDocumentChanges)
{
document.CaretPosition.MoveToPosition(textRange.StartPosition);
document.DeleteRange(textRange.StartPosition, textRange.EndPosition);
StyleDefinition style =
new
StyleDefinition();
style.SetPropertyValue(Span.ForeColorProperty, Colors.Red);
document.Insert(toReplaceWith, style);
textRange.StartPosition.Dispose();
textRange.EndPosition.Dispose();
}
}
document.Insert(
"My Business"
, style);
Give the approach I described a try and let us know if you experience any difficulties. Regards,
Iva
the Telerik team

Yes, it is possible to open a docx document and export it to PDF on the server.
If you run into any problems, let us know how we can assist you further.
Iva
the Telerik team

I've tried your solution and it doesn't work.
calling the
document.DeleteRange(range.StartPosition, range.EndPosition);
Here is the stack trace:
at Telerik.Windows.Documents.DocumentPosition.RemoveAnchorFromNextFormattingSymbol()
at Telerik.Windows.Documents.Model.RadDocument.DeleteInternal(DocumentPosition documentPosition, Boolean deletePrevious)
at Telerik.Windows.Documents.Model.RadDocument.DeleteRangeInternal(SelectionRange range)
at Telerik.Windows.Documents.Model.RadDocument.DeleteRange(DocumentPosition fromPosition, DocumentPosition toPosition)
at < My code>
Some background:
I have an html-formatted string.
I use an HtmlFormatProvider to make a RadDocument out of it (using the Import method).
I use your solution in order to replace any occurrence of some Regex string.
The search works fine (the resulted ranges are accurate).
Here is my code:
DocumentTextSearch searchDoc =
new
DocumentTextSearch(document);
List<TextRange> rangesParameters =
new
List<TextRange>();
foreach
(var range
in
searchDoc.FindAll(
"<<<[\x20-\x7E]+>>>"
))
{
TextRange rangeCopy =
new
TextRange(
new
DocumentPosition(range.StartPosition,
true
),
new
DocumentPosition(range.EndPosition,
true
));
rangesParameters.Add(rangeCopy);
}
foreach
(TextRange range
in
rangesParameters)
{
document.CaretPosition.MoveToPosition(range.StartPosition);
document.DeleteRange(range.StartPosition, range.EndPosition); // this is where the exception is being thrown
StyleDefinition style =
new
StyleDefinition();
style.SetPropertyValue(Span.ForeColorProperty, Colors.Red);
document.Insert(
"[param]"
, style);
range.StartPosition.Dispose();
range.EndPosition.Dispose();
}
I've tried to manipulate it in many other ways and they all result with the same problem.
BTW-
When deleting a single character using the delete method, it deletes the character next to the caret position but for some reason the document is being duplicated (I've failed to figure out why).
Thanks.
I am sorry, but I was not able to reproduce the issue. Please find attached a demo project that essentially uses the code from your snippet and seems to work as expected.
A possible cause for an exception is that the document has not been measured and arranged before you invoke the code form the snippet. If you are not showing the document in the editor before doing the replacing, you have to measure and arrange it in the default size like this:
private
void
MeasureAndArrangeInDefaultSize(RadDocument document)
{
document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
document.Arrange(
new
RectangleF(PointF.Empty, document.DesiredSize));
}
If that does not help, we would appreciate some sample project illustrating the behavior you are observing.
Iva
the Telerik team