0

I am trying to use PDFBox 2.0 (snapshot of 20151009) due to the availability of TTC support. But I haven't found any documentation on how to use this feature. I found a ticket here https://issues.apache.org/jira/browse/PDFBOX-2752 and I found how to load TTC file:

    InputStream is = MyClass.class.getResourceAsStream("font.ttc");
    TrueTypeCollection coll = new TrueTypeCollection(is);

but I don't know how to embed TrueTypeFont into my PDDocument. In PDFBox 1.8 I was using something similar to the following:

public String addFont(String key, PDFont font){

    PDResources res = pdfForm.getDefaultResources();

    if (res == null){
        res = new PDResources();
    }

    String fontName = res.addFont(font);

    pdfForm.setDefaultResources(res);

    return fontName;
}

but know I have a TrueTypeFont not a PDFont. How can I "convert" a TrueTypeFont into PDFont ? Or am I using something in a wrong way ?

Thanks

Ernest Poldrige
  • 399
  • 1
  • 6
  • 17
  • 1
    This is a bit confusing... the issue you mention was about using TTC files on the computer to improve rendering. Your question in the text is different, it asks how to use TT fonts. Later it asks how to convert a font. So it is three different questions, and I don't know what you really want to do. If you want to embed an ordinary TT font (.ttf file), see the EmbeddedFonts.java example. – Tilman Hausherr Oct 09 '15 at 17:55
  • Sorry if I was misleading. I have a TTC file and I want to embed this set of TTF file in a PDDocument. – Ernest Poldrige Oct 10 '15 at 09:36
  • You can't embed the whole file, as PDF does not have the concept of TTC itself. (see 9.5 in http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf ). What I tried is this: take the TrueTypeCollection.getFonts(), choose a font name, and return an inputStream of it by using getOriginalData(). That one could then be used by PDType0Font.load(). However it didn't work, I get IOException: head is mandatory :-( So plan B for you is http://stackoverflow.com/questions/15455895/convert-or-extract-ttc-font-to-ttf-how-to – Tilman Hausherr Oct 10 '15 at 11:30
  • https://issues.apache.org/jira/browse/PDFBOX-3018 has resulted in an improvement, you can now pass a TrueTypeFont to PDType0Font.load(). Please give feedback there how it worked for you. – Tilman Hausherr Oct 17 '15 at 21:45
  • And now there's also a convenience method TrueTypeCollection.getFontByName(fontName). Please give some feedback that it works for you. – Tilman Hausherr Oct 18 '15 at 17:19
  • I was in a hurry and I solved my problem exporting ttc to ttfs using fontforge. – Ernest Poldrige Oct 20 '15 at 10:57

1 Answers1

1

This is brandnew (https://issues.apache.org/jira/browse/PDFBOX-3018), and will be in the upcoming 2.0 release (but not in RC1). Here's a sample code for windows:

PDFont font = PDType0Font.load(document, new TrueTypeCollection(new File("c:/windows/fonts/MSGothic.ttc")).getFontByName().get("MS-Gothic"), true);
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97