1

Below is the code to write PDF using Java.

Code

public class PDFTest {    

    public static void main(String args[]) {
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);       

        try {
            File file = new File("C://test//itext-test.pdf");
            FileOutputStream fileout = new FileOutputStream(file);            
            PdfWriter.getInstance(document, fileout);
            document.addAuthor("Me");
            document.addTitle("My iText Test");
            document.open();
            Chunk chunk = new Chunk("iText Test");
            Paragraph paragraph = new Paragraph();
            String test = "și";
            String test1 = "şi";
            if (test.equalsIgnoreCase(test1)) {
               // System.out.println("equal ignore case true");
                paragraph.add(test + " New Font equal with Old Font");
            } else {
              //  System.out.println("equal ignore case X true");
                paragraph.add(test1 + " New Font Not equal with Old Font");
            }
            paragraph.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph);          
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
}

When I test with Romanian language, I found that "ș" is missing in created PDF. The Document appears like below: Error

Any advice or references links regarding this issue is highly appreciated.

**EDITED**
I've use unicode example like below and the output is still same. "ș" is still missing.

Code

static String RESULT = "C://test/itext-unicode4.pdf";
    static String FONT = "C://Users//PenangIT//Desktop//Arial Unicode.ttf";
    public static void main(String args[])
    {
        try
        {
            Document doc = new Document();
            PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
            doc.open();
            BaseFont bf;
            bf = BaseFont.createFont(FONT,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
            doc.add(new Paragraph("Font : "+bf.getPostscriptFontName()+" with encoding: "+bf.getEncoding()));
            doc.add(new Paragraph(" TESTING "));
            doc.add(new Paragraph(" TESTING 1 și "));
            doc.add(new Paragraph(" TESTING 2 şi "));
            doc.add(Chunk.NEWLINE);
            doc.close();

        }
        catch(Exception ex)
        {            
        }

The Output looks like this
enter image description here
It same for encode as well. The "ș" is still missing.

chinna_82
  • 6,353
  • 17
  • 79
  • 134
  • What do you want . I am not getting. – Rahul Kulhari Sep 05 '13 at 04:28
  • @RahulKulhari some font missing when writing file in PDF. The PDF only got **`i`** instead **`și`**. – chinna_82 Sep 05 '13 at 04:30
  • You are not selecting any specific font. Thus, a default font is used which surely is one of the standard 14 fonts each PDF viewer must support without further ado. Unfortunately, they only need to support it for a limited character set, and most likely your special character is not in that character set. So you should go ahead and explicitly use a font which does support your character and embed it in the PDF. – mkl Sep 05 '13 at 05:11

1 Answers1

2

Please take a look at this PDF: encoding_example.pdf (*)

It contains all kinds of characters that aren't present in the default font Helvetica (which is the default font you're using as you're not defining any other font).

In the EncodingExample source, we use arialbd.ttf with a specific encoding, resulting in the use of a simple font in the PDF. In the UnicodeExample source, we use IDENTITY_H as encoding, resulting in the use of a composite font in the PDF.

I've adapted your code, because I see that you didn't understand my answer:

BaseFont bf = BaseFont.createFont(FONT,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
doc.add(new Paragraph(" TESTING 1 și ", new Font(bf, 12)));
doc.add(new Paragraph(" TESTING 2 \u015Fi ", new Font(bf, 12)));

Do you see the difference? In your code, you create bf, but you aren't using that object anywhere.

(* )Note: pdf.js can't interpret some glyphs because pdf.js doesn't support simple fonts with a special encoding; these glypgh show up correctly in Adobe Reader and Chrome PDF viewer. If you want to be safe, use composite fonts, because pdf.js can render those glyphs correctly: unicode_example.pdf

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I've tested both way and the output is still same. The **`"ș"`** is still missing. I've edited my question with your example. – chinna_82 Sep 05 '13 at 09:47
  • Are you sure the glyph is available in arial? Surely you didn't put "ş" in your code, you replaced it with "\u015F" didn't you? If using arial didn't work, did you try with arialuni.ttf? **UPDATE:** I saw your code. I see that you're still using Helvetica instead of Arial! – Bruno Lowagie Sep 05 '13 at 15:48