PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
J2SE J2EE Windows Linux Mac (OS X)

How to add Multi-Line Text to Fields in a PDF Forms Document

Should I use newline or carriage return in my string values?
By Santhanam L.

On a PDF forms document displayed on a viewer application, we can enter text in more than one line or in multiple paragraph by pressing the Enter key at the end of each line or paragraph.

As programmers, we need to specify the character equivalent of pressing the Enter key in Java code while specifing values for text form fields.

Java uses "\n" and "\r" to signify newline and carriage return characters. The interpretation of these characters varies across platforms. What about PDF Forms?

With a text form field, a "\r" (carriage return) character can be used to separate two lines. (Two consecutive carriage returns can be used to separate two lines with an empty line.)

PDFOne Java also allows the use of "\n" (newline) character but it will be replaced with a "\r" character.

Apprehending that some programmers (particularly those with a "C/C++" background) might use both end-of-line characters to indicate a single line break, PDFOne Java will first replace all instances of "\r\n" and "\n\r" with a single "\r" and then attempt to replace any standalone instances of "\n" with "\r".

Please remember this order of replacement when you specify multi-line strings to text fields. Why is this important?

It is important because "\n\r" or "\r\n" means only one new line while "\r\r" or "\n\n" means two new lines.

A sample class displayed below illustrates several scenarios for end-of-line characters in string values specified for text fields. (To ensure the multiple lines are displayed on viewer applications, PdfFormTextField.setMultiline(boolean multiline) has been set to true.)

import java.awt.Color;
import java.io.File;
import java.io.IOException;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfFormField;
import com.gnostice.pdfone.PdfFormTextField;
import com.gnostice.pdfone.PdfPage;
import com.gnostice.pdfone.PdfRect;
import com.gnostice.pdfone.PdfWriter;
import com.gnostice.pdfone.encodings.PdfEncodings;
import com.gnostice.pdfone.fonts.PdfFont;

public class June2008
    static {
    public static void main(String[] args) 
      throws IOException, PdfException

        PdfDocument doc1 = new PdfDocument();
        PdfFont fontHelvetica = 
        // Create a PDF page
        PdfPage page = new PdfPage();
        // Create several text form fields with
        // different values 
        PdfFormTextField tf1 = 
            new PdfFormTextField(
                         new PdfRect(100, 100, 150, 50),
            "45, Sankey Road," +
            "Lower Palace Orchards," +
            "Bangalore, India.");
        PdfFormTextField tf2 = (PdfFormTextField) tf1.clone();
        tf2.setRect(new PdfRect(100, 200, 150, 50));
            "45, Sankey Road,\r" +
            "Lower Palace Orchards,\r" +
            "Bangalore, India.");
        PdfFormTextField tf3 = (PdfFormTextField) tf1.clone();
        tf3.setRect(new PdfRect(100, 300, 150, 50));
            "45, Sankey Road,\n" +
            "Lower Palace Orchards,\n" +
            "Bangalore, India.");
        PdfFormTextField tf4 = (PdfFormTextField) tf1.clone();
        tf4.setRect(new PdfRect(100, 400, 150, 50));
            "45, Sankey Road,\n\r" +
            "Lower Palace Orchards,\r\n" +
            "Bangalore, India.");
        PdfFormTextField tf5 = (PdfFormTextField) tf1.clone();
        tf5.setRect(new PdfRect(100, 500, 150, 50));
            "45, Sankey Road,\r\r" +
            "Lower Palace Orchards,\r\r" +
            "Bangalore, India.");
        PdfFormTextField tf6 = (PdfFormTextField) tf1.clone();
        tf6.setRect(new PdfRect(100, 600, 150, 50));
            "45, Sankey Road,\n\n" +
            "Lower Palace Orchards,\n\n" +
            "Bangalore, India.");
        // Write form field values near the 
        // the form fields
        page.writeText("45, Sankey Road," +    
                       "Lower Palace Orchards," +
                       "Bangalore, India.",
                       100, 85);
        page.writeText("45, Sankey Road,\\r" +    
                       "Lower Palace Orchards,\\r" +
                       "Bangalore, India.",
                       100, 185);
        page.writeText("45, Sankey Road,\\n" +    
                       "Lower Palace Orchards,\\n" +
                       "Bangalore, India.",
                       100, 285);
        page.writeText("45, Sankey Road,\\n\\r" +    
                       "Lower Palace Orchards,\\r\\n" +
                       "Bangalore, India.",
                       100, 385);
        page.writeText("45, Sankey Road,\\r\\r" +    
                       "Lower Palace Orchards,\\r\\r" +
                       "Bangalore, India.",
                       100, 485);
        page.writeText("45, Sankey Road,\\n\\n" +    
                       "Lower Palace Orchards,\\n\\n" +
                       "Bangalore, India.",
                       100, 585);
        // Add the form fields to the page

The output PDF document looks like this.

Note how "\r", "\n", "\r\n", and "\n\r" all behave like a single "\r". And, see how "\r\r" and "\n\n" in the last two fields has the same effect.


Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.


A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.