繁体   English   中英

如何在Java中使用itext在pdf中使用i18n

[英]how to use i18n in pdf using itext in java

我正在使用印地语语言以pdf格式显示表格的标题内容,但在表格的标题部分显示了空单元格。

我创建如下:

HttpServletResponse response = ServletActionContext.getResponse();
Document document = new Document();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, buffer);
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));           
document.open();
PdfPTable lineItemTable = new PdfPTable(cellval);
lineItemTable.setHeaderRows(1);                
PdfPCell num1 = new PdfPCell(new Phrase("No",Bold_NORMAL)); 
num1.setHorizontalAlignment(Element.ALIGN_CENTER);
num1.setBackgroundColor(BaseColor.LIGHT_GRAY);
lineItemTable.addCell(num1); 
PdfPCell lineval = new PdfPCell(new Phrase(""));
lineval = new PdfPCell(new Phrase(HindiItenName,Bold_NORMAL));
lineval.setHorizontalAlignment(Element.ALIGN_CENTER);
lineval.setBackgroundColor(BaseColor.LIGHT_GRAY);
lineItemTable.addCell(lineval);
document.add(lineItemTable);
document.close();            
response.setContentType("application/pdf;charset=ISO-8859-1");
response.setHeader("Content-Disposition","attachment;filename=Invoice-"+invNumber+".pdf");
response.getOutputStream().write(buffer.toByteArray());

这是我的示例代码。 你能帮我吗 提前致谢。

呼叫

writer.close();
document.close();
response.setContentType("application/pdf");

关闭作者回合文档中的某些结构。 但是我看到了很多没有完成此操作的示例。 所以我不确定。

内容类型是二进制的,不需要编码。

关于错误:选择Unicode字体IDENTITY_H:

final String FONT = "c:/windows/... .ttf";
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
document.add(new Paragraph("Font: " + bf.getPostscriptFontName()
                + " with encoding: " + bf.getEncoding()));

这里是嵌入式字体,因此在接收方不会有任何问题。 这是摘自itextpdf.com

为了获得更好的浏览器体验:

byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM