I want to merge two PdDocument with:
Both documents created from scratch in java. I open PDPageContentStream for each document, add some text and then close PDPageContentStream. For each document I used PdFont which declared by next code:
Then I merge documents:
Then close document2:
And save document1 to OutputStream:
I get pdf file with all fonts embedded as subset.
Font is embeded correctly only for pages created with document1, pages created with document2 are present, but no embed font for them.
As a result if I open created pdf file in OS which has Calibri.ttf I see correct font on all pages, if Calibri.ttf is not exist then font is correct only on pages created with document1.
I see that PdDocument has field:
fonts are added to this field when client call:
and actual embedding happens in method:
In my example above, method save is never called for document2.
We append docuement2 to document1 and save only document1.
I reviewed method:
And I did not find that this method do something with fontsToSubset field.
So I create next method:
And used it before merge documents:
(I need to use some Reflection because fontsToSubset is private part of PdDocument)
I think other and maybe better option maybe:
But I did not tested this option.
I think this problem should be solved on library side in PDFMergerUtility#appendDocument method, and not in client code. Or we should have javadoc which tells that we should use PDFMergerUtility#appendDocument only for saved PdDocument