PdfPageStamp text not properly embedding font

I have the following code, which produces a PDF whose text is not properly shown:

    var doc = new Document("220.pdf");
    var fontsPath = "fonts";
    final var pdfFolderFontSource = new FolderFontSource(fontsPath);
    FontRepository.getSources().add(pdfFolderFontSource);
    var docStamp = new Document();
    var pageStamp = docStamp.getPages().add();
    pageStamp.setPageSize(doc.getPages().get_Item(1).getPageRect(true).getWidth(),
            doc.getPages().get_Item(1).getPageRect(true).getHeight());
    var textFragment = new TextFragment("test");
    var font = FontRepository.findFont("OpenSans-Bold");
    textFragment.getTextState().setFont(font);
    textFragment.getTextState().setFontSize(10);
    textFragment.getTextState().setForegroundColor(Color.getBlack());
    textFragment.getTextState().setBackgroundColor(Color.getRed());
    var box = new FloatingBox();
    box.setWidth(100);
    box.setHeight(100);
    box.getParagraphs().add(textFragment);
    box.setBackgroundColor(Color.getGreen());

    pageStamp.getParagraphs().add(box);
    pageStamp.setBackground(Color.getTransparent());
    docStamp.processParagraphs();
    doc.getPages().get_Item(1).addStamp(new PdfPageStamp(pageStamp));
    doc.save("test2.pdf");

image.jpg (84.1 KB)
220.pdf (902.5 KB)

I cannot share the PDF I will be using to stamp other documents (the real one), but the behavior I am getting is the same.

@samuelmartinucci,

You do not have to share anything confidential but a single Pdf document where the issue can be replicated. It can be a random pdf with a random paragraph generated and if the problem happens there is more than enough.

So I am not clear on what are you trying to achive. Can you explain a little more. What where you trying to do, and what is the issue? Is not using the right font? Or the that the font is not embedded?

The code I shared (with the PDF attached) will produce a broken PDF.

As a side note, if I replace the last 2 lines with the code below, it works:

var doc2 = new Document();
    doc2.getPages().add(doc.getPages());
    doc2.getPages().get_Item(1).addStamp(new PdfPageStamp(pageStamp));
    doc2.save("test2.pdf");

In regards my requirements, I have a PDF (like the one I shared) which is going to be stamped with content in a PDF produced by another process. My code does pretty much what I have, except that the data is mocked.

@samuelmartinucci,

I asked you because the title is very misleading regarding what you want. Because you are missing the line that embeds the font. But from what you mention here, that is not the issue.

Let me guess what “broken” means, and I will answer later.

The PdfPageStamp document has the text “test”, which is a TextFragment that uses OpenSans-Bold (an embedded font).

If I open the produced document using Adobe Reader, I see the text corrupted (like in the shared screenshot). If I open using Adobe Reader the document metadata, the font is there though (and I see no difference with the version produced by the code I shared later.

@samuelmartinucci,

To embbed a font you need to use:

textState.getFont().setEmbedded(true);

I tried to add the following line to the code I shared an it doesn’t change the final result:

    textFragment.getTextState().getFont().setEmbedded(true);

Furthermore, I tried to debug the code and the font value had isEmbedded set to true already

@samuelmartinucci,

I cannot replicate the error,

Here is my code(same as yours except on how to get the width and height)

public void Logic() throws Exception
{
    var doc = new com.aspose.pdf.Document(PartialPath + "_input.pdf");

    var pdfFolderFontSource = new FolderFontSource(prefixPathFonts + "\\Open_Sans\\static\\OpenSans");
    FontRepository.getSources().add(pdfFolderFontSource);
    var docStamp = new com.aspose.pdf.Document();
    var pageStamp = docStamp.getPages().add();
    pageStamp.setPageSize(doc.getPages().get_Item(1).getPageInfo().getWidth(),
            doc.getPages().get_Item(1).getPageInfo().getHeight());
    var textFragment = new TextFragment("test");
    var font = FontRepository.findFont("OpenSans-Bold");
    textFragment.getTextState().setFont(font);
    textFragment.getTextState().setFontSize(10);
    textFragment.getTextState().setForegroundColor(Color.getBlack());
    textFragment.getTextState().setBackgroundColor(Color.getRed());
    var box = new FloatingBox();
    box.setWidth(100);
    box.setHeight(100);
    box.getParagraphs().add(textFragment);
    box.setBackgroundColor(Color.getGreen());

    pageStamp.getParagraphs().add(box);
    pageStamp.setBackground(Color.getTransparent());
    docStamp.processParagraphs();

    doc.getPages().get_Item(1).addStamp(new PdfPageStamp(pageStamp));

    doc.save(PartialPath + "_output.pdf");
}

Here is the input and output files:
BrokenFloatingBox_input.pdf (902.5 KB)
BrokenFloatingBox_output.pdf (925.7 KB)

I am using version 23.3.