公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了添加了无法为Acrobat加载正确的pdfbox的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码嵌入字体,
基于Stackoverflow和PDFBOX-2661:

作为Helvetica的替代字体嵌入的字体为DejaVuSans。

// given: PDDocument document, PDAcroForm acroForm

InputStream font_file = ClassLoader.getSystemResourceAsStream("DejaVuSans.ttf");
font = PDType0Font.load(document, font_file);
if (font_file != null) {
    font_file.close();
}
System.err.println("Embedded font 'DejaVuSans.ttf' loaded.");

PDResources resources = acroForm.getDefaultResources();
if (resources == null) {
    resources = new PDResources();
}

resources.put(COSName.getPDFName("Helv"), font);
resources.put(COSName.getPDFName("Helvetica"), font);
// Also use "DejaVuSans.ttf" for "HeBo", "HelveticaBold" and "Helvetica-Bold" in a similar way, but this is left out to keep this short.

acroForm.setDefaultResources(resources);

// let pdfbox handle refreshing the values, now that all the fonts should be there.
acroForm.refreshAppearances();

然而,在acroForm.refreshAppearances()中,它导致了大量的Using fallback font LiberationSans for CID-keyed TrueType font DejaVuSans。稍加调试,在org.apache.pdfbox.pdmodel.font.PDCIDFontType2的findFontOrSubstitute中,它试图再次从文件系统加载字体文件&DejaVuSans",而不是使用提供的资源。由于它是在JAR文件中提供的,而不是从正常的文件系统(系统的字体)中找到的,因此将使用备用字体。

如何使其正确识别并加载字体?

我已经尝试的内容:

我尝试扩展字体加载机制,但由于所有内容都是私有的和/或最终的,我不得不在复制了大约10个未更改的原始代码文件后停止,以便能够访问它们;这必须以不同的方式实现。

直接写入ContentStream似乎使用不同的方式(contentStream.setFont(pdfFont, fontSize)),因此不受影响。

推荐答案

PDFBox中当前的AcroForm表单域刷新机制不能真正与尚未设置子集的字体结合使用。

原因是,每当使用字体刷新外观时,都会从某些资源词典中检索到该字体。然而,在这些资源词典中,没有您的原始PDType0Font,而只有支持您的PDType0Font的PDF对象的初步版本。但是这些PDF对象不知道它们支持的是最终将被子集化的字体,所以检索该字体会生成一个新的、不同的PDType0Font对象,该对象声称是非嵌入的。因此,它也不会被告知最终要嵌入的字形。

这也是为什么您使用的PDType0Font.load方法会用提示来记录(JavaDoc注释)如果您要为AcroForm加载字体,请改用3参数构造函数:

/**
 * Loads a TTF to be embedded and subset into a document as a Type 0 font. If you are loading a
 * font for AcroForm, then use the 3-parameter constructor instead.
 *
 * @param doc The PDF document that will hold the embedded font.
 * @param input An input stream of a TrueType font. It will be closed before returning.
 * @return A Type0 font with a CIDFontType2 descendant.
 * @throws IOException If there is an error reading the font stream.
 */
public static PDType0Font load(PDDocument doc, InputStream input) throws IOException

其文档中的3参数构造函数告诉您在AcroForm使用时不要使用字体的子集:

/**
 * Loads a TTF to be embedded into a document as a Type 0 font.
 *
 * @param doc The PDF document that will hold the embedded font.
 * @param input An input stream of a TrueType font. It will be closed before returning.
 * @param embedSubset True if the font will be subset before embedding. Set this to false when
 * creating a font for AcroForm.
 * @return A Type0 font with a CIDFontType2 descendant.
 * @throws IOException If there is an error reading the font stream.
 */
public static PDType0Font load(PDDocument doc, InputStream input, boolean embedSubset)
        throws IOException

但是,即使在设置为时使用3个参数构造函数也不会呈现良好结果。乍一看,渲染的字段看起来还不错:

但你一点击它们,就会发生一些奇怪的事情:

@Tilman,这里可能仍有需要解决的问题。


子集嵌入字体的潜在问题也可能出现在其他上下文中,例如:

try (   PDDocument pdDocument = new PDDocument();
        InputStream font_file = [...]    ) {
    PDType0Font font = PDType0Font.load(pdDocument, font_file);

    PDResources pdResources = new PDResources();
    COSName name = pdResources.add(font);
    PDPage pdPage = new PDPage();
    pdPage.setResources(pdResources);
    pdDocument.addPage(pdPage);

    try (   PDPageContentStream canvas = new PDPageContentStream(pdDocument, pdPage)    ) {
        canvas.setFont(pdResources.getFont(name), 12);
        canvas.beginText();
        canvas.newLineAtOffset(30, 700);
        canvas.showText("Some test text.");
        canvas.endText();
    }

    pdDocument.save("sampleOfType0Issue.pdf");
}

(RefreshAppearances测试testIllustrateType0Issue)

这篇关于添加了无法为Acrobat加载正确的pdfbox的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:Acrobat pdfbox 加载 字体 正确 添加
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定