Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.
Tue Nov 21, 2023 6:46 pm
Hello,
I am adding various size tables to a section that spans multiple pages. Is there a way to make sure that the table is not split between two pages? For example, if the start of the table is toward the end of the page and extends to the following page, I'd like the whole table to be shown on the second page.
-

nataliegracegibbons
-
- Posts: 13
- Joined: Tue Oct 03, 2023 4:45 pm
Wed Nov 22, 2023 8:04 am
Hi,
Thank you for your inquiry.
I put the complete code below for your reference:
- Code: Select all
// Create a new Document object
Document document = new Document();
// Load the contents of a Word document from a file
document.LoadFromFile(@"test.docx");
// Iterate through each table in the first section of the document
for (int i = 0; i < document.Sections[0].Tables.Count; i++)
{
// Cast the current table to a Table object
Table table = document.Sections[0].Tables[i] as Table;
// Iterate through each row in the table
foreach (TableRow row in table.Rows)
{
// Iterate through each cell in the row
foreach (TableCell cell in row.Cells)
{
// Iterate through each paragraph in the cell
foreach (Paragraph p in cell.Paragraphs)
{
// Prevent page breaks within the paragraph
p.Format.KeepFollow = true;
}
}
}
}
// Define the result file name
String result = @"result.docx";
// Save the modified document to a new file with the specified format
document.SaveToFile(result, FileFormat.Docx2013);
If you have any issue just feel free to contact us.
Sincerely,
Ula
E-iceblue support team
-


Ula.wang
-
- Posts: 282
- Joined: Mon Aug 07, 2023 1:38 am