iText 5在段落下添加文本字段

时间:2017-07-20 14:50:24

标签: itext textfield

我正在使用iText在我的java spring mvc web项目中创建PDF。我是iText 5的新手。有人能告诉我如何在段落下添加文本字段吗?根据我当前的代码,文本字段不会显示在段落下。我可能会向PDF添加更多问题,因此我想在段落下显示每个文本字段。

try {
    List<FormQuestionBean> questions = formBuilderBean.getQuestions();
    MyHeaderAndFooter event = new MyHeaderAndFooter();
    Document pdfDocument = new Document(PageSize.A4);
    pdfDocument.setMargins(20, 20, 20, 30);
    ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(pdfDocument, pdfBaos);
    writer.setPageEvent(event);
    pdfDocument.open();
    Paragraph paragraph1 = new Paragraph(FORM_NAME + " : " + formBuilderBean.getName(), HEADING);
    paragraph1.setSpacingAfter(20);
    pdfDocument.add(paragraph1);

    for (FormQuestionBean formQuestionBean: questions) {
        if (formQuestionBean.getFieldImplementation().contentEquals("Text Field")) {
            Paragraph p = new Paragraph(formQuestionBean.getName());
            pdfDocument.add(p);
            TextField text = new TextField(writer, new Rectangle(36, 788, 559, 806), "text" );             
            text.setBackgroundColor(BaseColor.WHITE);
            PdfFormField field = text.getTextField();
            writer.addAnnotation(field);
        }

    }

    pdfDocument.close();
    response.reset();
    response.setHeader("Content-Length", String.valueOf(pdfBaos.size()));
    response.setContentType("application/pdf");
    response.setContentLength(pdfBaos.size());
    String downloadName = getFormFileName(".pdf");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadName + "\"");
    OutputStream outputStream = response.getOutputStream();
    pdfBaos.writeTo(outputStream);
    outputStream.flush();
    outputStream.close();
    return true;
} catch (Exception e) {
    logger.error(e);
    e.printStackTrace();
    return false;
}

enter image description here

1 个答案:

答案 0 :(得分:1)

我想出了使用表格和单元格。以下是代码:

try {
        List<FormQuestionBean> questions = formBuilderBean.getQuestions();
        MyHeaderAndFooter event = new MyHeaderAndFooter();
        Document pdfDocument = new Document(PageSize.A4);
        pdfDocument.setMargins(20, 20, 20, 30);
        ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(pdfDocument, pdfBaos);
        writer.setPageEvent(event);
        pdfDocument.open();
        Paragraph paragraph1 = new Paragraph(FORM_NAME + " : " + formBuilderBean.getName(), HEADING);
        paragraph1.setSpacingAfter(20);
        pdfDocument.add(paragraph1);

        for (FormQuestionBean formQuestionBean: questions) {
            if (formQuestionBean.getFieldImplementation().contentEquals("Text Field")) {
                PdfPTable table = new PdfPTable(1);
                table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                PdfPCell c1 = new PdfPCell(new Phrase(formQuestionBean.getName()));
                c1.setFixedHeight(30);
                table.addCell(c1);
                PdfPCell c2 = new PdfPCell();
                TextField text = new TextField(writer, c2, "text" );               
                text.setBackgroundColor(BaseColor.WHITE);
                FieldPositioningEvents events = new FieldPositioningEvents(writer, text.getTextField()); 
                c2.setCellEvent(events);
                c2.setFixedHeight(30);
                table.addCell(c2);
                pdfDocument.add(table);

            }
        }
        pdfDocument.close();
        response.reset();
        response.setHeader("Content-Length", String.valueOf(pdfBaos.size()));
        response.setContentType("application/pdf");
        response.setContentLength(pdfBaos.size());
        String downloadName = getFormFileName(".pdf");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadName + "\"");
        OutputStream outputStream = response.getOutputStream();
        pdfBaos.writeTo(outputStream);
        outputStream.flush();
        outputStream.close();
        return true;
    } catch (Exception e) {
        logger.error(e);
        e.printStackTrace();
        return false;
    }