为什么创建PDF文档时不能应用我的字体? [英] Why is my font not applied when I create a PDF document?

查看:219
本文介绍了为什么创建PDF文档时不能应用我的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用iText生成PDF文档。当我试图包含议程表格光字体,iText忽略我的字体选择。
我确认字体没有使用Adobe Reader的 File> Properties> Fonts 设置。
PDF显示使用了Helvetica,但我没有选择Helvetica。
颜色是可见的,但不是字体。



我的代码如下所示:

  public static final Font FONT_HEADER = FontFactory.getFont(AGENDA_TABULAR_LIGHT,18,Font.NORMAL,TITLE_COLOR); 

我甚至尝试过一个示例程序。

  // step 1 
Document document = new Document();
//第2步
PdfWriter.getInstance(document,new FileOutputStream(filename));
//第3步
document.open();
//第四步:
Font font = FontFactory.getFont(Agenda Tabular Light);

System.out.println(font.toString());

document.add(new Phrase(Agenda Tableular Light J j,font));
Font fontbold = FontFactory.getFont(Times-Roman,12,Font.BOLD);
document.add(new Phrase(Times-Roman,Bold,fontbold));
document.add(Chunk.NEWLINE);

document.close();

它显示为Times-Roman,并为议程使用不同的字体。
但是,无论何时我看到Adobe Reader属性的字体选项卡,它都会显示Helvetica。

解决方案

,这通常是行不通的:

  Font font = FontFactory.getFont(garamond bold); 

默认情况下,只有标准Type 1 c $ c> FontFactory 和garamond bold不是这些字体之一,因此将使用Helvetica(Helvetica是默认字体在iText中)。



您可以通过注册来教 FontFactory 在哪里查找其他字体。您可以尝试:

 FontFactory.registerDirectories(); 

但是这是一个非常昂贵的操作,因为您要求iText在不同的操作系统中搜索不同的字体文件字体目录(例如 C:/ Windows / Fonts )。这可能需要几秒钟,你会得到比你需要更多的字体(也许你需要的字体将不会被注册)。



最好的方法是注册你需要的字体,如下所示:

  FontFactory.register(c:/ windows / fonts / garabd。 ttf,garamond bold); 

我们告诉iText在哪里找到ttf文件(c:/ windows /fonts/garabd.ttf),我们为该字体定义一个别名(garamond bold)。现在字体名已经注册了,我们可以开始使用它:

  Font myBoldFont = FontFactory.getFont(garamond bold) ; 


I am generating PDF documents with iText. When I try to include the "Agenda Tabular Light" font, iText ignores my choice of font. I confirmed that the font isn't set using Adobe Reader's File > Properties > Fonts. The PDF shows that Helvetica was used, but I didn't choose Helvetica. The colors are visible, but not the font.

My code looks like this:

public static final Font FONT_HEADER = FontFactory.getFont(AGENDA_TABULAR_LIGHT, 18, Font.NORMAL, TITLE_COLOR);

I even I tried a sample program.

// step 1
    Document document = new Document();
    // step 2
    PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4:
    Font font = FontFactory.getFont("Agenda Tabular Light");

    System.out.println(font.toString());

    document.add(new Phrase("Agenda Tabular Light J j", font));
    Font fontbold = FontFactory.getFont("Times-Roman", 12, Font.BOLD);
    document.add(new Phrase("Times-Roman, Bold", fontbold));
    document.add(Chunk.NEWLINE);

    document.close();

It displays like Times-Roman and uses a different font for agenda. But whenever I see fonts tab on properties of Adobe Reader, it displays Helvetica.

解决方案

As explained in the comments, this usually doesn't work:

Font font = FontFactory.getFont("garamond bold");

By default only the standard Type 1 fonts are known to the FontFactory and "garamond bold" isn't one of those fonts, hence Helvetica will be used instead (Helvetica is the default font in iText).

You can "teach" the FontFactory where to find other fonts by registering them.

You could try:

FontFactory.registerDirectories();

But that is a very expensive operation as you ask iText to search your operating system for font files in different font directories (e.g. C:/Windows/Fonts). This can take a few seconds and you'll end up with much more fonts than you need (and maybe the font you need won't be registered anyway).

The best way is to register the fonts you need like this:

FontFactory.register("c:/windows/fonts/garabd.ttf", "garamond bold");

We tell iText where to find the ttf file ("c:/windows/fonts/garabd.ttf") and we define an alias for that font ("garamond bold"). Now that the font name is registerd, we can start using it:

Font myBoldFont = FontFactory.getFont("garamond bold");

这篇关于为什么创建PDF文档时不能应用我的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