Setting of choice form field using setValue Method
New for iText Core 7.1.11 is a change to how form fields are handled, specifically when /TI value is present, and when using SetValue for a choice form field.
Details
- Prior to 7.1.11, calling a setValue method on the choice field set the value incorrectly. It does not modify the I dictionary of the field.
- For the choice field, the I dictionary exists but is not filled in resulting in incorrect setting.
- After the fix, you will notice that the values are properly set for the choice field.
Below is some sample code to test the issue with iText 7.1.11 and previous versions:
JAVA
JAVA
package com.itext.choicefieldbug;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
public class ChoiceFieldExample {
private static final String SRC = "src/main/resources/choicefieldbug/";
private static String srcPdf = SRC + "choiceFieldsWithUnnecessaryIEntries.pdf";
private static String outPdf = SRC + "choiceFieldsSetValueTest.pdf";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
generatePDF();
}
public static void generatePDF() throws Exception {
PdfDocument pdfDocument = new PdfDocument(new PdfReader(srcPdf), new PdfWriter(outPdf));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDocument, false);
form.getField("First").setValue("First");
form.getField("Second").setValue("Second");
pdfDocument.close();
}
}
C#
C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Forms;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace Multichoicefield
{
public class ChoiceFieldExample
{
public static readonly string DEST = "results/test/output/choicefield/";
public static string srcPdf = DEST + "choiceFieldsWithUnnecessaryIEntries.pdf";
public static string outPdf = DEST + "choiceFieldsSetValueTest.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new ChoiceFieldExample().generatePDF();
}
public void generatePDF()
{
PdfDocument pdfDocument = new PdfDocument(new PdfReader(srcPdf), new PdfWriter(outPdf));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, false);
form.GetField("First").SetValue("First");
form.GetField("Second").SetValue("Second");
pdfDocument.Close();
}
}
}
Resources
choiceFieldsWithUnnecessaryIEntries.pdf