Multiline fields

Tags: multilinetext fieldfield propertiesiText 5

Examples with multi-line text fields.

Files: 
/*
 * Example written by Bruno Lowagie in answer to
 * http://stackoverflow.com/questions/30233783/alignment-issue-with-multiline-text-form-fields-in-itextsharp
 */
package sandbox.acroforms;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class MultiLineField {
 
    public static final String DEST = "results/acroforms/multinewline.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new MultiLineField().createPdf(DEST);
    }
 
    public void createPdf(String filename) throws DocumentException, IOException {
        PdfReader reader = new PdfReader(createForm());
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
        AcroFields form = stamper.getAcroFields();
        form.setField("text", "A B C D E F\nG H I J K L M N\nO P Q R S T U\r\nV W X Y Z\n\nAlphabet street");
        stamper.setFormFlattening(true);
        stamper.close();
    }
 
    public byte[] createForm() throws DocumentException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.open();
        Rectangle rect = new Rectangle(36, 720, 144, 806);
        TextField tf = new TextField(writer, rect, "text");
        tf.setOptions(TextField.MULTILINE);
        writer.addAnnotation(tf.getTextField());
        document.close();
        return baos.toByteArray();
    }
}
/*
 * Example written by Bruno Lowagie in answer to
 * http://stackoverflow.com/questions/33808187/get-row-count-of-a-multiline-acrofiled
 */
package sandbox.acroforms;
 
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PRIndirectReference;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfString;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class MultiLineFieldCount {
 
    public static final String SRC = "resources/pdfs/multiline.pdf";
    public static final String DEST = "results/acroforms/multiline.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new MultiLineFieldCount().manipulatePdf(SRC, DEST);
    }
 
    public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        AcroFields form = stamper.getAcroFields();
        StringBuilder sb = new StringBuilder();
        for (String name : form.getFields().keySet()) {
            int n = getInformation(form, name);
            for (int i = 0; i < n; i++) {
                sb.append(" *");
            }
            String filler = sb.toString();
            form.setField(name, name + filler);
        }
        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
    }
 
    public int getInformation(AcroFields form, String name) {
        form.getFieldItem(name);
        AcroFields.Item item = form.getFieldItem(name);
        PdfDictionary dict = item.getMerged(0);
        PdfString da = dict.getAsString(PdfName.DA);
        Object[] da_values = AcroFields.splitDAelements(da.toUnicodeString());
        if (da_values == null) {
            System.out.println("No default appearance");
        }
        BaseFont bf = null;
        String font = (String)da_values[AcroFields.DA_FONT];
        if (font != null) {
            PdfDictionary dr = dict.getAsDict(PdfName.DR);
            if (dr != null) {
                PdfDictionary fontDict = dr.getAsDict(PdfName.FONT);
                bf = BaseFont.createFont((PRIndirectReference)fontDict.get(new PdfName(font)));
            }
        }
        if (bf == null) {
            System.out.println("No BaseFont");
        }
        else {
            System.out.println("Basefont: " + bf.getPostscriptFontName());
            System.out.println("Size: " + da_values[AcroFields.DA_SIZE]);
            Float size = (Float)da_values[AcroFields.DA_SIZE];
            if (size == 0)
                return 1000;
            Rectangle rect = form.getFieldPositions(name).get(0).position;
            float factor = bf.getFontDescriptor(BaseFont.BBOXURY, 1) - bf.getFontDescriptor(BaseFont.BBOXLLY, 1);
            int rows = Math.round(rect.getHeight() / (size * factor) + 0.5f);
            int columns = Math.round(rect.getWidth() / bf.getWidthPoint(" *", size) + 0.5f);
            System.out.println("height: " + rect.getHeight() + "; width: " + rect.getWidth());
            System.out.println("rows: " + rows + "; columns: " + columns);
            return rows * columns;
        }
        return 1000;
    }
}
/*
 * Example written by Bruno Lowagie.
 */
