这是我花在调查上的第二天没有结果.至少现在,我能够提出非常具体的问题.
我想写包含在PDF文件中使用一些非拉丁字符有效的HTML代码的iText和更具体的使用ITextRenderer从飞碟.
我的简短示例/代码首先使用以下值初始化字符串变量doc:
String doc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\">"
+ "<body>Some greek characters: ???????? Some greek characters"
+ "</body></html>";
Run Code Online (Sandbox Code Playgroud)
这是我用于调试目的的代码.我将此字符串保存为HTML文件然后我通过浏览器打开它只是为了仔细检查HTML内容是否有效,我仍然可以读取希腊字符:
//write for debugging purposes in an html file
File newTextFile = new File("C:/work/test.html");
FileWriter fw = new FileWriter(newTextFile);
fw.write(doc);
fw.close();
Run Code Online (Sandbox Code Playgroud)
下一步是尝试在PDF文件中写入此值.这是我的代码:
ITextRenderer renderer = new ITextRenderer();
//add some fonts - if paths are not right, an exception will be thrown
renderer.getFontResolver().addFont("c:/work/fonts/TIMES.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("c:/work/fonts/TIMESBD.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("c:/work/fonts/TIMESBI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("c:/work/fonts/TIMESI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); …Run Code Online (Sandbox Code Playgroud)