2016-09-26 17 views
0

ボタンの表があるフォームを作成しています。以下はその考え方を示しています。ラジオボタンの表の不思議な表示

   Good   OK   Bad 

Item1  [button] [button] [button] 
Item2  [button] [button] [button] 

各行はボタングループです。

私のプログラムは、上記のような小さなテーブルでも動作します。しかし、行または列が複数のページを保持する場合、表示は奇妙です。これは私のプログラムです:ときtable.setKeepTogether(false)ここ

PdfPTable table = new PdfPTable(colHeaders.size() + 1 ); 
table.setWidthPercentage(100); 
PdfPCell cell; 

table.setSpacingBefore(10); 
table.setSpacingAfter(0); 
table.setKeepTogether(false); //tested "true" different display error 

//upper-left corner empty cell 
cell = new CustomPdfPCell(); 
cell.setBackgroundColor(BaseColor.LIGHT_GRAY); 
table.addCell(cell); 

//header row 
for (TableOfChoicesHeader c: colHeaders) { 
    String text = c.getText(); 
    cell = new PdfPCell(new Phrase(text)); 
    cell.setBackgroundColor(BaseColor.LIGHT_GRAY); 
    table.addCell(cell); 
} 

List<PdfFormField> radioGroups= new ArrayList<PdfFormField>(); 

//for each row 
for (TableOfChoicesHeader r: rowHeaders) { 

    //row header 
    String rowText = r.getText(); 
    cell = new PdfPCell(new Phrase(rowText)); 
    cell.setBackgroundColor(BaseColor.LIGHT_GRAY); 
    table.addCell(cell); 

//for the current row row 
String fieldName = "question" + q.getId() +"_" + r.getId().toString(); 

PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true); 
radiogroup.setFieldName(fieldName); 

radioGroups.add(radiogroup); 

for (TableOfChoicesHeader c: colHeaders) {  
    cell = new PdfPCell(); 
    cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
    cell.setPadding(5); 
    cell.setCellEvent(new CheckboxCellEvent(fieldName, r.getId() + "_" + c.getId())); 
    table.addCell(cell);     
    } 

} 
document.add(table); 

for (PdfFormField g: radioGroups) { 
    writer.addAnnotation(g); 
} 

はスクリーンショットです。表示は少し異なりますが、値がtrueの場合はまだ変です。

ページ1 enter image description here

2ページ enter image description here

更新

サミュエルのアプローチに従うことにより、私は、最初のページに表示されるラジオボタンを見ることができています。ただし、最初のページのボタンが2ページ目に表示されました。スクリーンショットを参照してください。

enter image description here

enter image description here

アップデート私は追加することにより、作業それを得た2

MyCellFieldに次の代わりに

writer.addAnnotation(radiogroup); 

PdfFormField field = radio.getRadioField(); 
writer.addAnnotation(field); 

理由を説明できません。 SamuelとBrunoに助けてくれてありがとう!

答えて

2

あなたのコードをもう一度見て、ラジオボタンのアノテーションを最後に追加する理由はありますか? セルの行を追加したときにアノテーションを追加しようとしましたか?このような何か:

for (TableOfChoicesHeader r: rowHeaders) { 

    //row header 
    String rowText = r.getText(); 
    cell = new PdfPCell(new Phrase(rowText)); 
    cell.setBackgroundColor(BaseColor.LIGHT_GRAY); 
    table.addCell(cell); 

    //for the current row row 
    String fieldName = "question" + q.getId() +"_" + r.getId().toString(); 

    PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true); 
    radiogroup.setFieldName(fieldName); 

    radioGroups.add(radiogroup); 

    for (TableOfChoicesHeader c: colHeaders) {  
     cell = new PdfPCell(); 
     cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
     cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
     cell.setPadding(5); 
     cell.setCellEvent(new CheckboxCellEvent(fieldName, r.getId() + "_" + c.getId())); 
     table.addCell(cell);     
    } 
    writer.addAnnotation(radiogroup) 

} 

EDIT:複数のページ上のラジオグループを配布する

ブルーノが言ったように、例がRadioGroupMultiPage1RadioGroupMultiPage2です。テーブル全体にラジオグループを配信する例は、Distributing radio buttons of a radio field across multiple PdfCellsです。

EDIT 2、電気Bogaloo:いくつかのstub-で(私はbasicly上記の質問FRMあなたのコードを取った enter image description here

はあなたのコードとブルーノが指摘した例に基づいて簡単な例をWippedカスタムデータクラスの実装)、および RadioGroupMultiPage2で見つかった MyCellField PdfCellEvent拡張子を使用:

public MyCellField(PdfWriter writer, PdfFormField radiogroup, String value) { 
     this.writer = writer; 
     this.radiogroup = radiogroup; 
     this.value = value; 
    } 

    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { 
     try { 
      RadioCheckField radio = new RadioCheckField(writer, position, null, value); 
      PdfFormField field = radio.getRadioField(); 
      writer.addAnnotation(field); 
      radiogroup.addKid(field); 
     } catch (IOException ex) { 
      // do nothing 
     } catch (DocumentException ex) { 
      // do nothing 
     } 
    } 
+0

サミュエルを、私はポストを作る前にそれをテストしました。 document.add(table)の前にそれを行うと、ラジオボタンはまったく表示されません。思考をありがとう! – curious1

+1

Samuelとcurious1さん、[RadioGroupMultiPage1](http://developers.itextpdf.com/examples/form-examples-itext5/creating-form-fields#1177-radiogroupmultipage1.java)と[RadioGroupMultiPage2]( http://developers.itextpdf.com/examples/form-examples/create-fields-table#1178-radiogroupmultipage2.java)。異なるページにラジオボタンを配布するときは、特別な注意が必要です。 –

+0

こんにちは、あなたのご意見ありがとうございます。 2つの例を見て、ここでどのように役立つのか理解できません。基本的に、最初の例では、ラジオボタンを配置するページを手動で指定しています。 2番目の例では、私の状況と同様のページングを直接処理しませんが、私は奇妙な表示をしました。私がしたことをより具体的に教えてください。間違っていますか?ベスト。 – curious1

関連する問題