下面有一个工作代码。不幸的是,某些行(Title Row)有时可能位于页面底部,这在显示期间不太好。如果行位于页面底部,是否可以在下一页设置特定行?提前谢谢。
/*
* 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());
}
}
}
发布于 2015-10-17 07:32:22
您可以使用table.keepRowsTogether()
指定不希望分页的行:keepRowsTogether API
如果您希望将每个5行的“块”放在一起,那么将这些块创建为单独的子表并在每个子表上使用subtable.setKeepTogether(true)
可能更容易一些。可以将子表添加到1个主表中,也可以将它们直接添加到文档中。
https://stackoverflow.com/questions/33187733
复制相似问题