Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5985275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:30:24+00:00 2026-05-22T22:30:24+00:00

I’m using PDFBox and iText to create a simple (just paragraphs) pdf document from

  • 0

I’m using PDFBox and iText to create a simple (just paragraphs) pdf document from various languages. Something like :

pdfBox:

private static void createPdfBoxDocument(File from, File to) {
    PDDocument document = null;
    try {
        document = new TextToPDF().createPDFFromText(new FileReader(from));
        document.save(new FileOutputStream(to));
    } finally {
        if (document != null)
            document.close();
    }
}

private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    PDType1Font font = PDType1Font.TIMES_ROMAN;
    contentStream.setFont(font, 12);
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(100, 400);
    contentStream.drawString("š");
    contentStream.endText();
    contentStream.close();
    document.save("test.pdf");
    document.close();
}

itext:

private static Font blackFont = new Font(Font.FontFamily.COURIER, 12, Font.NORMAL, BaseColor.BLACK);

private static void createITextDocument(File from, File to) {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(to));
    document.open();
    addContent(document, getParagraphs(from));
    document.close();
}

private static void addContent(Document document, List<String> paragraphs) { 

    for (int i = 0; i < paragraphs.size(); i++) {
        document.add(new Paragraph(paragraphs.get(i), blackFont));
    }
}

The input files are encoded in UTF-8 and some languages of Unicode char set, like Russian alphabet etc., are not rendered properly in pdf. The Fonts in both libraries don’t support Unicode charset I suppose and I can’t find any documentation on how to add and use third party fonts. Could please anybody help me out with an example ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:30:25+00:00Added an answer on May 22, 2026 at 10:30 pm

    If you are using iText, it has quite good support.

    In iText in Action (chapter 2.2.2) you can read more.

    You have to download some unicode Fonts like arialuni.ttf and do it like this :

        public static File fontFile = new File("fonts/arialuni.ttf");
    
        public static void createITextDocument(File from, File to) throws DocumentException, IOException {
    
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(to));
            document.open();
            writer.getAcroForm().setNeedAppearances(true);
            BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    
            FontSelector fs = new FontSelector();
            fs.addFont(new Font(unicode));
    
            addContent(document, getParagraphs(from), fs);
            document.close();
        }
    
        private static void addContent(Document document, List<String> paragraphs, FontSelector fs) throws DocumentException { 
    
            for (int i = 0; i < paragraphs.size(); i++) {
                Phrase phrase = fs.process(paragraphs.get(i));
                document.add(new Paragraph(phrase));
            }
        }
    

    arialuni.ttf fonts work for me, so far I checked it support for

    BG, ES, CS, DA, DE, ET, EL, EN, FR, IT, LV, LT, HU, MT, NL, PL, PT, RO, SK, SL, FI, SV
    

    and only PDF in Romanian language wasn’t created properly…

    With PDFBox it’s almost the same:

    private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage();
        document.addPage(page);
        PDPageContentStream contentStream = new PDPageContentStream(document, page);
    
        PDFont font = PDTrueTypeFont.loadTTF(document, "fonts/arialuni.ttf");
        contentStream.setFont(font, 12);
        contentStream.beginText();
        contentStream.moveTextPositionByAmount(100, 400);
        contentStream.drawString("š");
        contentStream.endText();
        contentStream.close();
        document.save("test.pdf");
        document.close();
    }
    

    However as Gagravarr says, it doesn’t work because of this issue PDFBOX-903 . Even with 1.6.0-SNAPSHOT version. Maybe trunk will work. I suggest you to use iText. It works there perfectly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.