Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using itext to generate editable Calendar pdf.

I'm trying to add TextField to the PdfPCell using this code,

//To create PdfPCell for a specific day

Java
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

Java
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.

Refer

NOTE: Im using iText5

What I have tried:

I have tried,

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


rect.Bottom is showing error "The final field Rectangle.BOTTOM cannot be assigned".
Posted
Comments
Richard MacCutchan 23-Oct-17 9:01am    
The message is quite clear, you cannot set a field that is declared final; check the documentation to see what options are available.
Member 13480314 23-Oct-17 9:34am    
Hi Richard,

Found issue

Document document = new Document(PageSize.A4.rotate()); "rotate()" was causing the issue. I changed it as Document document = new Document(PageSize.A4); the focus issue is fixed. But the cells are going out of the PDF. https://ibb.co/g8ZwO6
Richard MacCutchan 23-Oct-17 10:08am    
You need to do something to resize the content so it fits within the margins of the page size. I don't know iTextSharp so cannot suggest anything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900