Java iText-字体显示不正确
我正在使用iText和java+struts2 web应用程序生成pdf,我想在浏览器中显示创建的pdf。 这是我用来生成PDF的代码的一部分。我想查看僧伽罗字体的内容。因此,我下载了一个僧伽罗语.ttf 文件并创建了如下字体, 这是正常工作,除了一些字符,如连字没有正确显示。这些字符被移位了 如果有人能在这个问题上帮助我,我将不胜感激Java iText-字体显示不正确,java,struts2,itext,Java,Struts2,Itext,我正在使用iText和java+struts2 web应用程序生成pdf,我想在浏览器中显示创建的pdf。 这是我用来生成PDF的代码的一部分。我想查看僧伽罗字体的内容。因此,我下载了一个僧伽罗语.ttf 文件并创建了如下字体, 这是正常工作,除了一些字符,如连字没有正确显示。这些字符被移位了 如果有人能在这个问题上帮助我,我将不胜感激 import com.lowagie.text.Document; import com.lowagie.text.DocumentException; imp
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Phrase;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.FontSelector;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;
public class GeneratePDFAction {
public String execute() throws DocumentException, IOException {
final HttpSession session = createSession();
Document document = new Document();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance(document, buffer);
document.open();
FontSelector selector = new FontSelector();
BaseFont bf2 = BaseFont.createFont("C:/Projects/FM-MalithiUW46.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
fontbold = new Font(bf2, 12);
selector.addFont(fontbold);
PdfPCell headerPerInfo = new PdfPCell();
final String s = "\u0DB4\u0DDE\u0DAF\u0DCA\u0D9C\u0DBD\u0DD2\u0D9A \u0DAD\u0DDC\u0DBB\u0DAD\u0DD4\u0DBB\u0DD4";
Phrase phrase2 = selector.process(s);
headerPerInfo.addElement(phrase);
PdfPTable table = new PdfPTable(1);
table.addCell(headerPerInfo);
document.add(table);
document.close();
inputStream = new ByteArrayInputStream(buffer.toByteArray());
fileName = "CV" + email + ".pdf";
session.setAttribute("fileName", fileName);
return SUCCESS;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(final InputStream inputStreamVal) {
this.inputStream = inputStreamVal;
}
public String getFileName() {
return fileName;
}
public void setFileName(final String fileNameVal) {
this.fileName = fileNameVal;
}
}
struts.xml
<action name="generatepdf" class="myApps.org.GeneratePDFAction">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">inline;fileName=${fileName}/param>
<param name="bufferSize">1024</param>
</result>
导入com.lowagie.text.Document;
导入com.lowagie.text.DocumentException;
导入com.lowagie.text.Font;
导入com.lowagie.text.FontFactory;
导入com.lowagie.text.Phrase;
导入com.lowagie.text.paragration;
导入com.lowagie.text.pdf.FontSelector;
导入com.lowagie.text.pdf.PdfPCell;
导入com.lowagie.text.pdf.PdfPTable;
导入com.lowagie.text.pdf.PdfWriter;
导入com.lowagie.text.pdf.BaseFont;
公共类GeneratePDAction{
公共字符串execute()引发DocumentException,IOException{
最终HttpSession会话=createSession();
文档=新文档();
ByteArrayOutputStream缓冲区=新建ByteArrayOutputStream();
getInstance(文档,缓冲区);
document.open();
FontSelector=新FontSelector();
BaseFont bf2=BaseFont.createFont(“C:/Projects/FM-MalithiUW46.ttf”,BaseFont.IDENTITY\u H,BaseFont.EMBEDDED);
fontbold=新字体(bf2,12);
选择器.addFont(字体粗体);
PdfPCell headerPerInfo=新的PdfPCell();
最后一个字符串s=“\u0DB4\u0DDE\u0DAF\u0DCA\u0D9C\u0DBD\u0DD2\u0D9A\u0DAD\u0DDC\u0DBB\u0DAD\u0DD4\u0DBB\u0DD4”;
短语短语2=选择器进程;
标题信息补遗(短语);
PdfPTable table=新的PdfPTable(1);
表2.addCell(headerPerInfo);
文件。添加(表);
document.close();
inputStream=new ByteArrayInputStream(buffer.toByteArray());
fileName=“CV”+电子邮件+”.pdf”;
setAttribute(“文件名”,文件名);
回归成功;
}
公共输入流getInputStream(){
返回输入流;
}
public void setInputStream(最终InputStream inputStreamVal){
this.inputStream=inputStreamVal;
}
公共字符串getFileName(){
返回文件名;
}
public void setFileName(最终字符串fileNameVal){
this.fileName=fileNameVal;
}
}
struts.xml
申请表格/pdf
输入流
内联;fileName=${fileName}/param>
1024
您使用的是一个过时版本的iText,它永远不支持连字。支持连字是最新版本iText的路线图,但尚未实施。我认为这应该是答案,而不是评论……明白了:)感谢Lowagie先生的回复。