簡體   English   中英

如何使用PDFBox將標准字體嵌入到生成的PDF中

[英]How to embed an standard font into generated PDF with PDFBox

我需要使用適用於Java的Apache PDFBox庫向PDF / A文件中添加一些文本。 問題在於,因為它必須是有效的PDF / A文件,所以所有使用的字體都必須嵌入其中。 我知道我可以使用PDFBox嵌入TTF字體,但是我想避免在應用程序中提供字體文件,所以我想知道是否有一種方法可以在PDFBox中嵌入一種可用的標准字體,就好像它是外部的。

例如,當我使用一種標准字體寫東西時,PDF驗證器會抱怨:

在此處輸入圖片說明

我使用以下代碼編寫文本:

  PDFont standardFont = PDType1Font.HELVETICA_BOLD;

  PDPage pag = new PDPage();

  pag.setResources(new PDResources());

  PDPageContentStream contentStream = new PDPageContentStream(pdfFile, pag);

  //Begin the Content stream 
  contentStream.beginText();       

  //Setting the font to the Content stream  
  contentStream.setFont(standardFont, 12);

  //Setting the position for the line 
  contentStream.newLineAtOffset(25, 500);

  //Adding text in the form of string 
  contentStream.showText("JUST A SAMPLE STRING");      

  //Ending the content stream
  contentStream.endText();

  //Closing the content stream
  contentStream.close();

  pdfFile.addPage(pag);

  pdfFile.save(file);

  pdfFile.close();

設置字體時是否有任何選項可以強制嵌入字體?

提前致謝,

PDFBox中僅嵌入一種字體。 您可以通過以下方式使用它:

PDFont font = PDType0Font.load(doc, SomePdfboxClass.class.getResourceAsStream(
                   "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM