0

I am not able to print Chinese Japanese characters in pdf using itext. I am using xmlWorker to convert HTML containing Chinese, Japanese and Korean characters in a single string.But I am getting blank spaces in the pdf though I registered the fonts with xmlFontProvider.

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class ParseHtml11 {
    public static final String DEST = "cyrillic.pdf";
    public static final String HTML = "<html><head>    <meta http-equiv='content-type' content='application/xhtml+xml; charset=UTF-8'/></head><body><h4 style='font-family: Arial Unicode MS, FreeSans; font-size:16px; font-weight: normal; ' >Здраво Kristijan!</h4><p>This is chinese language test 测试 </p></body></html>";



    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);

        new ParseHtml11().createPdf(DEST);
    }

    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontImp.register("/Users/user/Downloads/MyFonts/FreeSans.ttf");
        fontImp.register("/Users/user/Downloads/MyFonts/THSarabunNew.ttf");
        fontImp.register("/Users/user/Downloads/MyFonts/Kh-Content.ttf");
        fontImp.register("/Users/user/Downloads/MyFonts/THSarabunNew Italic.ttf");
        fontImp.register("/Users/user/Downloads/MyFonts/THSarabunNew BoldItalic.ttf");
        fontImp.register("/Users/user/Downloads/MyFonts/THSarabunNew Bold.ttf");
        fontImp.register("/Users/user/Downloads/MyFonts/arial.ttf");
        fontImp.register("/Users/user/Downloads/MyFonts/FreeSans.ttf");
        fontImp.register( "/Users/user/Downloads/MyFonts/NotoSansCJKsc-Regular.otf");
        fontImp.register( "/Users/user/Downloads/MyFonts/arialuni.ttf");
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new ByteArrayInputStream((HTML.toString().getBytes("UTF-8"))), null, Charset.forName("UTF-8"), fontImp);
        // step 5
        document.close();
    }
}
José Castro
  • 661
  • 6
  • 14
  • Registering fonts through a `FontProvider` is only one aspect. The other aspect is: how are the fonts defined in the HTML? You don't show that in your question. See [How to convert Arabic HTML to PDF?](http://developers.itextpdf.com/question/how-convert-arabic-html-pdf) [How to set RTL direction for Hebrew when converting HTML to PDF?](http://developers.itextpdf.com/question/how-set-rtl-direction-hebrew-when-converting-html-pdf) or see [this Asian example](http://developers.itextpdf.com/question/how-export-vietnamese-text-pdf-using-itext). – Bruno Lowagie Aug 02 '16 at 08:52
  • I found a duplicate of this question, but I noticed that all links in that answer were broken. I've fixed these links. There are different strategies that can be used, so it would help to look at the other Asian examples available on [this page](http://developers.itextpdf.com/examples/xml-worker/xml-worker-examples). – Bruno Lowagie Aug 02 '16 at 08:59
  • @BrunoLowagie : Can you please tell me what'Wrong in http://stackoverflow.com/questions/38718216/print-all-the-languages-in-a-single-pdf-using-itext – Krishna Mohan Mathakala Aug 02 '16 at 10:48

0 Answers0