相关文章推荐
率性的玉米  ·  linux tpm模拟器 - ...·  1 年前    · 
完美的双杠  ·  CMake错误。"add_subdirect ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to create new page in document and write some text to it, while using the font contained in the file.

The font is extracted from the resources:

PDPage page = document.getPage(0);
PDResources res = page.getResources();
List<PDFont> fonts = new ArrayList<>();
for (COSName fontName : res.getFontNames()) {
     PDFont font = res.getFont(fontName);
     System.out.println(font);
     fonts.add(font);

And later used to write some text:

stream.beginText();
stream.setFont(fonts.get(0), 12);
stream.setTextMatrix(Matrix.getTranslateInstance(20, 50));
stream.showText("Protokol");
stream.endText();

The showText method always fails with error

No glyph for U+0050 (P) in font QZHBRL+ArialMT

But the glyph is there, as verified by FontForge:

Also the method hasGlyph returns true.

The complete project including the PDF is available at github repository showing the issue

Here you actually ran into an open PDFBox TODO, your stream.showText eventually calls encode of the underlying CID font for each character and here we have:

public class PDCIDFontType2 extends PDCIDFont
    public byte[] encode(int unicode)
        int cid = -1;
        if (isEmbedded)
            // otherwise we require an explicit ToUnicode CMap
            if (cid == -1)
                //TODO: invert the ToUnicode CMap?
                // see also PDFBOX-4233
                cid = 0;
        if (cid == 0)
            throw new IllegalArgumentException(
                    String.format("No glyph for U+%04X (%c) in font %s", unicode, (char) unicode, getName()));
        return encodeGlyphId(cid);

(org.apache.pdfbox.pdmodel.font.PDCIDFontType2)

Where PDFBox could not otherwise determine a mapping from Unicode to glyph code (if (cid == -1)), the JavaDoc comments indicate another way to determine a glyph code, an inverse lookup of the ToUnicode map. If this was implemented, PDFBox could have determined a glyph ID and written your text.

Unfortunately it is not implemented yet.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.