1
我在下面有一個工作代碼。不幸的是,某些行(標題行)有時可能位於頁面底部,在顯示期間不好。如果該行位於頁面底部,是否有任何方法可以在下一頁上設置特定的行?提前致謝。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());
}
}
}