Skip to main content
Skip table of contents

pdfCalligraph: Support wrapping at word boundaries for languages which don't use spaces for word separation

Introduction

Some languages don't use spaces to separate words and instead use spaces to only separate sentences. Languages which do this include Thai and Khmer, but we will use Thai for the purpose of this example.

iText has developed an advanced mechanism that allows for wrapping at word boundaries for languages which don't use spaces for word separation.

We are thrilled to announce full support for this mechanism within the iText 7 Core library as of version 7.1.12 and version 2.0.7 of the pdfCalligraph add-on.

Code example

Here is an example demonstrating this new word wrapping behavior:

JAVA

JAVA
package WordWrapExample;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

import java.io.FileNotFoundException;
import java.io.IOException;

public class WordWrappingExample {
    String thaiText="???????????????????????????????????????????????????????????????????????????????????????? " +
            "????????????????????????????????????? ????? ?????????????????????????????????? ??????????????????????????????? " +
            "????????????????????????????????????????????????????????????????? ??????????????????????????????????????????????? ????????????????????????????????? ????????????????????";
    /**
     * Some languages don't use spaces to separate words and instead use spaces to only separate sentences.
     * An Example of one of these languages is Thai.
     * Itext has developed an advanced mechanism that allows for
     * wrapping at word boundaries for languages which don't use spaces for word separation
     * */
    public  void thaiWrappingExample(String outPutFilePath, String fontPath){

        try {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outPutFilePath));
            Document doc = new Document(pdfDoc);
            //add thaiText to a paragraph
            Paragraph paragraph = new Paragraph(this.thaiText);
            //set width of paragraph (optional)
            paragraph.setWidth(50);
            //set background color of paragraph (optional)
            paragraph.setBackgroundColor(ColorConstants.LIGHT_GRAY);
            //add paragraph to document and createFont with specified path to Thai font add Pdf Encodings
            doc.add(paragraph.setFont(PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H)));
            doc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

C#

C#
using iText.IO.Font;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ConsoleApp1
{
    class WordWrapping

    {
        String thaiText = "???????????????????????????????????????????????????????????????????????????????????????? " +
        "????????????????????????????????????? ????? ?????????????????????????????????? ??????????????????????????????? " +
        "????????????????????????????????????????????????????????????????? ??????????????????????????????????????????????? ????????????????????????????????? ????????????????????";
        public void thaiWrappingExample(String outPutFilePath, String fontPath)
        {

            try
            {
                PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outPutFilePath));
                Document doc = new Document(pdfDoc);
                //add thaiText to a paragraph
                Paragraph paragraph = new Paragraph(this.thaiText);
                //set with of paragraph (optional)
                paragraph.SetWidth(50);
                //set background color of paragraph (optional)
                paragraph.SetBackgroundColor(ColorConstants.LIGHT_GRAY);
                //add paragraph to document and createFont with specified path to Thai font add Pdf Encodings
                doc.Add(paragraph.SetFont(PdfFontFactory.CreateFont(fontPath, PdfEncodings.IDENTITY_H)));
                doc.Close();
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.StackTrace);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }
    }
}

Below you can see an example of the output PDF that iText generates using the code above:




JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.