iText Java: Not adding phonogram symbol

In our project we need to print the phonogram symbol (℗) on our PDFs. After finding out we have to use Arial for this (or any other font, but arial will do) we added that font to out project. When trying to print the pdf, however, the phonogram symbol is not printed.

We use iText 5.5.0 and add the arial.ttf with the IDENTITY_H encoding. If I understood correctly, this should enable all characters in the font. Can anyone point out what is going wrong? Might there be a problem with our font file or is iText bugging us?

This code creates a pdf with the letter f and the phonogram character. In this testclass, the phonogram character is also not printed.

public class TestPdf {

    public static void main(String[] args) throws FileNotFoundException, DocumentException {
        // Step 1: create document
        Document document = new Document();
        // Step 2: get instance of PdfWriter
        PdfWriter.getInstance(document, new FileOutputStream("myfile.pdf"));
        // Step 3: open document
        document.open();
        // Step 4: prepare paragraph
        Font arial12 = ArialFont.ofSize(12);
        Paragraph paragraph = new Paragraph("The letter f: u0066 nThe phonogram symbol: u2117");
        paragraph.setFont(arial12);
        // Step 5: add paragraph
        document.add(paragraph);
        // Step 6: close document
        document.close();
    }

    public static class ArialFont {

        protected static BaseFont arialFont;

        public static Font ofSize(int size) {
            return new Font(getBaseFont(), size);
        }

        public static BaseFont getBaseFont() {
            try {
                if (arialFont == null) {
                    arialFont = BaseFont.createFont("/fonts/ARIAL.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                }
                return arialFont;
            } catch (DocumentException e) {
                throw new IllegalStateException(e);
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }
    }
}

Try doing this:

Font arial12 = ArialFont.ofSize(12);
Paragraph paragraph = new Paragraph("The letter f: u0066 nThe phonogram symbol: u2117", arial12);

Why? to be honest I have no idea. It seems to be a bug with setting the font after setting the text of the paragraph. I ran your code and saw that your pdf wasnt getting the Arial font embedded.

For debugging PDF files I use a set of command line tools called XPdf found here:

http://www.foolabs.com/xpdf/

You can use the pdffonts <filename> call to check the embedded or referenced fonts in pdf file.

链接地址: http://www.djcxy.com/p/65456.html

上一篇: iText输出缺少Unicode字符

下一篇: iText Java:不添加音讯符号