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?
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");
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));
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.