简体   繁体   English

如何使用iText7将字体嵌入pdf / a

[英]How to embed font into pdf/a using iText7

I'm trying to see how to embed fonts into my pdf/a. 我正在尝试查看如何将字体嵌入到pdf / a中。
I found a lot of answer but using iTextSharp. 我使用iTextSharp找到了很多答案。
In my cas I use iText7 and all I tried gave me the error: 在我的cas中,我使用iText7,而我尝试的所有操作都给了我错误:
"All the fonts must be embedded..." “所有字体都必须嵌入...”

I have a ttf file for my font but I didn't find a way to embed it into my pdf to use it... 我的字体有一个ttf文件,但没有找到将其嵌入到pdf中使用的方法...

Could someone help me? 有人可以帮我吗?
Thanks in advance 提前致谢

kor6k kor6k

As documented in the tutorial and as indicated by the error you mention ("All the fonts must be embedded"), you need to embed the fonts. 教程中记录的那样,并且如您所提到的错误所指示(“必须嵌入所有字体”),您需要嵌入字体。

You are probably not defining a font, in which case the standard Type 1 font Helvetica will be used. 您可能没有定义字体,在这种情况下,将使用标准的Type 1字体Helvetica。 These standard Type 1 fonts are never embedded, hence you need to pick another font. 这些标准的Type 1字体永远不会被嵌入,因此您需要选择另一种字体。

The example from the tutorial uses the free font FreeSans: 本教程中的示例使用免费字体FreeSans:

public const String FONT = "resources/font/FreeSans.ttf";

The font object is defined like this: 字体对象的定义如下:

PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.WINANSI, true);

This font is used in a Paragraph like this: 这种字体在Paragraph如下:

Paragraph p = new Paragraph();
p.SetFont(font);
p.Add(new Text("Font is embedded"));
document.Add(p);

This is the C# version. 这是C#版本。 If you need the Java version, take a look at the Java version of the tutorial : 如果需要Java版本,请查看本教程Java版本

public static final String FONT = "src/main/resources/font/FreeSans.ttf";
PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, true);
Paragraph p = new Paragraph();
p.setFont(font);
p.add(new Text("Font is embedded"));
document.add(p);

If you already use this approach, and you still get the error, you probably have some content somewhere for which you didn't define a font that is embedded. 如果您已经使用了这种方法,并且仍然出现错误,则可能是某些内容未定义嵌入字体的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM