Hi!
Have a scenario where building multiple tables, each table being on a new page (added a builder.pagebreak statement for this) and each table can have different orientation. just a sample code here:
public void TestDiffOrientationPerTable()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
Table table1 = builder.StartTable();
builder.InsertCell();
builder.Write("Table 1 row 1 cell 1");
builder.InsertCell();
builder.Write("This is row 1 cell 2");
builder.EndRow();
builder.InsertCell();
builder.Writeln("This is row 2 cell 1");
builder.InsertCell();
builder.Writeln("This is row 2 cell 2");
builder.EndRow();
builder.EndTable();
builder.InsertBreak(BreakType.PageBreak);
builder.PageSetup.Orientation = Aspose.Words.Orientation.Portrait;
Table table2 = builder.StartTable();
builder.InsertCell();
builder.Write("Table 2 row 1 cell 1");
builder.InsertCell();
builder.Write("This is row 1 cell 2");
builder.EndRow();
builder.InsertCell();
builder.Writeln("This is row 2 cell 1");
builder.InsertCell();
builder.Writeln("This is row 2 cell 2");
builder.EndRow();
builder.EndTable();
doc.Save(tempPath + "DiffOrientation.docx");
}
The orientation property here is getting the last set property (being on a PageSetup instance level), portrait; and not different orientations as expected.
How would we achieve that?
Regards,
Remus
@Remus87 you need to add a section break to generate a new Section:
public void TestDiffOrientationPerTable()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
Table table1 = builder.StartTable();
builder.InsertCell();
builder.Write("Table 1 row 1 cell 1");
builder.InsertCell();
builder.Write("This is row 1 cell 2");
builder.EndRow();
builder.InsertCell();
builder.Writeln("This is row 2 cell 1");
builder.InsertCell();
builder.Writeln("This is row 2 cell 2");
builder.EndRow();
builder.EndTable();
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.PageSetup.Orientation = Aspose.Words.Orientation.Portrait;
Table table2 = builder.StartTable();
builder.InsertCell();
builder.Write("Table 2 row 1 cell 1");
builder.InsertCell();
builder.Write("This is row 1 cell 2");
builder.EndRow();
builder.InsertCell();
builder.Writeln("This is row 2 cell 1");
builder.InsertCell();
builder.Writeln("This is row 2 cell 2");
builder.EndRow();
builder.EndTable();
doc.Save(tempPath + "DiffOrientation.docx");
}
Works perfect now
Thank you
1 Like
Replacing BreakType.PageBreak with BreakType.SectionBreakNewPage worked fine for the solution but did arise a minor issue: is not clearing the last blank page.
Sometimes had the issue of having a last page blank. To overcome this we’re using this snippet at end:
NodeCollection runs = doc.LastSection.GetChildNodes(NodeType.Run, true);
if (runs.Count > 0)
{
Run runLast = (Run)runs.Last(); // careful here, might be null
if (runLast.Text.IndexOf(ControlChar.PageBreakChar) >= 0)
{
runLast.Text = runLast.Text.Remove(runLast.Text.IndexOf(ControlChar.PageBreakChar), 1);
}
}
Now just noticed that doesn’t have effect anymore, hence the last blank page again.
@Remus87 that should not happen, can you please attach a document where this happen?
@Remus87 The problem occurs because the last empty page is causes not by page break as is supposed the provided code, but by a section break. So you should remove the last empty section to avoid an empty page:
doc.LastSection.Remove();
You can add additional checks if required before removing the last section.
FYI @eduardo.canal
Thanks!
I replaced all the snippet code provided earlier with a check if last section is empty, than remove it (and refactor in a method) as below:
private void RemoveEmptySection(Document doc, Section section)
{
if (section.Body.ToString(SaveFormat.Text).Trim().Equals(String.Empty)
&& section.Body.GetChildNodes(NodeType.Shape, true).Count == 0)
{
section.Remove();
}
}
It seems to work fine, but if have any other suggestions to improve it with other checks please let me know
1 Like
@Remus87 It is perfect that you managed to make it work. Your code looks good. Please feel free to ask in case of any issues, we are always glad to help you.