Buh*_*ndi 2 java pdf rtf itext
向我转发了一项要求,将动态 RTF 文档转换为 PDF,在转换之前填充 RTF 中的所有属性。
遵循此博客文章中的确切示例,我NullPointerException在运行应用程序时遇到了一个。
注意:我知道 iText 上的 RTF 文档支持已被放弃,但它已被客户端使用。
确切的堆栈跟踪:
Exception in thread "main" java.lang.NullPointerException
at com.lowagie.text.rtf.parser.destinations.RtfDestinationFontTable.importSystemFonts(RtfDestinationFontTable.java:571)
at com.lowagie.text.rtf.parser.destinations.RtfDestinationFontTable.init(RtfDestinationFontTable.java:206)
at com.lowagie.text.rtf.parser.destinations.RtfDestinationFontTable.setParser(RtfDestinationFontTable.java:190)
at com.lowagie.text.rtf.parser.destinations.RtfDestinationMgr.addDestination(RtfDestinationMgr.java:184)
at com.lowagie.text.rtf.parser.ctrlwords.RtfCtrlWordHandler.<init>(RtfCtrlWordHandler.java:175)
at com.lowagie.text.rtf.parser.ctrlwords.RtfCtrlWordMap.<init>(RtfCtrlWordMap.java:607)
at com.lowagie.text.rtf.parser.ctrlwords.RtfCtrlWordMgr.<init>(RtfCtrlWordMgr.java:93)
at com.lowagie.text.rtf.parser.RtfParser.init(RtfParser.java:655)
at com.lowagie.text.rtf.parser.RtfParser.convertRtfDocument(RtfParser.java:551)
at za.co.sindi.utils.Prints.printToPDFWithIText(Prints.java:114)
at za.co.sindi.utils.Prints.main(Prints.java:150)
Run Code Online (Sandbox Code Playgroud)
源码示例:
public static void printToPDFWithIText() {
InputStream input = null;
OutputStream output = null;
Document document = null;
try {
input = new BufferedInputStream(new FileInputStream(new File("C:/testPDF.rtf")));
output = new BufferedOutputStream(new FileOutputStream(new File("C:/testPDF_" + System.nanoTime() + ".pdf")));
document = new Document();
PdfWriter.getInstance(document, output);
document.open();
RtfParser parser = new RtfParser(null);
parser.convertRtfDocument(input, document); //NullPointerException is here (line 114)
document.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// if (document != null && document.isOpen()) {
// document.close();
// }
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
罪魁祸首是com.lowagie.text.rtf.parser.destinations.RtfDestinationFontTable类上的以下实现:
private Properties getEnvironmentVariables() throws Throwable {
Properties environmentVariables = new Properties();
String operatingSystem = System.getProperty("os.name").toLowerCase();
Runtime runtime = Runtime.getRuntime();
Process process = null;
if (operatingSystem.indexOf("windows 95") > -1
|| operatingSystem.indexOf("windows 98") > -1
|| operatingSystem.indexOf("me") > -1) {
process = runtime.exec("command.com /c set");
} else if ((operatingSystem.indexOf("nt") > -1)
|| (operatingSystem.indexOf("windows 2000") > -1)
|| (operatingSystem.indexOf("windows xp") > -1)
|| (operatingSystem.indexOf("windows 2003") > -1)
|| (operatingSystem.indexOf("windows vista") > -1)) {
process = runtime.exec("cmd.exe /c set");
} else {
process = runtime.exec("env");
}
BufferedReader environmentStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
String inputLine = "";
int idx = -1;
while ((inputLine = environmentStream.readLine()) != null) {
idx = inputLine.indexOf('=');
inputLine.substring(idx + 1));
}
}
Run Code Online (Sandbox Code Playgroud)
显然,开发人员检查了从 Windows 95 到 Windows Vista 的 Windows 操作系统。他/她忘记检查 Windows 7 和 Windows 8 环境。
该System.getProperty("os.name");回报Windows 7我的开发工作站上,从而Runtime试图调用env过程,并不会在Windows环境中存在。由于这会抛出 a Throwable,因此会发生以下实例:
private void importSystemFonts() {
Properties pr = null;
try {
pr = getEnvironmentVariables();
} catch (Throwable e) {
}
String systemRoot = pr.getProperty("SystemRoot");
Runtime runtime = Runtime.getRuntime();
String fileSeperator = System.getProperty("file.separator");
int r = FontFactory.registerDirectory(systemRoot + fileSeperator + "fonts");
}
Run Code Online (Sandbox Code Playgroud)
如您所见,该pr字段null最初是。现在,有罪方法(已经提到)抛出了一个异常并且importSystemFonts没有做任何处理,尽管它捕获了它。因此pr仍然存在null。该行String systemRoot = pr.getProperty("SystemRoot");现在抛出NullPointerException.
我们如何解决?
在运行您的程序之前,您必须作弊:欺骗库它不是 Windows 7 或更高版本。就我而言,我将其设置为Windows Vista.
这必须在执行代码之前放置:
System.setProperty("os.name", "Windows Vista");
Run Code Online (Sandbox Code Playgroud)
现在,运行您的代码,您将看到异常消失。
注意:看到这个类被放弃了,使用它的风险自负。如果您仍在使用此版本的 iText,我发布此信息是为了帮助其他人。
| 归档时间: |
|
| 查看次数: |
3138 次 |
| 最近记录: |