package sandbox.acroforms;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class ReadOnlyField {
 
    public static final String DEST = "results/acroforms/multilineField.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ReadOnlyField().createPdf(DEST);
    }
 
    public void createPdf(String filename) throws DocumentException, IOException {
        PdfReader reader = new PdfReader(createForm());
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
        AcroFields form = stamper.getAcroFields();
        form.setFieldProperty("text", "setfflags", TextField.READ_ONLY, null);
        form.setField("text", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
        stamper.close();
    }
 
    public byte[] createForm() throws DocumentException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.open();
        Rectangle rect = new Rectangle(36, 770, 144, 806);
        TextField tf = new TextField(writer, rect, "text");
        tf.setFontSize(20);
        tf.setOptions(TextField.MULTILINE);
        writer.addAnnotation(tf.getTextField());
        document.close();
        return baos.toByteArray();
    }
}
/*
 * Example written by Bruno Lowagie.
 */
package sandbox.acroforms;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class ReadOnlyField2 {
 
    public static final String DEST = "results/acroforms/multilineField2.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ReadOnlyField2().createPdf(DEST);
    }
 
    public void createPdf(String filename) throws DocumentException, IOException {
        PdfReader reader = new PdfReader(createForm());
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
        AcroFields form = stamper.getAcroFields();
        form.setFieldProperty("text1", "setfflags", TextField.READ_ONLY, null);
        form.setField("text1", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
        form.setFieldProperty("text2", "setfflags", TextField.READ_ONLY, null);
        form.setField("text2", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
        form.setFieldProperty("text3", "setfflags", TextField.READ_ONLY, null);
        form.setField("text3", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
        form.setFieldProperty("text4", "setfflags", TextField.READ_ONLY, null);
        form.setField("text4", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
        stamper.close();
    }
 
    public byte[] createForm() throws DocumentException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.open();
        Rectangle rect = new Rectangle(36, 770, 144, 806);
        writer.addAnnotation(getTextField(writer, rect, "text1"));
        rect = new Rectangle(148, 770, 256, 806);
        writer.addAnnotation(getTextField(writer, rect, "text2"));
        rect = new Rectangle(36, 724, 144, 760);
        writer.addAnnotation(getTextField(writer, rect, "text3"));
        rect = new Rectangle(148, 727, 256, 760);
        writer.addAnnotation(getTextField(writer, rect, "text4"));
        document.close();
        return baos.toByteArray();
    }
 
    public PdfFormField getTextField(PdfWriter writer, Rectangle rect, String name) throws IOException, DocumentException {
        TextField tf = new TextField(writer, rect, name);
        tf.setFontSize(16);
        tf.setOptions(TextField.MULTILINE);
        return tf.getTextField();
    }
}
/*
 * Example written by Bruno Lowagie.
 */
package sandbox.acroforms;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class ReadOnlyField3 {
 
    public static final String DEST = "results/acroforms/multilineField3.pdf";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ReadOnlyField3().createPdf(DEST);
    }
 
    public void createPdf(String filename) throws DocumentException, IOException {
        PdfReader reader = new PdfReader(createForm());
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
        AcroFields form = stamper.getAcroFields();
        form.setGenerateAppearances(false);
        form.setFieldProperty("text1", "setfflags", TextField.READ_ONLY, null);
        form.setField("text1", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
        form.setFieldProperty("text2", "setfflags", TextField.READ_ONLY, null);
        form.setField("text2", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
        form.setFieldProperty("text3", "setfflags", TextField.READ_ONLY, null);
        form.setField("text3", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
        form.setFieldProperty("text4", "setfflags", TextField.READ_ONLY, null);
        form.setField("text4", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
        stamper.close();
    }
 
    public byte[] createForm() throws DocumentException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.open();
        Rectangle rect = new Rectangle(36, 770, 144, 806);
        writer.addAnnotation(getTextField(writer, rect, "text1"));
        rect = new Rectangle(148, 770, 256, 806);
        writer.addAnnotation(getTextField(writer, rect, "text2"));
        rect = new Rectangle(36, 724, 144, 760);
        writer.addAnnotation(getTextField(writer, rect, "text3"));
        rect = new Rectangle(148, 727, 256, 760);
        writer.addAnnotation(getTextField(writer, rect, "text4"));
        document.close();
        return baos.toByteArray();
    }
 
    public PdfFormField getTextField(PdfWriter writer, Rectangle rect, String name) throws IOException, DocumentException {
        TextField tf = new TextField(writer, rect, name);
        tf.setFontSize(16);
        tf.setOptions(TextField.MULTILINE);
        return tf.getTextField();
    }
}
Resources: 
File nameRaw URLUpdated
multiline.pdfmultiline.pdf2015-11-20 7:27 am