Get next paragraph after table

4 posts, 0 answers
  1. Thomas
    Thomas avatar
    4 posts
    Member since:
    May 2011

    Posted 01 Dec 2012 Link to this post

    I am using a .docx file as a template and I am doing some processing to the document (insert text, etc) Before I save the document as a PDF.

    Everything works great, but I have a problem to get and delete the next Paragraph after a specific table.

    I can find and delete the table by using the following code:
    //rdoc is the document, a .docx file used as a template.
     
    //Search for the text "sometext".
    DocumentTextSearch dts = new DocumentTextSearch(rdoc);
    TextRange tr = dts.Find("sometext");
     
    //Get the inline that the text is in.
    Inline iCurrent = (Inline)tr.StartPosition.GetCurrentInline();
     
    //Get the table to delete.
    Table tToDelete = iCurrent.GetParentOfType<Table>();
     
    //Delete the table.
    sCurrent.Blocks.Remove(tToDelete);

    How do I get and delete the paragraph that is directly below the table tToDelete?

    I tought about useing the tToDelete.nextsibling, but that seem to return the next table instead of the next paragraph. I also tried to do a "foreach" loop of all blocks in the current section and delete the block that is directly after the table tToDelete, but that does not seem to work either since the blocks does not seem to be in order (top to bottom of the document).

    Please note that I only want to process the document in memory and that I am therefore not using the actual RadRichTextBox Control.

     

  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 06 Dec 2012 Link to this post

    Hi Thomas,
    First, I should say that when a document is built using the RadRichTextBoxUI and you insert a table, an empty paragraph is inserted before and after the table and for some reasons related to the document structure we strongly recommend to clients who decide to build their documents from code to do the same.
    Other than that, I tried the approach you adopted and accessing the paragraph after a table using the NextSibling property works fine. However, we usually advise clients not to remove elements from the Blocks, Inlines, Children, etc. collections of the document elements once the document is built as that may cause the invalidation of the document or other problems. As you may have guessed yourself, we recommend using the RadRichTextBox/RadDocument API in such cases in order to ensure that after modifying the document it will still be valid. That said and from what you stated in your post - that the NextSibling property does not work for you and elements don't seem in order, I highly suspect the modifications you made to your document may have caused such issues.
    In this regard I would suggest using the Document Position and Document Selection approaches when removing from your documents. For example, you can get a move a DocumentPosition to the paragraph after a table as follows:
    Table table = tr.EndPosition.GetCurrentTableCellBox().AssociatedTableCell.Row.Table;
    Paragraph next = (Paragraph)table.NextSibling;
    rdoc.CaretPosition.MoveToInline(next.Inlines.First);
    Once you've successfully selected the required elements (for example - the table and the paragraphs prior and after it), you can call the Delete method of the document.
    Also, I would recommend not to add elements in the collections mentioned above using the AddBefore, AddAfter methods as that may also cause problems. Instead, you can move the CaretPosition of the document to the desired place and call the Insert or InsertFragment methods. You an also take a look at this article for information regarding DocumentFragment.
    I hope this information is helpful!


    Greetings,
    Petya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Thomas
    Thomas avatar
    4 posts
    Member since:
    May 2011

    Posted 06 Dec 2012 Link to this post

    Hallo!

    I have not hade the time to try this yet, but will this work when you are not using the actual RadRichTextBox control?

    The document was first created in Microsoft Office Word 2010 and then modified by using the RadRichTextBox WPF Control since there seam to be incompatibillity issues with Word 2010 and the layout of this document turned out quite differently.

    I would also like to take the opertunity to ask if the RadRichTextBox control is supposed to be compatible with Microsoft Office Word 2010?

    One of our goals for using the RadDocument class is to use documents created in Microsoft Office Word 2010 as templates and then insert data from a database and then either export the result as a .docx file that can be opened in Microsoft Word 2010 with preserved layout or exported as a PDF.

    Therefore it is very important do us that the RadRichTextBox and RadDocument are fully compatible with the most common functions, for example, tables, paragraphs, images and styling (font size, bold, etc.) in Microsoft Office Word 2010.

    Please note that we are using the Swedish version of Microsoft Word.
  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 10 Dec 2012 Link to this post

    Hello Thomas,
    The suggestions I made were with consideration to the fact that you do not want to put the actual RadRichTextBox in your application. The only other thing you should consider is that when showing a document in the editor it is measured and if you are building it from code, you may have to do that manually prior to exporting:
    document.EnsureDocumentMeasuredAndArranged();
    When it comes to the different formats the control supports, all imported documents are converted to the internal document structure of RadDocument which is described here. The import/export features are implemented to comply with the specifications of the respective formats, including the Office Open Xml (DocX) format specification. However, as the specification is rather wide, there are still some inconsistencies which may cause layout problems.
    As for the common uses of document-processing applications such as tables, rich text, images, etc. these are indeed implemented in RadDocument. If you notice specific problems when showing your document in the editor you can contact us through the support ticketing system and send us the document, so we can investigate it and help you further.
     

    All the best,
    Petya
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top