-1

I'm using itext to generate editable Calendar pdf.

Im trying to add TextField to the PdfPCell using this code,

//To create PdfPCell for a specific day

public PdfPCell getDayCell(Calendar calendar, Locale locale) {
    PdfPCell cell = new PdfPCell();
    cell.setPadding(3);
    // set the background color, based on the type of day
    if (isSunday(calendar))
        cell.setBackgroundColor(BaseColor.GRAY);
    else if (isSpecialDay(calendar))
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
    else
        cell.setBackgroundColor(BaseColor.WHITE);
    // set the content in the language of the locale
    Chunk chunk = new Chunk(String.format(locale, "%1$ta", calendar), small);
    chunk.setTextRise(5);
    // a paragraph with the day
    Paragraph p = new Paragraph(chunk);
    // a separator
    p.add(new Chunk(new VerticalPositionMark()));
    // and the number of the day
    p.add(new Chunk(String.format(locale, "%1$te", calendar), normal));
    cell.addElement(p);
    cell.setCellEvent(new MyCellField(locale+""+calendar));
    cell.setFixedHeight(80);
    return cell;
}

// Adding TextField to the cellEvent

class MyCellField implements PdfPCellEvent {
protected String fieldname;
public MyCellField(String fieldname) {
    this.fieldname = fieldname;
}
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {

    final PdfWriter writer = canvases[0].getPdfWriter();


    final TextField textField = new TextField(writer, rectangle, fieldname);
    textField.setAlignment(Element.ALIGN_TOP); 
    textField.setOptions(TextField.MULTILINE); 
    try {
        final PdfFormField field = textField.getTextField();
        writer.addAnnotation(field);
    } catch (final IOException ioe) {
        throw new ExceptionConverter(ioe);
    } catch (final DocumentException de) {
        throw new ExceptionConverter(de);
    }
}

}

When I render the calendar pdf, the Cell focus is vertical not horizontal. Kindly help me to find out what I'm missing.

Kindly refer the image

NOTE: Please don't negative vote, I really want to figure out how to solve this. I referred other links like ITextSharp - text field in PdfPCell which where not helpful.

I tried adding

float textboxheight = 12f;
Rectangle rect = rectangle;
rect.Bottom = rect.Top - textboxheight;

rect.Bottom is showing error "The final field Rectangle.BOTTOM cannot be assigned".

Im using iText5

mkl
  • 90,588
  • 15
  • 125
  • 265
ir05
  • 13
  • 4
  • How do you set the page size to landscape? Do you use a rotated page size in your `Document` constructor or do you do that in a different manner? – mkl Oct 23 '17 at 12:55
  • I have used like this Document document = new Document(PageSize.A4.rotate()); – ir05 Oct 23 '17 at 12:58
  • I just tried using Document document = new Document(PageSize.A4); the focus is fixed, but calendar cells are going out of the page – ir05 Oct 23 '17 at 13:04
  • https://ibb.co/g8ZwO6 – ir05 Oct 23 '17 at 13:36

1 Answers1

1

I think it's odd, because the behavior you describe isn't supposed to happen in the official iText version. (That makes me wonder: where did you get your version?)

However, you could try replacing this line:

Document document = new Document(PageSize.A4);

With this line:

Document document = new Document(new Rectangle(842, 595));
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165