Java 无法在PDF上正确解析印地语字符串
我试图添加印地语文本使用曼加尔字体的PDF 问题:印地语字符串的某些字符未正确解析。其中大部分由“choti e matra”、“r matra”和带有“halanth”的字符组成。请提供解决方案Java 无法在PDF上正确解析印地语字符串,java,pdf,itext,Java,Pdf,Itext,我试图添加印地语文本使用曼加尔字体的PDF 问题:印地语字符串的某些字符未正确解析。其中大部分由“choti e matra”、“r matra”和带有“halanth”的字符组成。请提供解决方案 public static void main(String[] args) throws FileNotFoundException, DocumentException { Document document = new Document(); PdfWriter
public static void main(String[] args) throws FileNotFoundException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
FontFactory.register("C:\\Users\\Downloads\\Mangal Regular\\Mangal Regular.ttf");
Font f1 =FontFactory.getFont("Mangal", BaseFont.IDENTITY_H, true);
String str="रिन्यूअल सूचना"; //sample text
Phrase p = new Phrase(str,f1);
document.add(p);
document.close();
}
注意:复制并粘贴到word时在PDF上生成的文本已正确解析。
附加了执行上述代码后生成的PDF的屏幕截图
尝试下面的代码
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
public class HindiExample {
public static final String DEST = "results/fonts/hindi.pdf";
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new HindiExample().createPdf(DEST);
}
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, BaseFont.EMBEDDED);
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);
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);
document.close();
}
}
iText 5不完全支持印地语文本的呈现。如果要使用iText,则必须将iText 7与pdfCalligraph附加组件一起使用
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("hindi.pdf"));
Document doc = new Document(pdfDoc);
PdfFont f = PdfFontFactory.createFont("FreeSans.ttf", PdfEncodings.IDENTITY_H);
String str = "रिन्यूअल सूचना";
Paragraph p1 = new Paragraph(str);
p1.setFont(f);
doc.add(p1);
doc.close();
不带pdfCalligraph的输出:
pdfCalligraph的输出:
提供更多背景信息和自动选择字体的方法,而不是在内容元素上显式设置字体:
final PdfWriter writer = new PdfWriter("languages.pdf");
final PdfDocument pdfDocument = new PdfDocument(writer);
final Document document = new Document(pdfDocument);
final FontSet set = new FontSet();
set.addFont("fonts/NotoNaskhArabic-Regular.ttf");
set.addFont("fonts/NotoSansTamil-Regular.ttf");
set.addFont("fonts/FreeSans.ttf");
document.setFontProvider(new FontProvider(set));
对于Itext5和印地语,我使用了此站点的解决方法() :使用Google字体NOTSANS将文本转换为图像
com.itextpdf.text.Image png = com.itextpdf.text.Image.getInstance(textToImage(TEXT_HINDI,font));
png.scalePercent(50f);
Paragraph p = new Paragraph();
p.add(png)
...
方法“textToImage”
解决方案是将itext7与pdfCalligraph扩展一起使用。正如@Paulosares所说,尝试将itext7与pdfCalligraph扩展一起使用。我使用的是目前正在生产中运行的许可itext5,由于itext7不向后兼容,因此没有时间迁移到itext7。因此需要对itext5本身进行一些修复。我可以在我这边做自定义处理,但我不确定需要做什么才能处理所有区域语言。我使用了许可的itext5,目前正在生产中运行,因为itext7不向后兼容,所以没有时间迁移到itext7。因此需要对itext5本身进行一些修复。我可以在我这边做自定义处理,但我不确定需要做什么才能处理所有区域语言。@rhens,iText7 with
pdfCalligraph
是否已付款?我安装了一个nuget软件包itext7.pdfcalligraph
,但它没有在pdf中显示印地语字体。我有随机字符串,有些是印地语的,有些是英语的。如何区分两者并相应地应用ttf文件?是的,pdfCalligraph
已获得商业许可。如果你想试用,你可以得到一个有时间限制的试用许可证。我的回答涵盖了您关于应用字体的问题:pdfCalligraph
自动选择合适的字体和书写系统。
public static byte[] textToImage(String text, java.awt.Font fnt) throws IOException, FontFormatException {
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
try{
Graphics2D g2d = img.createGraphics();
g2d.setFont(fnt);
FontMetrics fm = g2d.getFontMetrics();
int width = fm.stringWidth(text) ;
int height = fm.getHeight();
g2d.dispose();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
g2d = img.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setFont(fnt);
fm = g2d.getFontMetrics();
g2d.setBackground(Color.WHITE);
g2d.setColor(Color.BLACK);
g2d.drawString(text, 0, fm.getAscent());
g2d.dispose();
}catch(Exception e){
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( img, "png", baos );
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
return imageInByte;
}