Hello PdfBox Team,
I ran into a problem where PDFs programmatically created and merged using
PdfBox fail to display all characters written in a type1 font. When opening
such a file with Adobe it shows the error message "Cannot find or create the
font 'NotoSans'. Some characters may not display or print correctly" and every
character displays as a black dot.
I believe that org.apache.pdfbox.multipdf.PDFMergerUtility has a bug where it
is failing to subset type1 fonts for appended documents unless individual
documents are saved first.
To reproduce the issue, I've included a minimal example below. The example is
based on one of the PdfBox examples except it loads a font from the filesystem
and "merges" the single PDF. If you run the code as-is, you'll get a PDF with
the missing font. If you run it again after uncommenting the line
"document.save(new ByteArrayOutputStream());" then the PDF contains the font
and works properly.
Thanks,
Jeffrey
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
// Create a new font object selecting one of the PDF base fonts
// use a different font
// PDFont font = PDType1Font.HELVETICA_BOLD;
PDFont font = PDType0Font.load(document, new
File("NotoSans/NotoSans-Regular.ttf"));
// Start a new content stream which will "hold" the to be
created content
PDPageContentStream contentStream = new
PDPageContentStream(document, page);
// Define a text content stream using the selected font, moving
the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.moveTextPositionByAmount(100, 700);
contentStream.drawString("Hello World");
contentStream.endText();
// Make sure that the content stream is closed:
contentStream.close();
// Save the results and ensure that the document is properly
closed:
// Uncomment the next line to make it work. Presumably because
it subsets the fonts.
// document.save(new ByteArrayOutputStream());
PDFMergerUtility merger = new PDFMergerUtility();
// Merge one document
PDDocument outDoc = new PDDocument();
for (PDDocument curDoc : Arrays.asList(document))
{
merger.appendDocument(outDoc, curDoc);
curDoc.close();
}
outDoc.save("Merged.pdf");
outDoc.close();
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]