Search code examples
javaitext7

Is iText7 possible to display fonts with IVS(Ideographic Variation Sequence)?


I am using the iText7(v7.1.1) to create PDF-file.
Environment: java version "1.7.0_45".

For IVS(Ideographic Variation Sequence) see below http://blogs.adobe.com/CCJKType/files/2017/09/iuc32-lunde-s5t3.pdf

Sample-Code see below

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.io.font.PdfEncodings;
import java.io.File;

public class SimpleTableIVS {
    public static final String DEST = "SimpleTableIVS.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        new SimpleTableIVS().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        // UTF-8 encoding table and Unicode characters
        // http://www.utf8-chartable.com/unicode-utf8-table.pl?start=131072&unicodeinhtml=hex&htmlent=1
        // http://www.utf8-chartable.com/unicode-utf8-table.pl?start=33792&number=1024
        byte[] bUtfA = {(byte)0xd8, (byte)0x40, (byte)0xdc, (byte)0x0b}; // U+2000B [IVS:2000B_E0103]
        byte[] bUtfB = {(byte)0x84, (byte)0x5b};                         // U+845B, [IVS: 845B_E0103]

        // After Add "Ideographic Variation Selector"
        byte[] bUtfC = {(byte)0xdb, (byte)0x40, (byte)0xdd, (byte)0x01}; // U+E0101
        byte[] bUtfD = {(byte)0xdb, (byte)0x40, (byte)0xdd, (byte)0x02}; // U+E0102

        //PdfFont font = PdfFontFactory.createFont("C:/Windows/Fonts/msmincho.ttc,0", PdfEncodings.IDENTITY_H);
        //PdfFont font = PdfFontFactory.createFont("C:/Windows/Fonts/meiryo.ttc,0", PdfEncodings.IDENTITY_H);
        PdfFont font = PdfFontFactory.createFont("C:/Program Files/ipamjm/ipamjm.ttf", PdfEncodings.IDENTITY_H);

        String strUtfA = new String(bUtfA, "UTF-16");
        String strUtfB = new String(bUtfB, "UTF-16");
        String strUtfC = strUtfA + (new String(bUtfC, "UTF-16"));
        String strUtfD = strUtfB + (new String(bUtfD, "UTF-16"));

        Table table = new Table(4);
        table.addCell(new Paragraph("\u200d" + strUtfA).setFont(font).setFontSize(12)); 
        table.addCell(new Paragraph("\u200d" + strUtfB).setFont(font).setFontSize(12)); 
        table.addCell(new Paragraph("\u200d" + strUtfC).setFont(font).setFontSize(12)); 
        table.addCell(new Paragraph("\u200d" + strUtfD).setFont(font).setFontSize(12)); 
        doc.add(table);
        doc.close();
    }
}

Solution

  • Unfortunately, Ideographic Variation Sequences are not currently supported in iText. Supporting it is feasible, but not very easy, and thus is not the highest priority at the moment.

    An internal development ticket has been created for that and it would be great to implement this feature in the next versions.