0

I am using PDFBox 3.0.0.alpha3 to manipulate a PDF file and add text with the Roboto font and saving incrementally. The font is properly displayed when installed on Windows, but when opened on a different machine without the font installed, the characters are not displayed correctly.

Here is the code I use to load the font and manupilate pdf file :

private PDType0Font GetFont(PDDocument document, DocumentMemberConfigDto config) throws IOException {
String fontFileName = "path to file . ttf";
if (!Files.exists(fontFileName )) {
    fontFileName =  DefaultFont;
}
PDType0Font Font = PDType0Font.load(document,
        new File(fontFileName ));

 return Font;
}
public void PutTextFromConfig(DocumentMemberConfigDto conf) throws IOException {

    SelectPage((int) conf.PageNumero - 1);

    PDType0Font font = GetFont(document, conf);
    PDFontDescriptor fontDescriptor = font.getFontDescriptor();
    float fontHeight = fontDescriptor.getFontBoundingBox().getHeight() / 1000 * conf.FondSize;
    PDRectangle mediaBox = this.currentPage.getMediaBox();
    float pageHeight = mediaBox.getHeight();
    String Text = conf.fieldLibelle == " Liste déroulante " ? conf.Valeur.split("-")[0] : conf.Valeur;
    addTextWithTooltip(Text,
            (float) conf.X,
            (float) (pageHeight - conf.Y - (fontHeight * 0.75)),
            font,
            (int) conf.FondSize,
            conf.infobull,
            conf.isItalic,
            conf.isBold,
            conf.isUnderline,
            getColor(conf.Fill));

}
public PDFHandling(PDDocument document) throws IOException {
    this.document = document;
}

public void init() throws IOException {
    SelectPage(0);
}

public void SelectPage(int numPage) throws IOException {

    if (document.getNumberOfPages() - 1 >= numPage) {
        numPage = document.getNumberOfPages() - 1;
    }
    if (this.contentStream != null)
        this.contentStream.close();
    currentNumPage = numPage;
    currentPage = document.getPage(numPage);
    this.contentStream = new PDPageContentStream(document, currentPage, AppendMode.PREPEND, false);

}
private Color getColor(String colorCode) {
    if (colorCode.equals("red"))
        return Color.RED;
    else if (colorCode.equals("orange"))
        return Color.ORANGE;
    else if (colorCode.equals("green"))
        return Color.GREEN;
    else if (colorCode.equals("white"))
        return Color.white;
    else
        return Color.BLACK;
}

and i use this functions like this :

private byte[] AddFields(DocumentToSignDto documentToSigne) throws Exception {
    byte[] content = documentToSigne.Content;
    PDDocument document = Loader.loadPDF(content);
    String[] fieldLibelleTexts = new String[] { "Fonction", "E-mail", "Nom", "champ de saisie", "société", "Tel",
            "Texte", "Date signature", " Liste déroulante " };
    try (PDFHandling pdfHandling = new PDFHandling(document)) {
        pdfHandling.init();
        for (DocumentMemberConfigDto conf : documentToSigne.Configs) {
            if (conf.fieldLibelle != null && !conf.fieldLibelle.equals("")) {
                if (Arrays.asList(fieldLibelleTexts).contains(conf.fieldLibelle)) {
                    pdfHandling.PutTextFromConfig(conf);
                } else if (conf.fieldLibelle.equals(" Case à cocher") && conf.isChecked == true) {
                    pdfHandling.PutChekboxFromConfig(conf);
                } else if (conf.fieldLibelle.equals(" Bouton radio ") && conf.isChecked == true) {
                    pdfHandling.PutRadioBoxFromConfig(conf);
                } else {

                }
            }
        }
    }

    ByteArrayOutputStream baosAfterFildes = new ByteArrayOutputStream();
    document.saveIncremental(baosAfterFildes);

    return baosAfterFildes.toByteArray();
}

When I check the font properties in the generated PDF file (File > Properties > Fonts), it shows: Roboto-Regular Type: TrueType (CID) Encoding: Identity-H Actual Font: Unknown

What could be causing the font not to be properly embedded in the PDF file? How can I ensure the Roboto font is correctly embedded and displayed across different devices and PDF readers?

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • You're doing incremental saving, I mentioned this in the title and in the text because this is highly relevant. Unless there is a specific reason to do incremental saving, use normal saving. If you do really want to do incremental saving, read the javadoc of saveIncremental, and see also this ticket with a similar problem: https://issues.apache.org/jira/browse/PDFBOX-5583 – Tilman Hausherr Apr 07 '23 at 13:08
  • thanks to you Tilman – rahim soufiane Apr 11 '23 at 07:04

0 Answers0