图中拼写错误 我正在使用 Java iText 从 josn 数据生成报告,我面临印地语拼写问题。我使用的谷歌字体是 NotoSansDevanagari-Regular.ttf
//This is the function
private void addTextToPdf(PdfDocument pdfDocument, String text, float x, float y, int pageNumber, int fontSize, PdfFont font) throws IOException {
PdfPage page = pdfDocument.getPage(pageNumber);
PdfCanvas canvas = new PdfCanvas(page);
// Convert text to lowercase for case-insensitive comparison
String lowerText = text.toLowerCase();
// Determine the color based on the text value
DeviceRgb rectangleColor = null;
String marathiText = text; // Default to the original text
// Draw the rectangle with the determined color
if (rectangleColor != null) {
drawRectangle(pdfDocument, x-2f, y - 2.8f, 85.5f, 12f, rectangleColor);
}
// Add the Marathi text to the PDF
canvas.beginText()
.setFontAndSize(font, fontSize)
.setColor(ColorConstants.BLACK, true)
.moveText(x, y)
.showText(marathiText)
.endText();
}
“वर्धा”是我传递的字符串,图像中的字符串被打印,但是在处理pdf时“वर्धा”被复制
要正确渲染印地语,您需要使用
com.itextpdf.layout.Canvas
模块中的 layout
,而不是内核模块中的 com.itextpdf.kernel.pdf.canvas.PdfCanvas
。因为 layout
负责 PDF 的高级工作(在我们的例子中是脚本布局)。
此外,为了能够应用印地语,您需要使用 iText 的 pdfCalligraph 产品,请参阅 https://itextpdf.com/products/pdfcalligraph。
有了这 2 个时刻,您的
addTextToPdf
代码应该如下所示:
//This is the function
private void addTextToPdf(PdfDocument pdfDocument, String text, float x, float y, int pageNumber, int fontSize, PdfFont font) throws IOException {
PdfPage page = pdfDocument.getPage(pageNumber);
Canvas canvas = new Canvas(page, pdfDocument.getDefaultPageSize());
// Convert text to lowercase for case-insensitive comparison
String lowerText = text.toLowerCase();
// Determine the color based on the text value
DeviceRgb rectangleColor = null;
String marathiText = text; // Default to the original text
// Draw the rectangle with the determined color
if (rectangleColor != null) {
drawRectangle(pdfDocument, x-2f, y - 2.8f, 85.5f, 12f, rectangleColor);
}
// Add the Marathi text to the PDF
canvas.showTextAligned(
new Paragraph(text)
.setFont(font)
.setFontSize(fontSize)
.setFontColor(ColorConstants.BLACK),
x, y, TextAlignment.LEFT);
canvas.close();
}