1

I've got a single page pdf form that uses custom fonts (made with libreoffice). The final output file will use this template multiple times. Each form control within the page has my custom font set as it's font type. The pdf file also has the fonts embedded. Here're the file properties according to foxit. I also copied the file to system without the fonts installed and filled it in with Reader DC and the fonts do show up correctly. Additionally, the fonts are installed on my system as well. Here's the relevant code to fill the form fields.

try(PDDocument finalDoc = new PDDocument())
    {
        template = new File(templatePath);
        List<PDField> fields = new ArrayList<PDField>();


        System.out.println("Pages to build: " + results.size());
        for(i = 0; i < results.size(); i++)
        {
            System.out.println("Building page: " + (i+1));
            singleResult = results.get(i);
            PDDocument doc = PDDocument.load(template);
            PDDocumentCatalog documentCatalog = doc.getDocumentCatalog();
            PDAcroForm acroForm = documentCatalog.getAcroForm();
            //acroForm.setNeedAppearances(false); //#First option

            PDField field = acroForm.getField("txtUsername");
            field.setValue(nameAndNumber.get(0));
            field.setPartialName("txtUsername" + i);
            fields.add(field);

            field = acroForm.getField("txtPhoneNumber");
            field.setValue(nameAndNumber.get(1));
            field.setPartialName("txtPhoneNumber" + i);
            fields.add(field);

            finalDoc.addPage(doc.getPage(0));
        }

        PDAcroForm finalForm = new PDAcroForm(finalDoc);
        //finalForm.setNeedAppearances(false); //#Second option
        finalDoc.getDocumentCatalog().setAcroForm(finalForm);
        finalForm.setFields(fields);
        finalForm.flatten(fields, true); //#Third option

        finalDoc.save(new File("outputPDF.pdf"));
    }catch(Exception e)
    {
        //Do Stuff
    }

Now, if either options 1,2 or 3 (as marked above) are commented out, I get no visible text in my outputPDF.pdf file. If either of those three is uncommented, I get visible text, but it's in Arial with the following warnings

Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'ArialMT' for 'AccordAltMedium'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'ArialMT' for 'AccordAltMedium'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'Arial-BoldMT' for 'AccordAltBold'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'Arial-BoldMT' for 'AccordAltBold'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'Arial-BoldMT' for 'AccordAltBold'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'Arial-BoldMT' for 'AccordAltBold'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'ArialMT' for 'AccordAltRegular'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'ArialMT' for 'AccordAltRegular'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'Arial-BoldMT' for 'AccordAltBold'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'Arial-BoldMT' for 'AccordAltBold'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'Arial-BoldMT' for 'AccordAltBold'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'ArialMT' for 'AccordAltRegular'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'ArialMT' for 'AccordAltRegular'
Jul 21, 2016 8:29:29 PM org.apache.pdfbox.pdmodel.font.PDTrueTypeFont <init>
WARNING: Using fallback font 'Arial-BoldMT' for 'AccordAltBold'

I'm not entirely sure at this point what's causing the problem. The closest I can guess is that when I try to read all fonts embedded in my template using this code:

PDDocument docTemp = PDDocument.load(new File(templatePath));
        PDPage page = docTemp.getPage(0);
        Iterable<COSName> pageFonts=page.getResources().getFontNames();
        for(COSName c:pageFonts)
        {
            System.out.println(c.getName());
        }

I see this list as output:

Font:F1
Font:F2
Font:F3
Font:He

I'm guessing this means that PDFBox isn't recognizing the fonts from the document as the correct ones, but I'm not sure why that is. I'm using PDFBox 2.0.2, but I've tried 2.0.1 as well. LibreOffice Writer is version 5.1.4.2

Can someone please help me? I've spent my entire workday on this and I'm at my wits end. I've also tried exporting as PDF and FDF but that made no difference.

Link to pdf I'm not sure why this file isn't larger, singe each of the three fonts are 30kb each, but I don't know enough about the PDF standard to be sure of anything. Link to odt (large because fonts embedded.)

Yash Capoor
  • 346
  • 4
  • 14
  • please open the file .pdfbox.cache from your user directory, then post the lines with "AccordAlt". Are the names spelled exactly like in the messages? (Btw your fonts are not embedded in the PDF) – Tilman Hausherr Jul 22 '16 at 10:47
  • @TilmanHausherr there are 3-4 lines for each font. Here're the lines for AccordAltBold `AccordAltBold|OTF||2bc|0|20000111|0|1|02000000000000000000|C:\WINDOWS\FONTS\Soneri Type - AccordAltBold.otf` `AccordAltBold|OTF||2bc|0|20000111|0|1|02000000000000000000|C:\WINDOWS\FONTS\Soneri Type - AccordAltBold_0.otf` `AccordAltBold|OTF||2bc|0|20000111|0|1|02000000000000000000|C:\WINDOWS\FONTS\Soneri Type - AccordAltBold_1.otf` `AccordAltBold|OTF||2bc|0|20000111|0|1|02000000000000000000|C:\WINDOWS\FONTS\Soneri Type - AccordAltBold_2.otf` – Yash Capoor Jul 22 '16 at 11:05
  • And if Libre isn't embedding fonts, I'll go over to the asklib forums to get an answer as to how to make it do so. – Yash Capoor Jul 22 '16 at 11:08
  • It definitively isn't embedding them. Maybe it is an option in the file save dialog? Or maybe the font has some flag that forbids embedding? – Tilman Hausherr Jul 22 '16 at 11:12
  • I'm clueless about why PDFBox isn't using the font on your system. I can't test this as I don't have that font. Can you reproduce your test with a font that is usually there? E.g. calibri, tahoma, verdana? – Tilman Hausherr Jul 22 '16 at 11:16
  • @Tilman I'm a little out of my depth here. Pretty much a rookie when if comes to fonts and pdfs. How do I check if the OTF file has a flag preventing embedding? And how would I check the final PDF to see if the fonts are actually being embedded? Lastly, since the names in the .pdfbox.cache seem to match those in the warning, am I missing some call to use these specific fonts? Here's the PDF export screen, which has [no specific option to enable embedding](https://i.imgur.com/7V1I6mt.png), and I've posted on the Ask LibreOffice Q&A section to find out if I'm missing something. – Yash Capoor Jul 22 '16 at 11:35
  • @Tilman Additionally, I just tested using calibri, and it works just fine. Oddly enough, I think it worked fine once or twice a few days ago when I was just playing around with PDFBox and forms trying to get the hang of it. I'll try wiping the font cache, uninstalling the fonts, rebooting, reinstalling, rebooting and then running the application again. As close to a 'new' state as I can bring on my machine. – Yash Capoor Jul 22 '16 at 11:36
  • Re OTF - I don't know. Re "embedded or not" - in Adobe Reader it shows "Embedded" or "Embedded subset" if it is embedded. Don't know about foxit. In PDFDebugger, fonts with embedding have a "fontfile" entry in the font descriptor, for your file it would be at Root/Pages/Kids/[0]/Resources/Font/F1/FontDescriptor . Re PDF export screen, the PDF/A option should enable embedding because this is a requirement for PDF/A. – Tilman Hausherr Jul 22 '16 at 11:45
  • PDF/A removes form fields so I get NullPpointerExeptions as soon as I try `acroForm.getField("fieldname")` – Yash Capoor Jul 22 '16 at 11:50
  • ah, I see your question: https://ask.libreoffice.org/en/question/73754/how-do-i-make-libreoffice-writer-embed-a-custom-font-when-exporting-to-pdf/ – Tilman Hausherr Jul 22 '16 at 11:57

0 Answers0