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);
}
}
PdfFormField.VISIBLE은 7.0.2 이상에서 옵션이 될 수 있습니다 (이미 7.0.1에있을 수 있습니다. 확인하지 않았습니다). 귀하의 솔루션과 직감에 관해서는 0은 실제로 VISIBLE을 따르지 않으며, VISIBLE은 기본 옵션이며 4 이상은 역시 작동해야합니다. –
@SamuelHuylebroeck 의견에 감사드립니다. '0이 VISIBLE에 맞지 않는다'는 것은 무엇을 의미합니까? 양식 필드 값이 기본적으로 인쇄되지 않으므로 기본값은 VISIBLE이 아니지만 가시성을 0으로 설정하면됩니다. 4 이상을 시도하지는 않았지만 기회를 얻었을 때 결과를 게시 할 것입니다. – lucasvw