2

I need to add some text to PDF/A files using the Apache PDFBox library for Java. The problem is that, because it needs to be a valid PDF/A file, all the used fonts must be embedded in it. I know that I can embed a TTF font using PDFBox, but I'd like to avoid having to provide a font file with the application, so I was wondering if there's a way to embed one of the standard fonts available in PDFBox as if it was external.

For example, when I write something using one of the standard fonts, the PDF validator complains about this:

enter image description here

I've used the following code to write the text:

  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();

Is there any option to force the embed of the font when setting it?

Thanks in advance,

Community
  • 1
  • 1
Fel
  • 4,428
  • 9
  • 43
  • 94
  • *"I was wondering if there's a way to embed one of the standard fonts available in PDFBox as if it was external."* - those fonts you mean are *available in pdfbox* because they are available in every pdf viewer. Pdfbox does not include those fonts completely, it merely knows some metadata of them. Thus, to embed fonts you do need font files. – mkl Dec 18 '18 at 06:42

1 Answers1

1

There is only one font embedded in PDFBox. You can use it this way:

PDFont font = PDType0Font.load(doc, SomePdfboxClass.class.getResourceAsStream(
                   "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97