Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Oct 08, 2015 9:11 am

Hi,

In short, I'm trying to insert a table between existing paragraphs.

Our users need to upload a docx that contains tokens/placeholders which will then be replaced by text or sometimes a table. Finding the tokens is easy, and replacing them with text too. Adding a table on a specific location is another thing.

Suppose the document structure is as follows:

Document
-Section
--Body
---Paragraph 1
---Paragraph 2
---Paragraph 3

I'm starting with
Code: Select all
TextSelection[] textSelection = document.FindAllPattern(regexPattern);

I can then call GetAncestor(..) on a TextRange of that selection to get its Paragraph, its Body, its Section, etc

So how do I insert a table between Paragraph 1 and Paragraph 2?
Section and Body both have an AddTable(..) method, but this will add the Table after all existing paragraphs.

Do I need to create the body from scratch and add everything in order?

Thank you

robinvos
 
Posts: 22
Joined: Thu Oct 08, 2015 8:43 am

Fri Oct 09, 2015 3:28 am

Hello,

Thanks for your inquiry.
Please try the below code snippet:
Code: Select all
   Paragraph paragraph = textSelection[0].GetAsOneRange().OwnerParagraph;
                int index = paragraph.Owner.ChildObjects.IndexOf(paragraph);
                Table table = new Table(document,true);
                table.ResetCells(3, 4);
                paragraph.Owner.ChildObjects.Insert(index, table);
                paragraph.Owner.ChildObjects.RemoveAt(index + 1);


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3046
Joined: Wed Jun 27, 2012 8:50 am

Fri Oct 09, 2015 4:55 pm

Hello,

Thanks for your reply, it was very helpful.

I'm running an older version of Spire.Doc, v5.1.6.4035. Perhaps that's why ChildObjects cannot be resolved in paragraph.Owner, which is a DocumentObject that has to be casted to the right type first.
The code below is a workaround for that, but perhaps there is a more elegant way than casting?
Another thing, are there any other objects that can be the owner of a paragraph, that are not in my list?

Thank you.

Code: Select all
protected static void InsertTableAfterParagraph(Table table, TextSelection textSelection)
{
   Paragraph paragraph = textSelection.GetAsOneRange().OwnerParagraph;

   DocumentObjectCollection childObjects = null;
   DocumentObject paragraphOwner = paragraph.Owner;

   if (paragraphOwner is Paragraph)
      childObjects = ((Paragraph)paragraphOwner).ChildObjects;
   else if (paragraphOwner is Table)
      childObjects = ((Table)paragraphOwner).ChildObjects;
   else if (paragraphOwner is Section)
      childObjects = ((Section)paragraphOwner).ChildObjects;
   else if (paragraphOwner is Body)
      childObjects = ((Body)paragraphOwner).ChildObjects;
   else if (paragraphOwner is Document)
      childObjects = ((Document)paragraphOwner).ChildObjects;
   else
      Debug.Fail("paragraph.Owner is a " + paragraph.Owner.ToString());

   if (childObjects != null)
   {
      childObjects.Insert(childObjects.IndexOf(paragraph) + 1, table);
   }
}

robinvos
 
Posts: 22
Joined: Thu Oct 08, 2015 8:43 am

Sat Oct 10, 2015 2:29 am

Hi,

Thanks for your further inquiry.
paragraph.Owner only is Spire.Doc.Body.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3046
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc