- 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
DocumentTextSearch search = new DocumentTextSearch(document);
foreach (var textRange in search.FindAll("Company"))
//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))
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:
inserts the text at the current caret position (which by default is in the beginning of the document and you do not set it anywhere in your code) and only once (since it is not in the body of the foreach-cycle).
Give the approach I described a try and let us know if you experience any difficulties. Regards,
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.
the Telerik team
I've tried your solution and it doesn't work.
calling the line raises a System.NullReferenceException.
Here is the stack trace:
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>
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:
I've tried to manipulate it in many other ways and they all result with the same problem.
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).
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:
If that does not help, we would appreciate some sample project illustrating the behavior you are observing.
the Telerik team