Open a Docx, make change, save as PDF

6 posts, 0 answers
  1. Iwhp
    Iwhp avatar
    40 posts
    Member since:
    Dec 2009

    Posted 05 Feb 2011 Link to this post

    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


  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 09 Feb 2011 Link to this post

    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>>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Justin
    Justin avatar
    8 posts
    Member since:
    Mar 2011

    Posted 28 Apr 2011 Link to this post

    Is it possible to do the DOCX to PDF conversion described above in a C# web application?
  5. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 03 May 2011 Link to this post

    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
  6. Rotem
    Rotem avatar
    25 posts
    Member since:
    Sep 2010

    Posted 02 Jun 2011 Link to this post

    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.
  7. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 07 Jun 2011 Link to this post

    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
Back to Top
UI for WPF is Visual Studio 2017 Ready