如何使用第三方字体从 Unicode 字符集语言创建 PDF 文档

发布于 2024-11-10 16:57:53 字数 1892 浏览 6 评论 0 原文

我正在使用 PDFBoxiText 创建各种语言的简单(仅段落)pdf 文档。类似于:

pdfBox:

private static void createPdfBoxDocument(File from, File to) {
    PDDocument document = null;
    try {
        document = new TextToPDF().createPDFFromText(new FileReader(from));
        document.save(new FileOutputStream(to));
    } finally {
        if (document != null)
            document.close();
    }
}

private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    PDType1Font font = PDType1Font.TIMES_ROMAN;
    contentStream.setFont(font, 12);
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(100, 400);
    contentStream.drawString("š");
    contentStream.endText();
    contentStream.close();
    document.save("test.pdf");
    document.close();
}

itext

private static Font blackFont = new Font(Font.FontFamily.COURIER, 12, Font.NORMAL, BaseColor.BLACK);

private static void createITextDocument(File from, File to) {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(to));
    document.open();
    addContent(document, getParagraphs(from));
    document.close();
}

private static void addContent(Document document, List<String> paragraphs) { 

    for (int i = 0; i < paragraphs.size(); i++) {
        document.add(new Paragraph(paragraphs.get(i), blackFont));
    }
}

输入文件以 UTF-8 编码,并且 Unicode 字符集的某些语言(如俄语字母等)在 pdf 中无法正确呈现。我想这两个库中的字体都不支持 Unicode 字符集,并且我找不到任何有关如何添加和使用第三方字体的文档。有人可以帮我举个例子吗?

I'm using PDFBox and iText to create a simple (just paragraphs) pdf document from various languages. Something like :

pdfBox:

private static void createPdfBoxDocument(File from, File to) {
    PDDocument document = null;
    try {
        document = new TextToPDF().createPDFFromText(new FileReader(from));
        document.save(new FileOutputStream(to));
    } finally {
        if (document != null)
            document.close();
    }
}

private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    PDType1Font font = PDType1Font.TIMES_ROMAN;
    contentStream.setFont(font, 12);
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(100, 400);
    contentStream.drawString("š");
    contentStream.endText();
    contentStream.close();
    document.save("test.pdf");
    document.close();
}

itext:

private static Font blackFont = new Font(Font.FontFamily.COURIER, 12, Font.NORMAL, BaseColor.BLACK);

private static void createITextDocument(File from, File to) {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(to));
    document.open();
    addContent(document, getParagraphs(from));
    document.close();
}

private static void addContent(Document document, List<String> paragraphs) { 

    for (int i = 0; i < paragraphs.size(); i++) {
        document.add(new Paragraph(paragraphs.get(i), blackFont));
    }
}

The input files are encoded in UTF-8 and some languages of Unicode char set, like Russian alphabet etc., are not rendered properly in pdf. The Fonts in both libraries don't support Unicode charset I suppose and I can't find any documentation on how to add and use third party fonts. Could please anybody help me out with an example ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

网名女生简单气质 2024-11-17 16:57:53

如果您使用 iText,它有很好的支持。

在 iText in Action(第 2.2.2 章)中,您可以阅读更多内容。

您必须下载一些 unicode 字体,例如 arialuni.ttf 并执行以下操作:

    public static File fontFile = new File("fonts/arialuni.ttf");

    public static void createITextDocument(File from, File to) throws DocumentException, IOException {

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(to));
        document.open();
        writer.getAcroForm().setNeedAppearances(true);
        BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        FontSelector fs = new FontSelector();
        fs.addFont(new Font(unicode));

        addContent(document, getParagraphs(from), fs);
        document.close();
    }

    private static void addContent(Document document, List<String> paragraphs, FontSelector fs) throws DocumentException { 

        for (int i = 0; i < paragraphs.size(); i++) {
            Phrase phrase = fs.process(paragraphs.get(i));
            document.add(new Paragraph(phrase));
        }
    }

arialuni.ttf 字体对我有用,到目前为止我检查了它的支持

BG, ES, CS, DA, DE, ET, EL, EN, FR, IT, LV, LT, HU, MT, NL, PL, PT, RO, SK, SL, FI, SV

,并且只有罗马尼亚语语言的 PDF 未正确创建...

对于 PDFBox 几乎是一样的:

private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    PDFont font = PDTrueTypeFont.loadTTF(document, "fonts/arialuni.ttf");
    contentStream.setFont(font, 12);
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(100, 400);
    contentStream.drawString("š");
    contentStream.endText();
    contentStream.close();
    document.save("test.pdf");
    document.close();
}

