2016-09-16 3 views
0

私はiText 5.5.9を使用しています。私はColumnTextの中に示すPdfPTableを持っています。 ColumnTextには、小さな列と大きな列があります。iTextテーブル内の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つの領域に分割します。私はColumnTextでそれを解決しようとしました。 タイトルを追加しましたが、これはエンベロープウィンドウの上にきれいに表示されます。次にテーブルを追加しました。この表は、エンベロープ・ウィンドウの上および下に部分的に表示されます。 これらの文字には、テーブルだけでなく複数のテーブルや段落を含むことができます。

+0

あなたはiTextの5の新しいバージョンで問題を再現しようとしたことがありますか?現在のリリースは5.5.9ですが、あなたが直面している問題はその間に修正されている可能性があります。 –

+0

また、すべての5.2.xバージョンに深刻な技術的問題があります。 5.2.0では、バイト位置に対して 'int'から' long'を使用するように変更しましたが、バグのために内部的に 'long'値が' int'に変更され、PDFが破損します。私たちはすべての5.2.xバージョンを撤回し、すべての顧客に警告し、[有料ユーザに通知しました](http://developers.itextpdf.com/content/changelogs/itext-520-521-fruaryruary9-2012-march- 31-2012)。まだそのバージョンを使用するのは無責任です。 –

+0

フィードバックありがとうございます。私はiTextのアップグレードを検討します。 – Dalendrion

答えて

0

私は最初にをシミュレーションモードで描画しようとするヘルパーメソッドを使用することを提案し、それを実際にフィットする最初の四角形に描画します。

このような方法は、次のようになります。このように使用した場合

Rectangle[] drawKeepTogether(Element element, PdfContentByte canvas, Rectangle... rectangles) throws DocumentException 
{ 
    int i = 0; 
    for (; i < rectangles.length; i++) 
    { 
     ColumnText columnText = new ColumnText(canvas); 
     columnText.addElement(element); 
     columnText.setSimpleColumn(rectangles[i]); 
     int status = columnText.go(true); 
     if (!ColumnText.hasMoreText(status)) 
      break; 
    } 

    if (i < rectangles.length) 
    { 
     Rectangle rectangle = rectangles[i]; 
     ColumnText columnText = new ColumnText(canvas); 
     columnText.addElement(element); 
     columnText.setSimpleColumn(rectangle); 
     columnText.go(false); 

     Rectangle[] remaining = new Rectangle[rectangles.length-i]; 
     System.arraycopy(rectangles, i, remaining, 0, remaining.length); 
     remaining[0] = new Rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getRight(), columnText.getYLine()); 
     return remaining; 
    } 

    return new Rectangle[0]; 
} 

TableKeepTogether.java方法drawKeepTogether

private void printPage3(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)); 
     } 
    } 
    table.setSpacingBefore(5); 

    Rectangle docBounds = document.getPageSize(); 
    Rectangle upper = new Rectangle(docBounds.getLeft(20), docBounds.getTop(20) - 200, docBounds.getRight(20), docBounds.getTop(20)); 
    upper.setBackgroundColor(new BaseColor(23, 142, 255, 20)); 
    Rectangle lower = new Rectangle(docBounds.getLeft(20), docBounds.getBottom(20), docBounds.getRight(20), docBounds.getBottom(20) + 600); 
    lower.setBackgroundColor(new BaseColor(255, 142, 23, 20)); 
    Rectangle[] rectangles = new Rectangle[] { upper, lower }; 

    for (Rectangle bounds : rectangles) 
    { 
     bounds.setBorder(Rectangle.BOX); 
     bounds.setBorderColor(BaseColor.BLACK); 
     bounds.setBorderWidth(1); 

     canvas.rectangle(bounds); 
    } 

    rectangles = drawKeepTogether(new Paragraph("This table should keep together!"), canvas, rectangles); 
    rectangles = drawKeepTogether(table, canvas, rectangles); 
} 

TableKeepTogether.java方法printPage3

この方法によって生成されたページには、次のようになります。

keepTogetherTableInColumnText.pdf page 3

関連する問題