iText 5.5.9 を使用しています。の中PdfPTable
に表示する がありColumnText
ます。にColumnText
は、小さい列と大きい列があります。
テーブルが小さい列に対して大きすぎる場合は、2 番目の大きい列に移動する必要があります。これどうやってするの?
を使用してみtable.setKeepTogether(true)
ましたが、うまくいきません。また、このページ ( http://itext.2136553.n4.nabble.com/PdfPTable-KeepTogether-and-ColumnText-td2141501.html )からの提案を試して、テーブルを 2 番目のテーブル内にラップして使用し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 の最初のページに封筒ウィンドウ用のオプションのスペースがあるという考え方です。このウィンドウにはアドレスが表示されます。これにより、(最初の) ページの残りの部分が封筒ウィンドウの上と下の 2 つの領域に分割されます。sで解いてみましたColumnText
。封筒ウィンドウの上にきれいに表示されるタイトルを追加しました。次に、テーブルを追加しました。この表は、エンベロープ ウィンドウの上下に部分的に表示されています。したがって、これらのレターには表だけでなく、場合によっては複数の表や段落を含めることができます。