问题描述
|
我正在使用itext创建PDf文档。由于许可限制,某些字体不能使用。
...
ExceptionConverter: com.lowagie.text.DocumentException: C:\\WINDOWS\\Fonts\\LucidaSansRegular.ttf cannot be embedded due to licensing restrictions.
at com.lowagie.text.pdf.TrueTypeFontUnicode.<init>(UnkNown Source)
at com.lowagie.text.pdf.BaseFont.createFont(UnkNown Source)
at com.lowagie.text.pdf.BaseFont.createFont(UnkNown Source)
at com.lowagie.text.pdf.BaseFont.createFont(UnkNown Source)
at com.lowagie.text.pdf.DefaultFontMapper.awtToPdf(UnkNown Source)
at com.lowagie.text.pdf.PdfGraphics2D.getCachedBaseFont(UnkNown Source)
at com.lowagie.text.pdf.PdfGraphics2D.setFont(UnkNown Source)
...
我正在考虑检查字体或PDF内容以检查这种情况。如何使用Java或itext检查字体是否可嵌入?
解决方法
据我所知,还没有直接的方法来识别是否可以嵌入字体。
我进行了快速搜索,除了使用Erik在评论中提到的异常捕获方法外,我认为这是不可能的。
// 1) have a list of all fonts ArrayList allAvailableFonts;
// 2) second list of fonts that that can be embedded ArrayList embedableFonts;
//Iterate through every available font in allAvailableFonts
for( .... allAvailableFonts ..... )
{
boolean isFontEmbeddable = true;
try
{
// try to embed the font
}
catch( DocumentException de)
{
//this font cannot be embedded
isEmbeddable = false;
}
if( isEmbeddable )
{
// add to list of embeddable fonts
embedableFonts.add ( font );
}
}
您可能真的可以成为硬核,并执行对Windows Apis的本地调用来获得相同的结果,但是我认为对于这样一个简单的任务,它的工作量太大。
做了一些研究,发现Java如何抛出此异常
可以在这里找到生成上述异常的代码。
http://kickjava.com/src/com/lowagie/text/pdf/TrueTypeFont.java.htm
行号367,368
if (!justNames && embedded && os_2.fsType == 2)
throw new DocumentException(fileName + style + \" cannot be embedded due to licensing restrictions.\");
要注意的有趣部分是条件os_2.fsType == 2
os_2是WindowsMetrics
的实例,请参见第174行
http://kickjava.com/src/com/lowagie/text/pdf/TrueTypeFont.java.htm
在Google中搜索WindowsMetrics,这就是我得到的。
这说明参数fsType包含有关是否可以嵌入字体的信息。
http://www.microsoft.com/typography/otspec/os2ver3.htm#fst
itext中使用的WindowsMetrics的Java等效项
http://www.docjar.org/docs/api/com/lowagie/text/pdf/TrueTypeFont.WindowsMetrics.html