首页 文章

横向页面上的 iText TextField - 输入未旋转的文本

提问于
浏览
1

我正在使用 iText 生成 PDF 并希望包含 iText 表单和 TextFields,以便用户可以通过电子方式填写 PDF 表单,而不是将其打印出来。

我在这里遵循了这些例子:http://itextpdf.com/examples/iia.php?id=161

在这里:http://itextpdf.com/examples/iia.php?id=162

到目前为止,我的 Landscape PDF 上有一个 TextField,我可以点击并输入文字。

我有两个问题:

  • TextField 对齐方式。要单击文本字段,我必须将鼠标放在我添加 TextField 的单元格的右侧。如何将 TextField 与单元格对齐?

  • 输入文本轮换。输入文字后,它会旋转显示 90 度到页面的其余部分。如何设置此显示文本的旋转?

输入文本的旋转是最让我烦恼的事情。我已经尝试在单元格和 TextField 上设置旋转,但这似乎没有任何效果。

有什么建议?

谢谢

这是我的代码:

int rotation = document.getPageSize().getRotation();

    PdfFormField pdfForm = PdfFormField.createEmpty(docWriter);
    coverSheetPdfForm.setFieldName("Form");
    coverSheetPdfForm.setRotate(rotation);

    ...

    cell = new PdfPCell(borderedTable("Cell Title:", tblHeadingFont, "", borderColor));
    cell.setColspan(1);
    cell.setPadding(5f);
    cell.setBorderWidth(0f);
    cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    cell.setMinimumHeight(100f);

    TextField textField = new TextField(docWriter, new Rectangle(50, 50,rotation), "cell_text_field");
    textField.setFontSize(12);
    textField.setRotation(rotation);
    textField.setVisibility(TextField.VISIBLE);
    textField.setBorderColor(borderColor);
    textField.setBorderWidth(BORDER_WIDTH);

    cell.setCellEvent(new ChildFieldEvent(pdfForm, textField.getTextField(), 1, rotation));

    .....

    docWriter.addAnnotation(pdfForm);

和我从示例页面借来的 ChildFieldEvent 代码,并为旋转添加了一个额外的参数:

/**
 * Creates a ChildFieldEvent.
 * @param parent the parent field
 * @param kid the child field
 * @param padding a padding
 * @param rotation 
 */
public ChildFieldEvent(PdfFormField parent, PdfFormField kid, float padding, int rotation) {
    this.parent = parent;
    this.kid = kid;
    this.padding = padding;
    this.rotation = rotation;
}

/**
 * Add the child field to the parent, and sets the coordinates of the child field.
 * @param cell
 * @param rect
 * @param cb 
 * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell,
 *      com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[])
 */
@Override
public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb) {
    try {
        parent.addKid(kid);
        kid.setWidget(new Rectangle(rect.getLeft(padding), rect.getBottom(padding),
                rect.getRight(padding), rect.getTop(padding),rotation),
                PdfAnnotation.HIGHLIGHT_INVERT);
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

1 回答

  • 1

    我做了一些更多的测试,看起来 Ubuntu PDF 文件(文档查看器)没有正确地呈现 PDF,因为在 Windows Adobe PDF Viewer 中查看相同的文件,表单看起来很好。

    我会做更多的测试和报告。

相关问题