我有以下一段用于在 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\"
它抛出了一个异常。 我正在使用 play 框架,它抛出的异常是“InvocableTargetException 发生:null”
最佳答案
到目前为止,您使用 PdfPTable 创建表格更好。创建单元格更简单,就像您逐个单元格添加单元格一样(每个单元格都是 PdfPCell)。现在关于您的两个问题
- 使用特定字体来显示希腊字符。当您将其嵌入为 unicode 时,最好使用 Arialuni.ttf 进行尝试。然后,如果您找到特定字体,请使用该字体。
- 将图像添加到单元格也很简单,只需使用 PdfPCell 的 add 方法即可。创建字体来实现,您必须使用 BaseFont 类创建字体。(只需谷歌搜索每个部分,以便更好地理解。)导出的 pdf 不显示这些希腊字符,因为它找不到代码点(Unicode)表示的字形。
如果您可以附上预期输出的图像,我们可以提供一些见解。
关于java - 使用 iText 和 htmlWorker 的希腊字符和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10675255/