Change the position of a field

Tags: formsfield positionmove fieldInspect fieldsiText 7

These examples were written in answer to questions such as:

Files: 
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24404413/increase-width-of-acrofieldsitextsharp
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.pdf.annot.PdfWidgetAnnotation;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class ChangeFieldSize extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/acroforms/change_field_size.pdf";
    public static final String SRC = "./src/test/resources/pdfs/form.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ChangeFieldSize().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
 
        String value = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
                "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
        PdfFormField field = form.getField("Name");
        PdfWidgetAnnotation widgetAnnotation = field.getWidgets().get(0);
        PdfArray annotationRect = widgetAnnotation.getRectangle();
        annotationRect.set(2, new PdfNumber(annotationRect.getAsNumber(2).floatValue() + 20f));
        field.setValue(value);
        form.getField("Company").setValue(value);
 
        pdfDoc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/34439850
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class CheckBoxFlatten extends GenericTest {
    public static final String DEST
            = "./target/test/resources/sandbox/acroforms/checkbox_flatten.pdf";
    public static final String SRC
            = "./src/test/resources/pdfs/checkboxes.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CheckBoxFlatten().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
        PdfAcroForm.getAcroForm(pdfDoc, true).flattenFields();
        pdfDoc.close();
    }
}
File nameRaw URLUpdated
ChangeFieldSize.javaChangeFieldSize.java2016-08-10 11:51 am
CheckBoxFlatten.javaCheckBoxFlatten.java2016-08-03 2:53 pm
Resources: 
File nameRaw URLUpdated
form.pdfform.pdf2016-08-09 3:19 pm
state.pdfstate.pdf2016-08-10 11:52 am
Results: