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.)