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



我正在使用PDFBox和iText从各种语言创建一个简单的(只是段落)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字符集,而且我找不到任何关于如何添加和使用第三方字体的文档。有人能帮我举个例子吗?

如果您正在使用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。它在那里工作得很好。

你可能会发现这个答案很有帮助-它证实了你不能用标准的1型字体做你需要的事情,因为它们只是拉丁语的

理论上,你只需要在文档中嵌入一个合适的字体,它可以处理你所有的代码点,并使用它。然而,在编写unicode字符串时至少有一个开放的错误,所以它可能还不起作用。。。试试svn trunk中最新的pdfbox,看看它是否有帮助!

在我的项目中,我刚刚将支持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));

现在,您可以使用此字体以各种语言显示文本。

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

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);
    }
}

最新更新