亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(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.PDCIDFontType2findFontOrSubstitute中,它試圖再次從文件系統加載字體文件&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

通用課目體育訓練成績評定