1
我無法弄清爲什麼我的文件沒有加載到PDDocument對象中。未加載Java pdfbox文件
我的過程如下:
- 打開目錄的文件
- 獲取文件從目錄
- 加載一個文件數組到PDDocument。
查看下面的代碼。
public class Main {
public static void main(String[] args) throws IOException {
//open directory
File folder = new File("pdfs");
//Extract Files
File[] files = folder.listFiles();
//print out file names
for (File file:files) {
System.out.println(file.getName());
System.out.println("Can read?: " + file.canRead());
System.out.println("Can write?: " + file.canWrite());
}
//Load PDF
PDDocument firstDocument = new PDDocument();
try {
firstDocument.load(files[0]);
}
finally
{
if (firstDocument != null) {
firstDocument.close();
}
}
System.out.println("Num Pages: " + firstDocument.getNumberOfPages());
輸出:
EnterpriseArchitectInvoice.pdf
Can read?: true
Can write?: true
ooad_textbooks_invoice.pdf
Can read?: true
Can write?: true
Num Pages: 0
我可以保證的是,PDF是有效的。
感謝您的幫助!
非常好,謝謝! – btbam91