Get information from a form
This example was written in answer to the question Click How to check a check box?
checkboxvalues
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.acroforms;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import java.io.File;
import java.util.Map;
public class CheckBoxValues {
public static final String DEST = "./target/sandbox/acroforms/check_box_values.pdf";
public static final String SRC = "./src/main/resources/pdfs/checkboxExample.pdf";
public static final String CHECKED_FIELD_NAME = "checked";
public static final String UNCHECKED_FIELD_NAME = "unchecked";
public static final String CHECKED_STATE_VALUE = "Yes";
public static final String UNCHECKED_STATE_VALUE = "Off";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CheckBoxValues().manipulatePdf(DEST);
}
public void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true);
Map<String, PdfFormField> fields = form.getAllFormFields();
PdfFormField checkedField = fields.get(CHECKED_FIELD_NAME);
PdfFormField uncheckedField = fields.get(UNCHECKED_FIELD_NAME);
// Get array of possible values of the checkbox
String[] states = checkedField.getAppearanceStates();
// See all possible values in the console
for (String state : states) {
System.out.print(state + "; ");
}
// Search and set checked state to the previously unchecked checkbox and vice versa
for (String state : states) {
if (state.equals(CHECKED_STATE_VALUE)) {
uncheckedField.setValue(state);
} else if (state.equals(UNCHECKED_STATE_VALUE)) {
checkedField.setValue(state);
}
}
pdfDoc.close();
}
}
C#
C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Pdf;
namespace iText.Samples.Sandbox.Acroforms
{
public class CheckBoxValues
{
public static readonly String DEST = "results/sandbox/acroforms/check_box_values.pdf";
public static readonly String SRC = "../../../resources/pdfs/checkboxExample.pdf";
public static readonly String CHECKED_FIELD_NAME = "checked";
public static readonly String UNCHECKED_FIELD_NAME = "unchecked";
public static readonly String CHECKED_STATE_VALUE = "Yes";
public static readonly String UNCHECKED_STATE_VALUE = "Off";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new CheckBoxValues().ManipulatePdf(DEST);
}
public void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);
IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
PdfFormField checkedField = fields[CHECKED_FIELD_NAME];
PdfFormField uncheckedField = fields[UNCHECKED_FIELD_NAME];
// Get array of possible values of the checkbox
String[] states = checkedField.GetAppearanceStates();
// See all possible values in the console
foreach (String state in states)
{
Console.Write(state + "; ");
}
// Search and set checked state to the previously unchecked checkbox and vice versa
foreach (String state in states)
{
if (state.Equals(CHECKED_STATE_VALUE))
{
uncheckedField.SetValue(state);
}
else if (state.Equals(UNCHECKED_STATE_VALUE))
{
checkedField.SetValue(state);
}
}
pdfDoc.Close();
}
}
}