在Word中创建表格时,位于页面底部的表格如果超出页面边幅大小会自动跨页布局到下一页,此时表格行数据会断行。为保证文档布局的合理性及美观性,可设置表格是否禁止跨页断行。下面将通过Spire.Doc for Java 来设置,分两种情况:
- 通过设置TableFormat.IsBreakAcrossPages属性选择是否跨页断行
- 保持表格内容在同一页面
方法1:设置属性禁止跨页断行
//加载测试文档
  Document doc= new Document("test.docx");
  //获取表格
  Table table = doc.getSections().get(0).getTables().get(0);
  //设置表格是否跨页断行
  for (int i = 0; i < table.getRows().getCount(); i++) {
      table.getRows().get(i).getRowFormat().isBreakAcrossPages(false);
  }
  //保存文档
  doc.saveToFile("result.docx",FileFormat.Docx_2013);方法2:保持表格内容在同一页面
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
public class PreventPagebreak {
    public static void main(String[]args){
        //加载测试文档
        Document doc= new Document("test.docx");
        //获取表格
        Table table = doc.getSections().get(0).getTables().get(0);
        //遍历表格单元格
        for (int i = 0;i< table.getRows().getCount();i++) {
            TableRow rows = table.getRows().get(i);
            for (int j = 0; j< rows.getCells().getCount(); j++){
                for (int z= 0; z < rows.getCells().get(j).getParagraphs().getCount();z++){
                    Paragraph p = rows.getCells().get(j).getParagraphs().get(z);
                    p.getFormat().setKeepFollow(true);//设置表格内容在同一页显示
                }
            }
        }
        //保存文档
        doc.saveToFile("result1.docx",FileFormat.Docx_2013);
    }
} 



 
					



