PABLO
PABLO

Reputation: 1

Embedding a font into an existing PDF using iText 5.5.13.2 and PDFBox 2.0.29 to convert it to PDF/A:

I have a problem. I need to be able to create a PDF/A from an existing PDF, but the validation fails because there are non-embedded fonts within the PDF. The technologies I am using are Java 8, iText 5.5.13.2, and PDFBox 2.0.29.

The issue is with embedding the non-embedded fonts.

  
    private static byte[] embeddingFont(byte[] bytes) {
        try {
            PDDocument document = PDDocument.load(new ByteArrayInputStream(bytes));
            PDType0Font arialFont = PDType0Font.load(document, new FileInputStream(ARIAL_TTF), true);
            for (PDPage page : document.getPages()) {
                PDFont font = PDType1Font.HELVETICA_BOLD;
                Iterable<COSName> fontNames = page.getResources().getFontNames();
                for (COSName fontName : fontNames) {
                    if (!font.isEmbedded() && font.getFontDescriptor() != null) {

                        if (arialFont != null) {
                            page.getResources().put(fontName, arialFont);
                        }
                    }

                }
            }
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            document.save(outputStream);
            document.close();
            return outputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}


I hope to find a solution with the technology I use or else find a free alternative

Upvotes: 0

Views: 150

Answers (0)

Related Questions