Mutiple tables gets merged on save

Hello!

I have a problem with tables while creating an Aspose.Words document.

I’m reading an XML file and creating a doc that I want to save to RTF.
I have multiple tables in the document, several of them right after each other, created with starttable-endtable and so on without any paragraphs in between.

When I call save, the tables without paragraphs between are getting merged into one table.
Se attached debug screenshots, the table count before Save is 8, and after it is 2.

If I add some text between them everthing is fine, but it is important for me to keep the tables separate and without any text in between them. Any advice?

Regards
Lars Christian Hegde

Hi

Thanks for your request. Could you please attach your input and output documents here? I will investigate the issue and provide you more information.

Best regards,

Hello Andrey!

Hm, its not that simple, as my input is an XML-file with a bunch of

s which throug a lot of code is used to build an Aspose.Word doc structure. And the output cant be saved without messing up the table content.

I have attached the complete RTF, as is after save though.
And I will create some code to demonstrate the problem and post it in a little while.

Regards,
Lars Christian

Hi

Thanks for your inquiry. I can suggest you the one simple way to work this problem around. You can insert an empty paragraph between tables. Please see the following code:

// Open document.
Document doc = new Document("in.doc");
// Get collection of tables
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
Paragraph spliter = new Paragraph(doc);
spliter.ParagraphBreakFont.Size = 1;
// Loop through all tables
foreach (Table table in tables)
{
    // Check if the next node after the tabel is another table.
    // If so, insert an empty paragraph between tables.
    if (table.NextSibling != null && table.NextSibling.NodeType == NodeType.Table)
        table.ParentNode.InsertAfter(spliter.Clone(true), table);
}
// Save output document
doc.Save("out.doc");

Best regards,

Also if you try using MS Word you will see the same behavior. MS Word merges tables which are following each other.

Of course, I can do that or I can simply add the paragraps when I build the document in the first place.

But the problem is that these paragraphs (however small) mess up the structured content that the RTF is used for. I need the tables to separate and I can’t put any content between them.

I have attached sample code and sample output.
You can comment/uncomment the AddParagraph-lines to toggle the issue:

//Creating an Aspose.Words doc structure
Word.Document doc = new Word.Document();

//Create the document builder
Word.DocumentBuilder builder = new Word.DocumentBuilder(doc);

builder.MoveToDocumentStart();

builder.Writeln("This is a text heading");

builder.Writeln("This is some text");

//Plain borders
Word.BorderCollection bc = builder.RowFormat.Borders;
bc[Word.BorderType.Bottom].LineStyle = Word.LineStyle.Single;
bc[Word.BorderType.Top].LineStyle = Word.LineStyle.Single;
bc[Word.BorderType.Left].LineStyle = Word.LineStyle.Single;
bc[Word.BorderType.Right].LineStyle = Word.LineStyle.Single;
bc[Word.BorderType.Horizontal].LineStyle = Word.LineStyle.Single;
bc[Word.BorderType.Vertical].LineStyle = Word.LineStyle.Single;

builder.StartTable();
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.EndRow();
builder.EndTable();

// builder.InsertParagraph();

builder.StartTable();
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.EndRow();
builder.EndTable();

builder.Writeln("This is some text");
builder.Writeln("This is some text");
builder.Writeln("This is some text");

builder.StartTable();
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.EndRow();
builder.EndTable();

// builder.InsertParagraph();

builder.StartTable();
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.InsertCell();
builder.Write("Cell content");
builder.EndRow();
builder.EndTable();

Console.WriteLine(String.Format("Table count before save: {0}", doc.FirstSection.Body.Tables.Count));

Console.WriteLine(String.Format("Saving RTF to {0}", @"D:\Exchange\test_code_output.rtf"));
Word.Saving.SaveOutputParameters ret = doc.Save(@"D:\Exchange\test_code_output.rtf", Word.SaveFormat.Rtf);

Console.WriteLine(String.Format("Table count after save: {0}", doc.FirstSection.Body.Tables.Count));

Regards,
Lars Christian

Hello

Thank you for additional information. As I mentioned earlier this is expected behavior. If you try doing the same in MS Word you will get the same result.

Best regards,