Hi Mark,

thanks for your reply.

 PS: This code won't create radio/check fields.

This code was for TextFields.

My problems (still) are:
- Text and checkbox-fields are modifiable
- When I view the pdf in a viever like Foxit-Reader or on Mac the radio and 
check-boxes aren't checked anymore. Acrobat is working.

My stripped code bellow:

package com.foo;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.GrayColor;
import com.lowagie.text.pdf.PdfBorderDictionary;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RadioCheckField;
import com.lowagie.text.pdf.TextField;

public class TestPDF
{

  public final int FIELD_TYPE_TEXTAREA = 1;

  public final int FIELD_TYPE_RADIO = 2;

  public final int FIELD_TYPE_CHECKBOX = 3;

  private static String FILE = "c:/temp/pdf/fields.pdf" + new Random().nextInt() + 
".pdf";

  public TestPDF() throws DocumentException, IOException
  {
    Document document = new Document(PageSize.A4, 20, 20, 55, 35);

    PdfWriter writer = PdfWriter.getInstance(document, new 
FileOutputStream(FILE));

    document.open();
    addContent(document, writer);
    document.close();
  }

  private void addContent(Document document, PdfWriter writer) throws 
DocumentException, IOException
  {
    PdfPTable table = new PdfPTable(2);
    table.setKeepTogether(true);
    table.setWidths(new int[] { 5, 130 });
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorderWidth(-1);

    PdfPCell defaultContentCell = table.getDefaultCell();

    // TEXT FIELD
    PdfFormField textField = PdfFormField.createEmpty(writer);
    textField.setFieldName("textField");
    textField.setFieldFlags(PdfFormField.FF_READ_ONLY);

    PdfPCell textCell = new PdfPCell(defaultContentCell);
    textCell.setColspan(2);
    textCell.setMinimumHeight(40);
    textCell.setCellEvent(new CellField(writer, textField, "textFieldCell", 
FIELD_TYPE_TEXTAREA,
        "test test test test \test test test test"));
    table.addCell(textCell);

    // RADIO BUTTON FIELD
    PdfFormField radiogroupField = PdfFormField.createRadioButton(writer, true);
    radiogroupField.setFieldName("radioGroup");
    radiogroupField.setFieldFlags(PdfFormField.FF_READ_ONLY);

    for (int i = 0; i < 3; i++)
    {
      PdfPCell radioCell = new PdfPCell(defaultContentCell);
      radioCell.setCellEvent(new CellField(writer, radiogroupField, 
"radioGroupCell" + i, FIELD_TYPE_RADIO, i == 1,
          "text" + i));
      table.addCell(radioCell);
      table.addCell("radio " + i);
    }

    // CHECKBOX FIELD
    PdfFormField checkboxGroupField = PdfFormField.createCheckBox(writer);
    checkboxGroupField.setFieldFlags(PdfFormField.FF_READ_ONLY);
    checkboxGroupField.setFieldName("checkboxGroup");
    for (int i = 0; i < 3; i++)
    {
      PdfPCell radioCell = new PdfPCell(defaultContentCell);
      radioCell.setCellEvent(new CellField(writer, radiogroupField, 
"checkboxGroupCell" + i, FIELD_TYPE_CHECKBOX,
          i == 1, "text" + i));
      table.addCell(radioCell);
      table.addCell("checkbox " + i);
    }

    document.add(table);
    writer.addAnnotation(checkboxGroupField);
    writer.addAnnotation(radiogroupField);
    writer.addAnnotation(textField);

  }

  protected class CellField implements PdfPCellEvent
  {
    private PdfFormField parent;

    private String partialFieldName;

    private PdfWriter writer;

    private int type;

    private boolean checked;

    private String text;

    public CellField(PdfWriter writer, PdfFormField parent, String name, int 
type, String text)
    {
      this.writer = writer;
      this.parent = parent;
      this.partialFieldName = name;
      this.type = type;
      this.text = text;
    }

    public CellField(PdfWriter writer, PdfFormField parent, String name, int 
type, boolean checked, String text)
    {
      this.writer = writer;
      this.parent = parent;
      this.partialFieldName = name;
      this.type = type;
      this.checked = checked;
      this.text = text;
    }

