简体   繁体   English

使用 PDFbox 创建引用 OTF 字体的 PDF

[英]Using PDFbox to create PDF referencing OTF fonts

Good morning!早上好!

I have a newbie PDFbox question which I'm hoping for some help with ...我有一个新手 PDFbox 问题,希望对...

I've just (last week) downloaded the latest PDFbox source from github and am trying to create a HelloWorldOTF.java, based on the HelloWorldTTF.java example, with the hope of creating a PDF file which uses an OTF font (in this case, Adobe Caslon Pro Regular) to add text to the output PDF.我刚刚(上周)从 github 下载了最新的 PDFbox 源代码,并试图基于 HelloWorldTTF.java 示例创建一个 HelloWorldOTF.java,希望创建一个使用 OTF 字体的 PDF 文件(在这种情况下, Adob​​e Caslon Pro Regular) 将文本添加到输出 PDF。

Here's what I have so far:这是我到目前为止所拥有的:

        doc = new PDDocument();

        PDPage page = new PDPage();
        doc.addPage(page);

        String testOtfFontFile =  "c:/windows/fonts/ACaslonPro-Regular.otf";
        String testTtfFontFile = "c:/windows/fonts/arial.ttf";
        String testPdfFile = "c:/tmp/pdfboxtest.pdf";

        CFFFont font = loadCFFFont(testOtfFontFile);

        PDFont ttfFont = PDTrueTypeFont.loadTTF(doc, new File(testTtfFontFile));

        PDPageContentStream contentStream = new PDPageContentStream(doc,
                page);
        contentStream.beginText();
        // How to set the CFFFont?
        contentStream.setFont(ttfFont, 12);
        contentStream.moveTextPositionByAmount(100, 700);
        contentStream.drawString(text);
        contentStream.endText();
        contentStream.close();
        doc.save(testPdfFile);
        System.out.println(testPdfFile + " created!");

I can load a CFFFont using this code: (loadCFFFont()):我可以使用以下代码加载 CFFFont: (loadCFFFont()):

            CFFFont cff = null;
            input = new FileInputStream(file);
            byte[] bytes = IOUtils.toByteArray(input);
            CFFParser cffParser = new CFFParser();
            cff = cffParser.parse(bytes).get(0);

... but can't for the life of me figure out how to get from a CFFFont to a PDFont in order to be able to use it to set the font via setFont(). ...但我一生都无法弄清楚如何从 CFFFont 到 PDFont 以便能够使用它通过 setFont() 设置字体。

Any help or pointers would be hugely appreciated ...任何帮助或指示将不胜感激......

Thanks a million for reading this far ;)感谢一百万读到这里;)

It worked for me by referring to this link.通过参考此链接,它对我有用

Using OTFParser to covert otf to ttf.使用 OTFParser 将 otf 转换为 ttf。

OTFParser otfParser = new OTFParser();
OpenTypeFont otf = otfParser.parse(new File("C:/Users/beder/Downloads/code/CODE Light.otf"));

PDFont font = PDType0Font.load(document, otf, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM