Java 将TextField添加到PdfPCell时出现问题

Java 将TextField添加到PdfPCell时出现问题,java,pdf,itext,pdf-generation,interactive,Java,Pdf,Itext,Pdf Generation,Interactive,我正在使用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 o

我正在使用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时,单元格焦点是垂直的,而不是水平的。 请帮我找出我遗漏了什么

注意:请不要投反对票,我真的想知道如何解决这个问题。我引用了其他一些没有帮助的链接

我试着加上

float textboxheight = 12f;
Rectangle rect = rectangle;
rect.Bottom = rect.Top - textboxheight;
rect.Bottom显示错误“无法分配最终字段Rectangle.Bottom”


我在使用iText5,我觉得这很奇怪,因为你描述的行为不应该发生在正式的iText版本中。(这让我想知道:你的版本是从哪里来的?)

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

Document document = new Document(PageSize.A4);
Document document = new Document(new Rectangle(842, 595));
这一行:

Document document = new Document(PageSize.A4);
Document document = new Document(new Rectangle(842, 595));

如何将页面大小设置为横向?您是在
文档中使用旋转页面大小,还是以不同的方式使用旋转页面大小?我使用了类似于此文档的Document=新文档(PageSize.A4.rotate());我刚刚尝试使用Document=新文档(PageSize.A4);焦点是固定的,但日历单元格将离开页面