You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by "Taiarol, Felipe" <fe...@sap.com> on 2015/08/19 14:23:14 UTC

How to Print UTF-8 Characters

Hi,

I am trying to create PDFs in languages such as Chinese and Russian but the characters of those languages are not being printed correctly.

This code outputs the attached PDF.

I read it has something to do with PDF not supporting UTF-8, I tried to use a font which had those characters but it didn’t work.


String text = "ру́сский язы́к, ру́сский язы́к, ру́сский язы́к";

PDDocument document = new PDDocument();

PDPage page = new PDPage();

document.addPage(page);

PDFont font = PDType1Font.HELVETICA_BOLD;

PDPageContentStream contentStream = new PDPageContentStream( document, page);

contentStream.beginText();

contentStream.setFont(font, 12);

contentStream.moveTextPositionByAmount(100, 700);

contentStream.drawString(text);

contentStream.endText();

contentStream.close();

document.save("Hello World.pdf");

document.close();

Does PDFBox supports printing this type of characters ? How can this be done ?


Thanks in advance!


Felipe

Re: How to Print UTF-8 Characters

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,

> Am 19.08.2015 um 14:23 schrieb Taiarol, Felipe <fe...@sap.com>:
> 
> Hi,
> 
> I am trying to create PDFs in languages such as Chinese and Russian but the characters of those languages are not being printed correctly.
> 
> This code outputs the attached PDF. 
> 
> I read it has something to do with PDF not supporting UTF-8, I tried to use a font which had those characters but it didn’t work.


You have to use PDFBox 2.0.0 if you're not already doing so. In addition you need to use a font which has these characters and load it

PDFont font = PDType0Font.load(doc, new File(path to you ttf file));

Please take a look at the HelloWorldTTF example in the examples project.

BR
Maruan

> 
> String text = "ру́сский язы́к, ру́сский язы́к, ру́сский язы́к";
> 
> PDDocument document = new PDDocument();
> PDPage page = new PDPage();
> document.addPage(page);
> 
> PDFont font = PDType1Font.HELVETICA_BOLD;
> PDPageContentStream contentStream = new PDPageContentStream( document, page);
> contentStream.beginText();
> contentStream.setFont(font, 12);
> contentStream.moveTextPositionByAmount(100, 700);
> contentStream.drawString(text);
> contentStream.endText();
> contentStream.close();
> 
> document.save("Hello World.pdf");
> document.close();
> Does PDFBox supports printing this type of characters ? How can this be done ?
> 
> Thanks in advance!
> 
> Felipe
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org