有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java iText字体显示不正确

我正在使用iText和java+struts2 web应用程序生成pdf,我想在浏览器中显示创建的pdf。 这是我用来生成PDF的代码的一部分。我想查看僧伽罗字体的内容。因此我下载了一本僧伽罗经。ttf 文件并创建了如下字体, 这是正常工作,除了一些字符,如连字没有正确显示。这些字符被移位了

如果有人能在这个问题上帮助我,我将不胜感激

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>  

共 (0) 个答案