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

时间:2014-08-01 07:54:03

标签: java itext

我使用印地语在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());

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

1 个答案:

答案 0 :(得分:0)

呼叫

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);