I am Inserting Heading along with table in a loop . I want both of them to be in same page. Right now it’s looking like this (take a look at uploaded document)
DR with Table-130202432628AM (UTC+0530).docx (136.0 KB)
@Ram_Arvind_Gadiraju To achieve what you need it is not required to calculate available space on the page. You can simply specify Page break before
option for heading paragraph.
If i have small tables i want them to appear on single page. i don’t want page break every time i insert table. Is there a way to find % of place available in last page so that i can go to next page
before inserting heading and Table ( i can do something like if %left < 25% add Page break)
@Ram_Arvind_Gadiraju In this case you should simply use Keep With Next
option in the heading paragraph and disable this option in the following paragraph.
MS Word documents are flow by their nature, so there is no “page” concept. Consumer applications reflows document content into pages on the fly.
I am trying to keep heading(not Paragraph) and Table together or heading and Ole object inserted (like in uploded document) in same page.
Can you provide sample code. I am using 17.12 Paid version.
@Ram_Arvind_Gadiraju Please see the following code that produces the expected output:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set keep with nest in the heading 1 style.
doc.Styles[StyleIdentifier.Heading1].ParagraphFormat.KeepWithNext = true;
// Generate some random content.
for (int i = 0; i < 10; i++)
{
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading_" + i);
builder.ParagraphFormat.ClearFormatting();
builder.StartTable();
for (int j = 0; j < 25; j++)
{
for (int k = 0; k < 5; k++)
{
builder.InsertCell();
// Set keep with next in the table
builder.ParagraphFormat.KeepWithNext = true;
builder.Write($"cell_{j}_{k}");
}
builder.EndRow();
}
Table table = builder.EndTable();
// Reset keep with next in the last row in the table.
foreach (Paragraph p in table.LastRow.GetChildNodes(NodeType.Paragraph, true))
p.ParagraphFormat.KeepWithNext = false;
}
doc.Save(@"C:\Temp\out.docx");
In your case heading and following paragraphs should have KeepWithNext
option set, but the paragraph before the next heading shoul have this option disabled (in case of table the paragraphs in the last row):
out.docx (11.1 KB)