2016-12-23 4 views
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가 유효 함을 보장 할 수 있습니다.

도움 주셔서 감사합니다.

답변

0

대신이 같은 문서로드 :

PDDocument firstDocument = new PDDocument(); 
firstDocument.load(files[0]); 

이 작업을 수행 : (이 좋은 경우) 당신은 load 정적 방법이라고 당신의 IDE에 의해 경고를 봤어야

PDDocument firstDocument = PDDocument.load(files[0]); 

. 코드가 무슨 짓을

enter image description here

빈 PDDocument 객체의 페이지 수를 표시하는 것이 었습니다.

이 대답은 2.0. *에만 적용됩니다. 1.8. *에서는 PDF가 암호화되어 있지 않으면 작동하지 않을 수 있습니다. 이를 처리하려면 load 대신 loadNonSeq을 사용하십시오. 그러면 해독도됩니다.

+0

우수 감사합니다. – btbam91