Java代码如何打开PDF文件呢?
下文笔者讲述使用java代码打开pdf文件的方法分享,如下所示
实现思路:
使用rundll32打开pdf
使用Awt Desktop的方式打开pdf
例:
rundll32 Windows平台解决方案
package com.java265.jdbc;
import java.io.File;
public class WindowsPlatformAppPDF {
public static void main(String[] args) {
try {
if ((new File("d:\\test.pdf")).exists()) {
Process p = Runtime
.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler c:\\Java-Interview.pdf");
p.waitFor();
} else {
System.out.println("File is not exists");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Awt Desktop跨平台打开pdf解决方案
package com.java265.io;
import java.awt.Desktop;
import java.io.File;
//Cross platform solution to view a PDF file
public class AnyPlatformAppPDF {
public static void main(String[] args) {
try {
File pdfFile = new File("d:\\test.pdf");
if (pdffile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
System.out.println("Awt Desktop is not supported!");
}
} else {
System.out.println("File is not exists!");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


