java - iText - 如果特定行位于页面底部,则在下一页上设置特定行

标签 java pdf pdf-generation itext

我在下面有一个工作代码。不幸的是,某些行(标题行)有时可能位于页面底部,这在显示过程中不太好。如果该行位于页面底部,有什么方法可以在下一页上设置特定行?提前致谢。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//package eklinik;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.List;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.itextpdf.text.BaseColor;
//import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.html.WebColors;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.awt.Desktop;

public class itextTest{

public static void main(String[] args) {
    try {
        Document document = new Document();
        PdfWriter.getInstance(document,
                new FileOutputStream("Report.pdf"));
        document.open(); 

        PdfPTable table = new PdfPTable(6);
        table.getDefaultCell().setBorder(0);
        table.setWidths(new float[]{ 1.5f, 3, 3.5f, 3, 30, 3});
        PdfPCell cell;

        int i = 0;
        int x = 0;

        for (i = 01; i <= 22; i++){ // loop it 22 times to represent 22 chapter

            int chapter_num = x; //to get chapter number
            int description_num =x + 1; // to get chapter description
            x = x+2; //current number + 2 to skip to next chapter number & description
            //x++;


              //chapter row
              cell = new PdfPCell(new Phrase("cell1"));
              cell.setColspan(1);
              table.addCell(cell);
              cell = new PdfPCell(new Phrase("Title 1"));
              cell.setColspan(4);
              table.addCell(cell);
              cell = new PdfPCell(new Phrase("cell3"));
              cell.setColspan(1);
              table.addCell(cell);



                  //block row
                  cell = new PdfPCell(new Phrase(""));
                  //cell.setColspan(5);
                  cell.setBorder(Rectangle.NO_BORDER); 
                  table.addCell(cell);
                  cell = new PdfPCell(new Phrase("Title 2"));
                  cell.setColspan(6);
                  table.addCell(cell);



                      cell = new PdfPCell(new Phrase(""));
                      cell.setColspan(1);
                      cell.setBorder(Rectangle.NO_BORDER);
                      table.addCell(cell);
                      cell = new PdfPCell(new Phrase("cell4"));
                      cell.setColspan(1);
                      table.addCell(cell);
                      cell = new PdfPCell(new Phrase("cell5"));
                      cell.setColspan(3);
                      table.addCell(cell);
                      cell = new PdfPCell(new Phrase("cell6"));
                      cell.setColspan(1);
                      table.addCell(cell);


                      // code row
                      cell = new PdfPCell(new Phrase(""));
                      cell.setColspan(2);
                      cell.setBorder(Rectangle.NO_BORDER); 
                      table.addCell(cell);
                      cell = new PdfPCell(new Phrase("Title 3"));
                      cell.setColspan(4);
                      table.addCell(cell);



                          cell = new PdfPCell(new Phrase(""));
                          cell.setColspan(2);
                          cell.setBorder(Rectangle.NO_BORDER); 
                          table.addCell(cell);
                          cell = new PdfPCell(new Phrase("cell9"));
                          cell.setColspan(1);
                          table.addCell(cell);
                          cell = new PdfPCell(new Phrase("cell10"));
                          cell.setColspan(2);
                          table.addCell(cell);
                          cell = new PdfPCell(new Phrase("cell11"));
                          cell.setColspan(1);
                          table.addCell(cell);



        } // for loop end       

        table.setWidthPercentage(100);
        document.add(table);

        document.close();


    }
    catch (Exception e) {
        System.err.println("Got an exception! ");
        System.err.println(e.getMessage());
    }





    }



}

最佳答案

您可以使用table.keepRowsTogether()来指定您不希望在哪些行上出现分页符:keepRowsTogether API

如果您想将 5 行的每个“ block ”保留在一起,则将这些 block 创建为单独的子表并在每个子表上使用 subtable.setKeepTogether(true) 可能会更容易: setKeepTogether API 。您可以将子表添加到 1 个主表中,也可以直接将它们添加到文档中。

关于java - iText - 如果特定行位于页面底部,则在下一页上设置特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33187733/

相关文章:

pdf - SVG linestroke-dasharray 无法正确打印到 PDF

c# - 如何使用 C# 和 MVC 2 从 SQL 数据库生成 PDF/Excel 文件?

java - 尝试使用 UTF-8 编码 Files.write(..) 但出现 OutOfMemoryError

java - 非常简单的Gradle Java项目,找不到依赖项

java - 私有(private)成员变量在 dispatchTouchEvent 中的 Nexus 5x 上变为空

javascript - 如何销毁 PDFJS 对象?

java - 手动格式化 Java double

r - 我运行 knit-minimal.lyx 得到 "LaTeX Error: File ` 图/最小无聊图 1' not found."

linux - 在 LAMP 环境中从 XHTML 生成 PDF

google-chrome - PDF文档文本在IE/Firefox/Chrome浏览器中的显示方式有所不同