【问题标题】:Greek characters and images using iText and htmlWorker使用 iText 和 htmlWorker 的希腊字符和图像
【发布时间】:2012-05-20 16:40:31
【问题描述】:

我在 Web 应用程序中有以下用于 PDF 导出的代码,该应用程序创建了一个带有 2x2 表格单元格的 PDF。

public static void myexport2() throws IOException{
    try {
        com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("C://Users/gkarapatis/Desktop/testpdf.pdf"));
        document.open();
        document.addAuthor("Author of the Doc");
        document.addCreator("Creator of the Doc");
        document.addSubject("Subject of the Doc");
        document.addCreationDate();
        document.addTitle("This is the title");
        HTMLWorker htmlWorker = new HTMLWorker(document);
        String str="<table height=300, border=\"1\"><tr><td></td><td></td></tr><tr><td>1</td><td></td></tr></table>"     

        htmlWorker.parse(new StringReader(str));
        document.close();
    } catch(DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

当我将希腊字符添加到 HTML 单元格时,它不会出现在导出的 PDF 文件中。我应该怎么做才能将 Unicode 添加到我的导出中。 是 HTMLWorker 的问题吗?

当我尝试将图像添加到添加

的单元格时
img src=\"file://C:/Webapp/dsp/public/images/dsp1.jpg\" width=\"80\" height=\"81\" alt=\"\" border=\"0\"

它给我抛出了一个异常。 我正在使用播放框架,它抛出的异常是“InvocationTargetException发生:null”

【问题讨论】:

  • 这可能与您使用的字体有关吗?您是否尝试过指定具有希腊字符的字体?
  • 我正在使用播放框架,它抛出的异常是 InvocationTargetException 发生:null
  • 我试图指定一个 unicode 喜欢但可能不正确...有人可以帮忙吗?

标签: java playframework itext


【解决方案1】:

到目前为止,您最好使用 PdfPTable 创建表格。创建单元格更简单,就像您逐个单元格添加单元格(每个单元格都是 PdfPCell)。现在关于您的两个问题

  1. 使用特定字体显示希腊字符。由于您将其嵌入为 unicode,因此最好使用 Arialuni.ttf 进行尝试。如果您发现特定字体,请使用它。
  2. 将图像添加到单元格也很简单,只需使用 PdfPCell 的 add 方法。创建字体用于实现,您必须使用 BaseFont 类创建字体。(只需谷歌搜索每个部分,以便更好地理解。)导出的 pdf 不显示那些希腊字符,因为它找不到由代码点(Unicode)表示的字形。

如果您可以附上预期输出的图片,我们可以提供一些见解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2018-03-12
    • 2016-04-02
    • 2013-09-30
    相关资源
    最近更新 更多