This project has moved. For the latest updates, please go here.

Closing a Table?

Sep 11, 2014 at 7:38 PM
I have a function where I'm adding a table to my document, one row, two columns.

In column one I add an image, in column zero I add text.
The text for column zero is coming from another document, so I'm iterating through that document to add each paragraph from the second document into the cell.

This all worked great, until I had a second document that had a table in it.

Once the table is added from the second document, all subsequent text is inserted into the table. Is there a concept of "CloseTable" so the next paragraph inserted does not go into the table?

See example below...assume the document I'm inserting into is already opened.
  public bool AppendDocumentAndImageInTable(string pathDocument, string pathImage, double firstColumnScale,  double imageScale)
        {
            var returnValue = false;
            if (null != document && File.Exists(pathDocument) && File.Exists(pathImage))
            {
                try
                {
                    Table simpleTable = document.AddTable(1, 2);                    
                    simpleTable.Alignment = Alignment.left;                    
                    var insertedTable = document.InsertTable(simpleTable);
                    
                    Image image = document.AddImage(pathImage);
                    var picture = image.CreatePicture();
                    picture.Width = Convert.ToInt32(picture.Width*imageScale);
                    picture.Height = Convert.ToInt32(picture.Height*imageScale);                    
                    insertedTable.Rows[0].Cells[1].Paragraphs.First().AppendPicture(picture);                    
                    using (var documentToAppend = DocX.Load(pathDocument))
                    {
                        foreach (var paragraph in documentToAppend.Paragraphs)
                        {                                                        
                            if (!paragraph.ParentContainer.ToString().Equals("Cell"))
                            {                                
                                insertedTable.Rows[0].Cells[0].Paragraphs.Last().InsertParagraphAfterSelf(paragraph);
                            }
                            if (paragraph.FollowingTable != null)
                            {                                
                                insertedTable.Rows[0].Cells[0].Paragraphs.Last().InsertTableAfterSelf(paragraph.FollowingTable);                                                                                                                             
                            }                            
                        }
                    }
                    insertedTable.Rows[0].Cells[0].Width = (document.PageWidth - document.MarginLeft - document.MarginRight) * firstColumnScale;
                    insertedTable.Rows[0].Cells[1].Width = (document.PageWidth - document.MarginLeft - document.MarginRight) * (1-firstColumnScale);                                        
                    returnValue = true;
                }
                catch (Exception)
                {


                }
            }
            return returnValue;
        }
Sep 11, 2014 at 8:13 PM
I have a something that works:

Replace
insertedTable.Rows[0].Cells[0].Paragraphs.Last().InsertTableAfterSelf(paragraph.FollowingTable);
With
insertedTable.Rows[0].Cells[0].Paragraphs.Last().InsertTableBeforeSelf(paragraph.FollowingTable);
Can anyone explain why this would work?