This is a migrated thread and some comments may be shown as answers.

Open a Docx, make change, save as PDF

5 Answers 549 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Iwhp
Top achievements
Rank 1
Iwhp asked on 05 Feb 2011, 12:02 PM
I try to do the following, but run into issues:
- 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

Sort by
0
Iva Toteva
Telerik team
answered on 09 Feb 2011, 10:44 AM
Hello Harry Pfleger,

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();
    }
}

I tried running your sample code and it did not stop the application. However, this line:
document.Insert("My Business", style);
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,
Iva
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Justin
Top achievements
Rank 1
answered on 28 Apr 2011, 03:54 PM
Is it possible to do the DOCX to PDF conversion described above in a C# web application?
0
Iva Toteva
Telerik team
answered on 03 May 2011, 04:43 PM
Hello Justin,

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.

Best wishes,
Iva
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rotem
Top achievements
Rank 1
answered on 02 Jun 2011, 10:14 AM
Hi Iva.

I've tried your solution and it doesn't work.
calling the 
document.DeleteRange(range.StartPosition, range.EndPosition);
line raises a System.NullReferenceException.
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.
0
Iva Toteva
Telerik team
answered on 07 Jun 2011, 05:40 PM
Hello Rotem,

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.


Greetings,
Iva
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
RichTextBox
Asked by
Iwhp
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Justin
Top achievements
Rank 1
Rotem
Top achievements
Rank 1
Share this question
or