为什么不打印iText 7表单字段值? [英] Why don't the iText 7 form field values print?

查看:183
本文介绍了为什么不打印iText 7表单字段值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF,其中包含使用iText 7生成的表单.但是,当我填写该表单并尝试打印时,表单值不会显示.仅表单大纲显示.如何生成打印时将显示值的表单字段?

示例表格生成器:

import java.io.FileNotFoundException;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;

/**
 * @author Lucas Vander Wal
 *
 */
public final class TestForm {

    private static final Logger log = LoggerFactory.getLogger(TestForm.class);

    private static final PageSize LETTER_SIZE = new PageSize(612, 792),
            LETTER_SIZE_LANDSCAPE = LETTER_SIZE.rotate();

    private static final PdfFont FONT;
    static {
        try {
            FONT = PdfFontFactory.createFont();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static final float FONT_SIZE_12 = 12;

    /**
     * @param args
     */
    public static void main(String[] args) {
        log.info("Running pdf generation...");
        new TestForm().generate("formTest.pdf");
        log.info("Done with pdf generation.");
    }

    private Document doc;
    private PdfDocument pdfDoc;
    private PdfAcroForm form;

    public TestForm() {
    }

    /**
     * Generates the timesheet pdf and saves it to the given file location
     * 
     * @param outputFile
     */
    public void generate(String outputFile) {
        try {
            pdfDoc = new PdfDocument(new PdfWriter(outputFile));
            doc = new Document(pdfDoc, LETTER_SIZE_LANDSCAPE);
            // set document properties
            doc.setFontSize(10);
            float marginSize = doc.getTopMargin();
            doc.setMargins(marginSize / 2, marginSize, marginSize / 2, marginSize);

            form = PdfAcroForm.getAcroForm(pdfDoc, true);

            // build the form
            buildUserInfo();
            // close the document
            doc.close();

        } catch (FileNotFoundException e) {
            log.warn("Unable to save to file: " + outputFile, e);
        }

    }

    private void buildUserInfo() {
        // build the user info table
        Table userTable = new Table(4).setBorder(Border.NO_BORDER).setWidthPercent(100).setMargin(0).setPadding(0);
        // add 4 text entry fields
        this.addFieldToTable(userTable, "nameEmployee", "Name of Employee:");

        // add the table to the document
        this.doc.add(userTable);

    }

    private void addFieldToTable(Table t, String fieldName, String fieldLabel) {
        t.addCell(new Cell().add(fieldLabel).setPadding(5));
        Cell cell = new Cell().setPadding(5);
        cell.setNextRenderer(new CellRenderer(cell) {

            @Override
            public void draw(DrawContext drawContext) {
                super.draw(drawContext);
                PdfTextFormField field = PdfFormField.createText(
                        drawContext.getDocument(), getOccupiedAreaBBox().decreaseHeight(5), fieldName, "",
                        FONT, FONT_SIZE_12);
                form.addField(field);
            }

        });
        t.addCell(cell);
    }
}

解决方案

PDF表单字段的可见性设置会影响打印功能.有关更多信息,请参见此Adobe页面.. >

需要使用PdfFormField对象上的setVisibility()方法将表单字段设置为可见". 此iText 7文档解释了如何将表单设置为"HIDDEN","VISIBLE_BUT_DOES_NOT_PRINT"和"HIDDEN_BUT_PRINTABLE",但是并未提及将其设置为visible,实际上,visible不是PdfFormField中的静态选项之一.

上面列出的三个值分别对应于整数1、2和3,直觉上,我将可见性设置为0,并且打印时显示了表单字段!

总而言之,调用PdfFormField.setVisibility(0)创建可打印的表单字段.

I have a PDF with forms that was generated with iText 7. However, when I fill out the form and attempt to print, the form values do not show up; only the form outline shows. How do I generate form fields that will display the value when printed?

A sample form generator:

import java.io.FileNotFoundException;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;

/**
 * @author Lucas Vander Wal
 *
 */
public final class TestForm {

    private static final Logger log = LoggerFactory.getLogger(TestForm.class);

    private static final PageSize LETTER_SIZE = new PageSize(612, 792),
            LETTER_SIZE_LANDSCAPE = LETTER_SIZE.rotate();

    private static final PdfFont FONT;
    static {
        try {
            FONT = PdfFontFactory.createFont();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static final float FONT_SIZE_12 = 12;

    /**
     * @param args
     */
    public static void main(String[] args) {
        log.info("Running pdf generation...");
        new TestForm().generate("formTest.pdf");
        log.info("Done with pdf generation.");
    }

    private Document doc;
    private PdfDocument pdfDoc;
    private PdfAcroForm form;

    public TestForm() {
    }

    /**
     * Generates the timesheet pdf and saves it to the given file location
     * 
     * @param outputFile
     */
    public void generate(String outputFile) {
        try {
            pdfDoc = new PdfDocument(new PdfWriter(outputFile));
            doc = new Document(pdfDoc, LETTER_SIZE_LANDSCAPE);
            // set document properties
            doc.setFontSize(10);
            float marginSize = doc.getTopMargin();
            doc.setMargins(marginSize / 2, marginSize, marginSize / 2, marginSize);

            form = PdfAcroForm.getAcroForm(pdfDoc, true);

            // build the form
            buildUserInfo();
            // close the document
            doc.close();

        } catch (FileNotFoundException e) {
            log.warn("Unable to save to file: " + outputFile, e);
        }

    }

    private void buildUserInfo() {
        // build the user info table
        Table userTable = new Table(4).setBorder(Border.NO_BORDER).setWidthPercent(100).setMargin(0).setPadding(0);
        // add 4 text entry fields
        this.addFieldToTable(userTable, "nameEmployee", "Name of Employee:");

        // add the table to the document
        this.doc.add(userTable);

    }

    private void addFieldToTable(Table t, String fieldName, String fieldLabel) {
        t.addCell(new Cell().add(fieldLabel).setPadding(5));
        Cell cell = new Cell().setPadding(5);
        cell.setNextRenderer(new CellRenderer(cell) {

            @Override
            public void draw(DrawContext drawContext) {
                super.draw(drawContext);
                PdfTextFormField field = PdfFormField.createText(
                        drawContext.getDocument(), getOccupiedAreaBBox().decreaseHeight(5), fieldName, "",
                        FONT, FONT_SIZE_12);
                form.addField(field);
            }

        });
        t.addCell(cell);
    }
}

解决方案

PDF form fields have a visibility setting that affects the ability to print. See this Adobe page for more information.

The form fields need to be set to 'VISIBLE', using the setVisibility() method on PdfFormField objects. This iText 7 documentation explains how to set forms to 'HIDDEN','VISIBLE_BUT_DOES_NOT_PRINT', and 'HIDDEN_BUT_PRINTABLE', however it doesn't mention setting to visible, and in fact, visible is not one of the static options in PdfFormField.

The three values listed above correspond to the integers 1, 2, and 3, and on a hunch, I set the visibility to 0, and the form field displayed when printed!

In summary, call PdfFormField.setVisibility(0) to create form fields that are printable.

这篇关于为什么不打印iText 7表单字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