    public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb)
    {
      try
      {
        if (FIELD_TYPE_RADIO == type)
        {
          createRadioField(rect);
        }
        else if (FIELD_TYPE_TEXTAREA == type)
        {
          createTextAreaField(rect);
        }
        else if (FIELD_TYPE_CHECKBOX == type)
        {
          createCheckboxField(rect);
        }
      }
      catch (Exception e)
      {
        throw new RuntimeException(e);
      }

    }

    private void createTextAreaField(Rectangle rect) throws IOException, 
DocumentException
    {
      int options = TextField.MULTILINE | TextField.DO_NOT_SPELL_CHECK;
      addTextField(rect, 500, options);
    }

    private void addTextField(Rectangle rect, int width, int options) throws 
IOException, DocumentException
    {
      TextField tf = new TextField(writer, new Rectangle(rect.getLeft(2), 
rect.getBottom(2), width, rect.getTop(2)),
          partialFieldName);
      tf.setBorderColor(Color.BLACK);
      tf.setBorderWidth(1);
      tf.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
      tf.setText(text);
      tf.setOptions(options);
      parent.addKid(tf.getTextField());
    }

    private void createRadioField(Rectangle rect) throws IOException, 
DocumentException
    {
      RadioCheckField rf = new RadioCheckField(writer, new 
Rectangle(rect.getLeft(4), rect.getBottom(4),
          rect.getRight(4), rect.getTop(4)), partialFieldName, "");
      rf.setChecked(checked);
      rf.setBorderColor(GrayColor.GRAYBLACK);
      rf.setBackgroundColor(GrayColor.GRAYWHITE);
      rf.setCheckType(RadioCheckField.TYPE_CIRCLE);
      parent.addKid(rf.getRadioField());
    }

    private void createCheckboxField(Rectangle rect) throws IOException, 
DocumentException
    {
      RadioCheckField rf = new RadioCheckField(writer, new 
Rectangle(rect.getLeft(4), rect.getBottom(4),
          rect.getRight(4), rect.getTop(4)), partialFieldName, "");
      rf.setChecked(checked);
      rf.setBorderColor(GrayColor.GRAYBLACK);
      rf.setBackgroundColor(GrayColor.GRAYWHITE);
      rf.setCheckType(RadioCheckField.TYPE_CHECK);
      parent.addKid(rf.getCheckField());
    }
  }

  /**
   * Main method
   * @param args no arguments needed
   * @throws IOException
   * @throws DocumentException
   */
  public static void main(String[] args) throws IOException, DocumentException
  {
    TestPDF test = new TestPDF();
  }

}


Thanks
jonny

Am 27.12.2010 17:48, schrieb Mark Storer:
If your CellField event makes a copy, then setting the FF_READ_ONLY flag
after creating the event handler won't do you any good.

PS: This code won't create radio/check fields.  You should at the very
least be calling PdfFromField.createButton, createCheckBox or
createRadioButton.  That or we're missing some Important Code in
CellField.

--Mark Storer
   Senior Software Engineer
   Cardiff.com

import legalese.Disclaimer;
Disclaimer<Cardiff>  DisCard = null;



-----Original Message-----
From: Johannes Becker [mailto:[email protected]]
Sent: Monday, December 27, 2010 6:14 AM
To: Post all your questions about iText here
Subject: Re: [iText-questions] Unmodifiable
radiobuttons/checkboxes in table cell

Hi,

thanks to your help, I'm able now to to add my
checkboxes/radiobuttons/textfields to my table.

Only one more thing bugs me: I can't get my fields immutable
(except of radiobutton).

