0

I am trying to convert PDF to PDFA-2B. And 1 of my test files contains Type1 fonts without a font descriptor:

enter image description here

I need to embed these fonts.

How can i embed these fonts?

I tried to create a font descriptor but I was not successful.

private static PdfDictionary addFontDesc(PdfDictionary font){
        PdfDictionary fontDescriptor;
        fontDescriptor = new PdfDictionary(PdfName.FONTDESCRIPTOR);
        PRIndirectReference fd = fontDescriptor.getIndRef();

        if (fd != null){
            font.put(PdfName.FONTDESCRIPTOR, fd);
            System.out.println("fontDescriptor was added to font " + font.getAsName(PdfName.BASEFONT).toString());
        }else {
            System.out.println("Error while trying to add fontDescriptor to " + font.getAsName(PdfName.BASEFONT).toString());
        }
        return font;
}

I always get a NullPointerException.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
RWYA
  • 3
  • 3
  • 1
    As explained by @mkl in his comment to [iTextSharp embed subset fonts in existing PDF](http://stackoverflow.com/questions/39747620/itextsharp-embed-subset-fonts-in-existing-pdf), you are asking something that can't be answered in a Stack Overflow post. You need to hire an expert to achieve what you want. I'm voting to close the question as **too broad**. You need to provide the font program, you need to examine the content streams, pick the right encoding, and so on. Show us the progress in your development process and we can help. Ask us "do this for me" and we'll close your question. – Bruno Lowagie Oct 03 '16 at 08:13
  • 2
    Base Fonts don't need to be embedded, the PDF Viewer should supply them or their appropriate substitutes. See section 9.6.2.2 of the PDF Specification Standard Type 1 Fonts (Standard 14 Fonts). Type 1 fonts, known as the standard 14 fonts, are as follows: Times-Roman, Helvetica, Courier, Symbol, Times-Bold, Helvetica-Bold, Courier-Bold, ZapfDingbats, Times-Italic, Helvetica-Oblique, Courier-Oblique, Times-BoldItalic, Helvetica-BoldOblique, Courier-BoldOblique These fonts, or their font metrics and suitable substitution fonts, shall be available to the conforming reader. – joelgeraci Oct 03 '16 at 14:57
  • @JoelGeraci The standard 14 fonts need not be embedded as far as the PDF standard is concerned. Actually no fonts need to be embedded as far as that standard is concerned (Type 3 fonts being special as there is no font program to *embed*). But the OP wants to generate PDF/A files, and here the rule is : "The font programs for all fonts used for rendering within a conforming file shall be embedded within that file." – mkl Oct 04 '16 at 08:49
  • There's a pretty important bit of information missing: what are you converting with? I see code, but no explanation of when this code runs (part of conversion? post-conversion? sideloading util?). Specifically: Always use the right tool for the job. If you need to convert PDF to PDFA-2B, the obvious solution is to use Adobe's PDF tools to achieve that conversion, not homegrowing a solution for a 1000 page spec you can't possibly cover in your own code – Mike 'Pomax' Kamermans Oct 07 '16 at 16:10

1 Answers1

0

As @Bruno already said in a comment, your question in its current state is way too broad. Thus, this answer merely focuses on the tangible issue part you present:

always get a NullPointerException.

You seem to assume that the getIndRef in

PRIndirectReference fd = fontDescriptor.getIndRef();

makes the fontDescriptor an indirect object (as only indirect objects can have indirect references) if it not already is one.

This assumption is wrong. If you call getIndRef on an object which is not already indirect, you'll get null as you observe.


You can make any PdfObject indirect using the PdfWriter method addToBody which returns a PdfIndirectObject which you can query for its indirect reference using getIndirectReference,

public PdfIndirectObject addToBody(final PdfObject object) throws IOException

or

public PdfIndirectObject addToBody(final PdfObject object, final boolean inObjStm) throws IOException
mkl
  • 90,588
  • 15
  • 125
  • 265