如何使 Itext 接受具有如下序列的 unicode
How do make Itext to Accept unicode having sequence like below
如何使 Itext 库接受 unicode,如下所示。实际上它使用 java console.But 打印一个印地文字符串 我试图通过 itext 接受它以便制作报告。
u0915\u093e\u0930 \u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917
下面是我正在使用的代码片段
BaseFont unicode = BaseFont.createFont("/home/mani/Downloads/arialuni.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font=new Font(unicode,12,Font.NORMAL,new BaseColor(50,205,50));
table = new PdfPTable(new float[] { 10, 60, 30 });
table.setWidthPercentage(100);
PdfPCell customerLblCell = new PdfPCell(new Phrase("CUSTOMERS",
this.headerFont));
PdfPCell balanceLblCell = new PdfPCell(new Phrase(
"\u0915\u093e\u0930\u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917", font));
但它还是无法识别文本。
预期:-कारपार्किंग
我假设您希望输出看起来像此屏幕截图中的第一行:
此屏幕截图取自文件 hindi.pdf, created using the HindiExample 示例:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, true);
Paragraph p1 = new Paragraph("\u0915\u093e\u0930 "
+ "\u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917", f);
document.add(p1);
Paragraph p2 = new Paragraph("\u0915 \u0915 \u093e \u093e \0930 \u0930\n"
+ "\u092a \u092a \u093e \u093e \u0930 \u0930 \u094d \u094d"
+ "\u0915 \u0915 \u093f \u093f \u093f \u0902 \u0902"
+ "\u0917 \u0917", f);
document.add(p2);
document.close();
}
你的问题不完整,因为你没有告诉我们你使用的是哪种字体(在我的例子中 FONT
是 FreeSans.ttf
),也没有告诉我们哪种编码(应该是IDENTITY_H
).
如果您期待屏幕截图的第一行,我的回答是双重的:
- 使用知道如何呈现印地语的字体(例如 FreeSans.ttf、arialuni.ttf、...),
- 使用正确的编码 (Identity-H)。
或者问题可能更微不足道:您在第一个 u
.
之前忘记了一个 \
字符
如果您期待其他内容, 那么请查看第二行和第三行,我在其中插入了 Unicode 值,并在每个 Unicode 字符前添加了一个 space .您会注意到每个字符都正确呈现,但是没有进行连字。如果您需要连字,则需要等待 iText 的下一个主要版本,它将支持印地语,但该支持不会作为开源软件提供。 (我们注意到有太多开发人员在没有商业许可的情况下使用 iText,因此我们将仅提供一些封闭源代码的组件。)
更新:
您已经更新了您的问题,但是您再一次没有提供允许人们重现问题的SSCCE。当我完成你的代码时,我得到了这个结果:
这是我的代码:
BaseFont unicode = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font=new Font(unicode,12,Font.NORMAL,new BaseColor(50,205,50));
PdfPTable table = new PdfPTable(new float[] { 10, 60, 30 });
table.setWidthPercentage(100);
PdfPCell customerLblCell = new PdfPCell(new Phrase("CUSTOMERS"));
PdfPCell balanceLblCell = new PdfPCell(new Phrase("\u0915\u093e\u0930\u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917", font));
table.addCell(customerLblCell);
table.addCell(balanceLblCell);
table.completeRow();
table.setSpacingBefore(10);
document.add(table);
我也在线更新了示例:HindiExample
您的代码中缺少什么?您创建了一个包含 3 列的 table,并创建了 2 个单元格。我没有看到您在任何地方将单元格添加到 table,我也没有看到任何地方的单元格 3。因此,即使您将这两个单元格添加到 table,它们也不会被渲染,因为 iText 会丢弃不完整的行(除非您使用 completeRow()
方法)。
我使用这种字体:FreeSans.ttf (you can download it here)。我也用 MS Arial Unicode (arialuni.ttf) 尝试过,这给了我以下结果:
简而言之:您关于 iText 无法识别 Unicode 的指控是错误的。有大量示例证明 Unicode 正确呈现(使用 FreeSans 和 MS Arial Unicode)。
如何使 Itext 库接受 unicode,如下所示。实际上它使用 java console.But 打印一个印地文字符串 我试图通过 itext 接受它以便制作报告。
u0915\u093e\u0930 \u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917
下面是我正在使用的代码片段
BaseFont unicode = BaseFont.createFont("/home/mani/Downloads/arialuni.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font=new Font(unicode,12,Font.NORMAL,new BaseColor(50,205,50));
table = new PdfPTable(new float[] { 10, 60, 30 });
table.setWidthPercentage(100);
PdfPCell customerLblCell = new PdfPCell(new Phrase("CUSTOMERS",
this.headerFont));
PdfPCell balanceLblCell = new PdfPCell(new Phrase(
"\u0915\u093e\u0930\u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917", font));
但它还是无法识别文本。 预期:-कारपार्किंग
我假设您希望输出看起来像此屏幕截图中的第一行:
此屏幕截图取自文件 hindi.pdf, created using the HindiExample 示例:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, true);
Paragraph p1 = new Paragraph("\u0915\u093e\u0930 "
+ "\u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917", f);
document.add(p1);
Paragraph p2 = new Paragraph("\u0915 \u0915 \u093e \u093e \0930 \u0930\n"
+ "\u092a \u092a \u093e \u093e \u0930 \u0930 \u094d \u094d"
+ "\u0915 \u0915 \u093f \u093f \u093f \u0902 \u0902"
+ "\u0917 \u0917", f);
document.add(p2);
document.close();
}
你的问题不完整,因为你没有告诉我们你使用的是哪种字体(在我的例子中 FONT
是 FreeSans.ttf
),也没有告诉我们哪种编码(应该是IDENTITY_H
).
如果您期待屏幕截图的第一行,我的回答是双重的:
- 使用知道如何呈现印地语的字体(例如 FreeSans.ttf、arialuni.ttf、...),
- 使用正确的编码 (Identity-H)。
或者问题可能更微不足道:您在第一个 u
.
\
字符
如果您期待其他内容, 那么请查看第二行和第三行,我在其中插入了 Unicode 值,并在每个 Unicode 字符前添加了一个 space .您会注意到每个字符都正确呈现,但是没有进行连字。如果您需要连字,则需要等待 iText 的下一个主要版本,它将支持印地语,但该支持不会作为开源软件提供。 (我们注意到有太多开发人员在没有商业许可的情况下使用 iText,因此我们将仅提供一些封闭源代码的组件。)
更新:
您已经更新了您的问题,但是您再一次没有提供允许人们重现问题的SSCCE。当我完成你的代码时,我得到了这个结果:
这是我的代码:
BaseFont unicode = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font=new Font(unicode,12,Font.NORMAL,new BaseColor(50,205,50));
PdfPTable table = new PdfPTable(new float[] { 10, 60, 30 });
table.setWidthPercentage(100);
PdfPCell customerLblCell = new PdfPCell(new Phrase("CUSTOMERS"));
PdfPCell balanceLblCell = new PdfPCell(new Phrase("\u0915\u093e\u0930\u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917", font));
table.addCell(customerLblCell);
table.addCell(balanceLblCell);
table.completeRow();
table.setSpacingBefore(10);
document.add(table);
我也在线更新了示例:HindiExample
您的代码中缺少什么?您创建了一个包含 3 列的 table,并创建了 2 个单元格。我没有看到您在任何地方将单元格添加到 table,我也没有看到任何地方的单元格 3。因此,即使您将这两个单元格添加到 table,它们也不会被渲染,因为 iText 会丢弃不完整的行(除非您使用 completeRow()
方法)。
我使用这种字体:FreeSans.ttf (you can download it here)。我也用 MS Arial Unicode (arialuni.ttf) 尝试过,这给了我以下结果:
简而言之:您关于 iText 无法识别 Unicode 的指控是错误的。有大量示例证明 Unicode 正确呈现(使用 FreeSans 和 MS Arial Unicode)。