但是正如 Gagravarr 所说,由于这个问题它不起作用 PDFBOX-903 。即使使用 1.6.0-SNAPSHOT 版本。也许行李箱会起作用。我建议你使用 iText。它在那里完美地工作。

If you are using iText, it has quite good support.

In iText in Action (chapter 2.2.2) you can read more.

You have to download some unicode Fonts like arialuni.ttf and do it like this :

    public static File fontFile = new File("fonts/arialuni.ttf");

    public static void createITextDocument(File from, File to) throws DocumentException, IOException {

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(to));
        document.open();
        writer.getAcroForm().setNeedAppearances(true);
        BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        FontSelector fs = new FontSelector();
        fs.addFont(new Font(unicode));

        addContent(document, getParagraphs(from), fs);
        document.close();
    }

    private static void addContent(Document document, List<String> paragraphs, FontSelector fs) throws DocumentException { 

        for (int i = 0; i < paragraphs.size(); i++) {
            Phrase phrase = fs.process(paragraphs.get(i));
            document.add(new Paragraph(phrase));
        }
    }

arialuni.ttf fonts work for me, so far I checked it support for

BG, ES, CS, DA, DE, ET, EL, EN, FR, IT, LV, LT, HU, MT, NL, PL, PT, RO, SK, SL, FI, SV

and only PDF in Romanian language wasn't created properly...

With PDFBox it's almost the same:

private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    PDFont font = PDTrueTypeFont.loadTTF(document, "fonts/arialuni.ttf");
    contentStream.setFont(font, 12);
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(100, 400);
    contentStream.drawString("š");
    contentStream.endText();
    contentStream.close();
    document.save("test.pdf");
    document.close();
}

However as Gagravarr says, it doesn't work because of this issue PDFBOX-903 . Even with 1.6.0-SNAPSHOT version. Maybe trunk will work. I suggest you to use iText. It works there perfectly.

赴月观长安 2024-11-17 16:57:53

您可能会发现这个答案很有帮助- 它确认您无法使用标准类型 1 字体之一执行您需要的操作,因为它们只是 Latin1

理论上,您只需要 将合适的字体嵌入到文档中,该字体处理您的所有代码点,并使用它。然而,在编写 unicode 字符串时至少存在一个未解决的错误,因此有一个可能它还不能工作...也尝试一下 svn trunk 中的最新 pdfbox,看看它是否有帮助!

You may find this answer helpful - it confirms that you can't do what you need with one of the standard type 1 fonts, as they're Latin1 only

In theory, you just need to embed a suitable font into the document, which handles all your codepoints, and use that. However, there's at least one open bug with writing unicode strings, so there's a chance it might not work just yet... Try the latest pdfbox from svn trunk too though to see if it helps!

徒留西风 2024-11-17 16:57:53

在我的项目中,我只是将支持UTF8(或任何你想要的语言)的字体复制到一个目录(或者你可以使用Windows字体路径)并添加一些代码,它看起来像这样

BaseFont baseFont = BaseFont.createFont("c:\\a.ttf", BaseFont.IDENTITY_H,true);
Font font = new Font(baseFont);
document.add(new Paragraph("Not English Text",font));

现在,你可以使用这个字体来显示你的各种语言的文本。

In my project, I just copied the font that supported UTF8 (or whatever language you want) to a directory (or you can used Windows fonts path) and add some code, it looked like this

BaseFont baseFont = BaseFont.createFont("c:\\a.ttf", BaseFont.IDENTITY_H,true);
Font font = new Font(baseFont);
document.add(new Paragraph("Not English Text",font));

Now, you can use this font to show your text in various languages.

后来的我们 2024-11-17 16:57:53

//使用此代码。有时 setfont() 不适用于 Paragraph

try
{

    FileOutputStream out=new FileOutputStream(name);

    Document doc=new Document();

    PdfWriter.getInstance(doc, out);

    doc.open();

    Font f=new Font(FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
    Paragraph p=new Paragraph("New PdF",f);

    p.setAlignment(Paragraph.ALIGN_CENTER);

    doc.add(p);
    doc.close();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}

//use this code.Sometimes setfont() willnot work with Paragraph

try
{

    FileOutputStream out=new FileOutputStream(name);

    Document doc=new Document();

    PdfWriter.getInstance(doc, out);

    doc.open();

    Font f=new Font(FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
    Paragraph p=new Paragraph("New PdF",f);

    p.setAlignment(Paragraph.ALIGN_CENTER);

    doc.add(p);
    doc.close();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文