PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
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 {
        PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8",
                "9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
    }    
    public static void main(String[] args) 
      throws IOException, PdfException
    {

        PdfDocument doc1 = new PdfDocument();
        PdfFont fontHelvetica = 
            PdfFont.create("Helvetica", 
                           10, 
                           PdfEncodings.CP1252);
        
        // 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),
                         "tf_name", 
                         PdfFormField.FLAG_REQUIRED, 
                         Color.BLACK, 
                         Color.YELLOW);
        tf1.setBorderColor(Color.BLACK);
        tf1.setFont(fontHelvetica);
        tf1.setMultiline(true);
        tf1.setValue(
            "45, Sankey Road," +
            "Lower Palace Orchards," +
            "Bangalore, India.");
        
        PdfFormTextField tf2 = (PdfFormTextField) tf1.clone();
        tf2.setRect(new PdfRect(100, 200, 150, 50));
        tf2.setName("tf_name2");
        tf2.setValue(
            "45, Sankey Road,\r" +
            "Lower Palace Orchards,\r" +
            "Bangalore, India.");
        
        PdfFormTextField tf3 = (PdfFormTextField) tf1.clone();
        tf3.setRect(new PdfRect(100, 300, 150, 50));
        tf3.setName("tf_name3");
        tf3.setValue(
            "45, Sankey Road,\n" +
            "Lower Palace Orchards,\n" +
            "Bangalore, India.");
        
        PdfFormTextField tf4 = (PdfFormTextField) tf1.clone();
        tf4.setRect(new PdfRect(100, 400, 150, 50));
        tf4.setName("tf_name4");
        tf4.setValue(
            "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));
        tf5.setName("tf_name5");
        tf5.setValue(
            "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));
        tf6.setName("tf_name6");
        tf6.setValue(
            "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
        page.addFormField(tf1);
        page.addFormField(tf2);
        page.addFormField(tf3);
        page.addFormField(tf4);
        page.addFormField(tf5); 
        page.addFormField(tf6);
            
        doc1.add(page);
        doc1.setOpenAfterSave(true);
        doc1.save("PdfFormTextField_example.pdf");
        doc1.close();      
    }
}

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.

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

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

PDFOne .NET

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
StarDocs

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.