Dilip Hajare
Dilip Hajare

Reputation: 11

PdfBox write hindi characters in pdf file

I tried many things to write hindi characters using Apache PdfBox but seems its existing issue in the library.

I tried many font files available, Can someone really help me out in this.

I tried following :

    PDDocument doc = new PDDocument();    
    PDPage page = new PDPage();
    doc.addPage(page);
    PDFont font = PDTrueTypeFont.loadTTF( doc, new FileInputStream(new File("D:\\Data\\fonts\\dn.ttf")));   
    font.setFontEncoding(new WinAnsiEncoding());   
    PDPageContentStream content = new PDPageContentStream( doc, page, true, false );   
    content.setFont(font, 10);   
    content.beginText();

    content.moveTextPositionByAmount( 200, 100 );   
    content.drawString( "हिंदी" ); // Writing word "Hindi" in hindi language.  
    content.endText();   
    content.close();
    doc.save( new FileOutputStream(new File("D:\\testOutput1.pdf")));   
    doc.close();

Upvotes: 0

Views: 1192

Answers (2)

Pioneer
Pioneer

Reputation: 1518

It's working for me in PDFBox. The trick here is to use non-unicode string instead of unicode string.

Use Kruti Dev Font given in below link. Then convert your unicode string to non-unicode string. And finally use that converted string in your code.

That means replace this like

content.drawString( "हिंदी" ); // Writing word "Hindi" in hindi language.

With this line

content.drawString( "fganh" ); // Writing word "Hindi" in hindi language.

Convert Unicode (Mangal) To Kruti Dev Font

Upvotes: 1

Dilip Hajare
Dilip Hajare

Reputation: 11

I think this cannot be done using PdfBox as there are lot of issues with it. I tried many fonts and the encoding types of PdfBox but failed to write in Hindi.

At the end I tried it in Node Js express pdfmaker() which converts Html to PDF, However I had issues on my Linux server and I installed appropriate ttf font and it worked !

Upvotes: 1

Related Questions