...
PdfFormField someField = PdfFormField.createEmpty(writer);
someField.setFieldName("xxx"); PdfPCell someCell = new
PdfPCell(); someCell.setCellEvent(new CellField(writer,
someField, cellFieldName, FIELD_TYPE_TEXT, someText);
someTable.addCell(someCell);
someField.setFieldFlags(PdfFormField.FF_READ_ONLY);
document.add(someTable);


Cheers
Jonny



Am 23.12.2010 13:01, schrieb 1T3XT BVBA:
Op 23/12/2010 10:25, Johannes Becker schreef:
    Please read chapter 8 for more info.
My problem is that im working with the 2007 book. :(
Read on until you reach p490.
There's a code sample on that page that shows how to:
get the connection between the PdfPCellEvent and how to add a
RadioCheckField into my cell.
Actually, the code sample adds a TextField, not a
RadioCheckField, but
it's easy to adapt the example.
More precise:
I dont't know how to get the rect out of the PdfPCellEvent, to
dynamically create a RadioCheckFields within the table. I
guess I'm missing something here.
As I explained in my previous answer:
The coordinates of the cell are stored in rect.
If I misinterpreted your question, and if you mean you don't
understand how cell events work, read section 10.2.2 in the First
Edition of "iText in Action" (that's the 2007 book).


----------------------------------------------------------------------
-------- Learn how Oracle Real Application Clusters (RAC) One Node
allows customers to consolidate database storage, standardize their
database environment, and, should the need arise, upgrade to a full
multi-node Oracle RAC database without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Many questions posted to this list can (and will) be
answered with a
reference to the iText book:http://www.itextpdf.com/book/  Please
check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php

--------------------------------------------------------------
----------------
Learn how Oracle Real Application Clusters (RAC) One Node
allows customers to consolidate database storage, standardize
their database environment, and, should the need arise,
upgrade to a full multi-node Oracle RAC database without
downtime or disruptionhttp://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Many questions posted to this list can (and will) be answered
with a reference to the iText book:
http://www.itextpdf.com/book/  Please check the keywords list
before you ask for examples:http://itextpdf.com/themes/keywords.php


------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Many questions posted to this list can (and will) be answered with a reference 
to the iText book:http://www.itextpdf.com/book/
Please check the keywords list before you ask for 
examples:http://itextpdf.com/themes/keywords.php


package com.foo;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.GrayColor;
import com.lowagie.text.pdf.PdfBorderDictionary;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RadioCheckField;
import com.lowagie.text.pdf.TextField;

public class TestPDF
{

  public final int FIELD_TYPE_TEXTAREA = 1;

  public final int FIELD_TYPE_RADIO = 2;

  public final int FIELD_TYPE_CHECKBOX = 3;

  private static String FILE = "c:/temp/pdf/fields.pdf" + new 
Random().nextInt() + ".pdf";

  public TestPDF() throws DocumentException, IOException
  {
    Document document = new Document(PageSize.A4, 20, 20, 55, 35);

    PdfWriter writer = PdfWriter.getInstance(document, new 
FileOutputStream(FILE));

    document.open();
    addContent(document, writer);
    document.close();
  }

  private void addContent(Document document, PdfWriter writer) throws 
DocumentException, IOException
  {
    PdfPTable table = new PdfPTable(2);
    table.setKeepTogether(true);
    table.setWidths(new int[] { 5, 130 });
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorderWidth(-1);

    PdfPCell defaultContentCell = table.getDefaultCell();

    // TEXT FIELD
    PdfFormField textField = PdfFormField.createEmpty(writer);
    textField.setFieldName("textField");
    textField.setFieldFlags(PdfFormField.FF_READ_ONLY);

    PdfPCell textCell = new PdfPCell(defaultContentCell);
    textCell.setColspan(2);
    textCell.setMinimumHeight(40);
    textCell.setCellEvent(new CellField(writer, textField, "textFieldCell", 
FIELD_TYPE_TEXTAREA,
        "test test test test \test test test test"));
    table.addCell(textCell);

    // RADIO BUTTON FIELD
    PdfFormField radiogroupField = PdfFormField.createRadioButton(writer, true);
    radiogroupField.setFieldName("radioGroup");
    radiogroupField.setFieldFlags(PdfFormField.FF_READ_ONLY);

    for (int i = 0; i < 3; i++)
    {
      PdfPCell radioCell = new PdfPCell(defaultContentCell);
      radioCell.setCellEvent(new CellField(writer, radiogroupField, 
"radioGroupCell" + i, FIELD_TYPE_RADIO, i == 1,
          "text" + i));
      table.addCell(radioCell);
      table.addCell("radio " + i);
    }

    // CHECKBOX FIELD
    PdfFormField checkboxGroupField = PdfFormField.createCheckBox(writer);
    checkboxGroupField.setFieldFlags(PdfFormField.FF_READ_ONLY);
    checkboxGroupField.setFieldName("checkboxGroup");
    for (int i = 0; i < 3; i++)
    {
      PdfPCell radioCell = new PdfPCell(defaultContentCell);
      radioCell.setCellEvent(new CellField(writer, radiogroupField, 
"checkboxGroupCell" + i, FIELD_TYPE_CHECKBOX,
          i == 1, "text" + i));
      table.addCell(radioCell);
      table.addCell("checkbox " + i);
    }

    document.add(table);
    writer.addAnnotation(checkboxGroupField);
    writer.addAnnotation(radiogroupField);
    writer.addAnnotation(textField);

  }

  protected class CellField implements PdfPCellEvent
  {
    private PdfFormField parent;

    private String partialFieldName;

    private PdfWriter writer;

    private int type;

    private boolean checked;

    private String text;

    public CellField(PdfWriter writer, PdfFormField parent, String name, int 
type, String text)
    {
      this.writer = writer;
      this.parent = parent;
      this.partialFieldName = name;
      this.type = type;
      this.text = text;
    }

    public CellField(PdfWriter writer, PdfFormField parent, String name, int 
type, boolean checked, String text)
    {
      this.writer = writer;
      this.parent = parent;
      this.partialFieldName = name;
      this.type = type;
      this.checked = checked;
      this.text = text;
    }

    public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb)
    {
      try
      {
        if (FIELD_TYPE_RADIO == type)
        {
          createRadioField(rect);
        }
        else if (FIELD_TYPE_TEXTAREA == type)
        {
          createTextAreaField(rect);
        }
        else if (FIELD_TYPE_CHECKBOX == type)
        {
          createCheckboxField(rect);
        }
      }
      catch (Exception e)
      {
        throw new RuntimeException(e);
      }

    }

    private void createTextAreaField(Rectangle rect) throws IOException, 
DocumentException
    {
      int options = TextField.MULTILINE | TextField.DO_NOT_SPELL_CHECK;
      addTextField(rect, 500, options);
    }

    private void addTextField(Rectangle rect, int width, int options) throws 
IOException, DocumentException
    {
      TextField tf = new TextField(writer, new Rectangle(rect.getLeft(2), 
rect.getBottom(2), width, rect.getTop(2)),
          partialFieldName);
      tf.setBorderColor(Color.BLACK);
      tf.setBorderWidth(1);
      tf.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
      tf.setText(text);
      tf.setOptions(options);
      parent.addKid(tf.getTextField());
    }

    private void createRadioField(Rectangle rect) throws IOException, 
DocumentException
    {
      RadioCheckField rf = new RadioCheckField(writer, new 
Rectangle(rect.getLeft(4), rect.getBottom(4),
          rect.getRight(4), rect.getTop(4)), partialFieldName, "");
      rf.setChecked(checked);
      rf.setBorderColor(GrayColor.GRAYBLACK);
      rf.setBackgroundColor(GrayColor.GRAYWHITE);
      rf.setCheckType(RadioCheckField.TYPE_CIRCLE);
      parent.addKid(rf.getRadioField());
    }

    private void createCheckboxField(Rectangle rect) throws IOException, 
DocumentException
    {
      RadioCheckField rf = new RadioCheckField(writer, new 
Rectangle(rect.getLeft(4), rect.getBottom(4),
          rect.getRight(4), rect.getTop(4)), partialFieldName, "");
      rf.setChecked(checked);
      rf.setBorderColor(GrayColor.GRAYBLACK);
      rf.setBackgroundColor(GrayColor.GRAYWHITE);
      rf.setCheckType(RadioCheckField.TYPE_CHECK);
      parent.addKid(rf.getCheckField());
    }
  }

  /**
   * Main method
   * @param args no arguments needed
   * @throws IOException
   * @throws DocumentException
   */
  public static void main(String[] args) throws IOException, DocumentException
  {
    TestPDF test = new TestPDF();
  }

}
------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and, 
should the need arise, upgrade to a full multi-node Oracle RAC database 
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to