使用itext java在pdf中写出心脏符号

时间:2016-12-02 11:54:22

标签: java pdf itext

尝试通过java代码在pdf中编写心脏符号。 这是我对pdf的输入:❤️❤️❤️

但是生成的pdf是空的,没有写任何东西。 使用itext写入pdf。 使用的字体是tradegothic_lt_boldcondtwenty.ttf

OutputStream file = new FileOutputStream(fileName);
        Document document = new Document(PageSize.A6);
        PdfWriter writer = PdfWriter.getInstance(document, file);
        document.open();

        PdfLayer nested = new PdfLayer("Layer 1", writer);
        PdfContentByte cb = writer.getDirectContent();
        cb.beginLayer(nested);
        ColumnText ct = new ColumnText(cb);

        Font font = getFont();
        Phrase para1 = new Phrase("❤️❤️❤️",font);

        ct.setSimpleColumn(para1,38,0,260,138,15, Element.ALIGN_LEFT);
        ct.go();

        cb.endLayer();

        document.close();
        file.close();

private Font getFont() { 

    final String methodName = "generatePDF";
    LOGGER.entering(CLASSNAME, methodName);

    Font font = null;
    try {

            String filename = tradegothic_lt_boldcondtwenty.ttf;
            FontFactory.register(filename, filename);
            font = FontFactory.getFont(filename, BaseFont.CP1252, BaseFont.EMBEDDED,11.8f);

    } catch(Exception exception) {
        LOGGER.logp(Level.SEVERE, CLASSNAME, methodName, "Exception Occurred while fetching the Trade Gothic font." + exception);
        font = FontFactory.getFont(FontFactory.HELVETICA_BOLD,11.8f);
    }
    return font;
}

短语para1的心脏正确。但是无法在pdf中看到

0 个答案:

没有答案