【问题标题】:Fonts not properly embedded in generated PDF using itextsharp 5.0.5.0字体未正确嵌入使用 itextsharp 5.0.5.0 生成的 PDF
【发布时间】:2026-02-08 10:30:01
【问题描述】:

我正在尝试使用以下代码使用 itextsharp api 为“古吉拉特语字体”生成 PDF。

BaseFont gujarati = BaseFont.CreateFont("D\Gujarati\shruti.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 字体 fontNormal = new Font(gujarati, 10, Font.NORMAL);

PDF 正在生成,但字体未正确显示。如果我将相同的 PDF 转换为 word,则字体会正确显示。

【问题讨论】:

  • 您需要 itext7 和 caligraph 插件来支持印度语脚本。它不适用于 itext5。
  • 除了@Paulo 说的:即使你需要使用itext 5 尝试一些东西,5.0.5.0 版本是古老的,现在的5.5.x 版本在很多方面都得到了改进。
  • 我已将 itext5 升级到 itext 7.1.x,但问题仍未解决。与显示的 itext5 输出相同。文档 arabicPdf = new Document(new PdfDocument(new PdfWriter("D:\\test.pdf")));字符串 ARIALUNI_TFF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "shruti.ttf"); PdfFont f = PdfFontFactory.CreateFont("D:\\Gujarati\\New folder\\shruti.ttf", PdfEncodings.IDENTITY_H, true); arabicPdf.SetFont(f); arabicPdf.Add(new Paragraph("iText 是:નમસ્તે ગતિના").SetFont(f)); arabicPdf.Close();

标签: itext


【解决方案1】:

这篇文章详细介绍了如何加载pdfCalligraph,使用正确的字体(因为字体必须能够显示字符),并将它们添加到PdfDocument

首先,您需要确保 pdfCalligraph 在您的类路径中。 如果您使用的是 Maven,只需添加依赖项即可。

https://developers.itextpdf.com/content/itext-7-jump-start-tutorial/installing-itext-7

其次,您需要加载有效的许可证。 pdfCalligraphiText 平台的闭源插件。所以需要许可证。

https://developers.itextpdf.com/content/license-key-frequently-asked-questions/how-do-i-load-license-key

接下来,下面的代码示例应该可以做到这一点。

Document arabicPdf = new Document(new PdfDocument(new PdfWriter("/path/to/arabic.pdf")));

// Arabic text starts near the top right corner of the page
arabicPdf.setTextAlignment(TextAlignment.RIGHT);

// create a font, and make it the default for the document
PdfFont f = PdfFontFactory.createFont("/path/to/DroidKufi-Regular.ttf", PdfEncodings.IDENTITY_H, true);
arabicPdf.setFont(f);

// add content: السلام عليكم (as-salaamu 'aleykum - peace be upon you)
arabicPdf.add(new Paragraph("\u0627\u0644\u0633\u0644\u0627\u0645 \u0639\u0644\u064A\u0643\u0645"));

arabicPdf.close();

它是从iText 网站逐字复制的。 https://itextpdf.com/itext7/pdfcalligraph

【讨论】: