有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 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());
    }





    }



}

共 (0) 个答案