我正在使用iText与我的java + struts2 Web应用程序生成pdf,我想在浏览器中显示创建的PDF。 这是我用于生成PDF的代码的一部分。我想以Sinhala字体查看内容。因此我下载了一个Sinhala .ttf 文件并创建如下字体, 这是正常工作,除了一些字符,如Ligatures没有正确显示。那些角色被转移了。
感谢是否有人可以帮我解决这个问题。
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>