Pergunta

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.

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top