将 TextField 添加到 PdfPCell 的问题

Issue with Adding TextField to PdfPCell

我正在使用 itext 生成可编辑的日历 pdf。

我正在尝试使用此代码将 TextField 添加到 PdfPCell,

//为特定日期创建 PdfPCell

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;
}

// 将 TextField 添加到 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);
    }
}

}

当我呈现日历 pdf 时,单元格焦点是垂直的而不是水平的。 请帮助我找出我所缺少的东西。

注意:请不要投反对票,我真的很想弄清楚如何解决这个问题。我提到了其他链接,例如 ITextSharp - text field in PdfPCell,但没有帮助。

我试过添加

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

rect.Bottom 显示错误 "The final field Rectangle.BOTTOM cannot be assigned"。

我正在使用 iText5

我认为这很奇怪,因为您描述的行为不应该发生在官方 iText 版本中。 (这让我想知道:你的版本是从哪里得到的?)

但是,您可以尝试替换此行:

Document document = new Document(PageSize.A4);

这一行:

Document document = new Document(new Rectangle(842, 595));