我正在使用 iText 5.5.9。我有一个PdfPTable
我在里面显示的ColumnText
。有ColumnText
一个小柱子和一个大柱子。
如果表对于小列来说太大,它应该移动到第二个更大的列。我怎样才能做到这一点?
我尝试使用table.setKeepTogether(true)
,但这不起作用。我还尝试了此页面(http://itext.2136553.n4.nabble.com/PdfPTable-KeepTogether-and-ColumnText-td2141501.html)的建议,将我的表包装在第二个表中并使用tableWrapper.setSplitRows(false)
。但如果我这样做,我就再也看不到这张桌子了。
这是我的代码:
public class PdfTest {
private static String filename = "C:/Users/Development/Documents/pdf_test.pdf";
public static void main(String[] args) {
try {
new PdfTest();
File f = new File(filename);
Desktop.getDesktop().open(f);
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
}
public PdfTest() throws DocumentException, IOException {
File file = new File(filename);
OutputStream os = new FileOutputStream(file);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, os);
document.open();
PdfContentByte canvas = writer.getDirectContent();
printPage1(document, canvas);
document.newPage();
printPage2(document, canvas);
document.close();
os.close();
}
private void printPage1(Document document, PdfContentByte canvas) throws DocumentException {
int cols = 3;
int rows = 15;
PdfPTable table = new PdfPTable(cols);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
table.addCell(new Phrase("Cell " + row + ", " + col));
}
}
Paragraph paragraph = new Paragraph();
paragraph.add(table);
ColumnText columnText = new ColumnText(canvas);
columnText.addElement(new Paragraph("This table should keep together!"));
columnText.addElement(paragraph);
int status = ColumnText.START_COLUMN;
Rectangle docBounds = document.getPageSize();
Rectangle bounds = new Rectangle(docBounds.getLeft(20), docBounds.getTop(20) - 200, docBounds.getRight(20), docBounds.getTop(20));
bounds.setBorder(Rectangle.BOX);
bounds.setBorderColor(BaseColor.BLACK);
bounds.setBorderWidth(1);
bounds.setBackgroundColor(new BaseColor(23, 142, 255, 20));
canvas.rectangle(bounds);
columnText.setSimpleColumn(bounds);
status = columnText.go();
if (ColumnText.hasMoreText(status)) {
bounds = new Rectangle(docBounds.getLeft(20), docBounds.getBottom(20), docBounds.getRight(20), docBounds.getBottom(20) + 600);
bounds.setBorder(Rectangle.BOX);
bounds.setBorderColor(BaseColor.BLACK);
bounds.setBorderWidth(1);
bounds.setBackgroundColor(new BaseColor(255, 142, 23, 20));
canvas.rectangle(bounds);
columnText.setSimpleColumn(bounds);
status = columnText.go();
}
}
private void printPage2(Document document, PdfContentByte canvas) throws DocumentException {
int cols = 3;
int rows = 15;
PdfPTable table = new PdfPTable(cols);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
table.addCell(new Phrase("Cell " + row + ", " + col));
}
}
PdfPTable tableWrapper = new PdfPTable(1);
tableWrapper.addCell(table);
tableWrapper.setSplitRows(false);
Paragraph paragraph = new Paragraph();
paragraph.add(tableWrapper);
ColumnText columnText = new ColumnText(canvas);
columnText.addElement(new Paragraph("This table should keep together!"));
columnText.addElement(tableWrapper);
int status = ColumnText.START_COLUMN;
Rectangle docBounds = document.getPageSize();
Rectangle bounds = new Rectangle(docBounds.getLeft(20), docBounds.getTop(20) - 200, docBounds.getRight(20), docBounds.getTop(20));
bounds.setBorder(Rectangle.BOX);
bounds.setBorderColor(BaseColor.BLACK);
bounds.setBorderWidth(1);
bounds.setBackgroundColor(new BaseColor(23, 142, 255, 20));
canvas.rectangle(bounds);
columnText.setSimpleColumn(bounds);
status = columnText.go();
if (ColumnText.hasMoreText(status)) {
bounds = new Rectangle(docBounds.getLeft(20), docBounds.getBottom(20), docBounds.getRight(20), docBounds.getBottom(20) + 600);
bounds.setBorder(Rectangle.BOX);
bounds.setBorderColor(BaseColor.BLACK);
bounds.setBorderWidth(1);
bounds.setBackgroundColor(new BaseColor(255, 142, 23, 20));
canvas.rectangle(bounds);
columnText.setSimpleColumn(bounds);
status = columnText.go();
}
}
}
编辑:我现在意识到我问的是解决方案,而不是问题本身。所以我要退后一步。
这个想法是 PDF 在第一页上有一个用于信封窗口的可选空间。此窗口将显示一个地址。这会将(第一页)页面的其余部分分成信封窗口上方和下方的两个区域。ColumnText
我尝试用s解决这个问题。我添加了一个标题,它整齐地出现在信封窗口的上方。然后我添加了一个表。该表部分显示在信封窗口的上方和下方。所以这些字母不仅可以包含表格,还可以包含多个表格和段落。