2017-03-27 73 views
-1
package document; 

import java.io.IOException; 

import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage; 

public class Adding_Pages { 

    public static void main(String args[]) throws IOException { 

     //Creating PDF document object 
     PDDocument document = new PDDocument(); 

     for (int i=0; i<10; i++) { 
     //Creating a blank page 
     PDPage blankPage = new PDPage(); 

     //Adding the blank page to the document 
     document.addPage(blankPage); 
     } 

     //Saving the document 
     document.save("C:/PdfBox_Examples/my_doc.pdf"); 
     System.out.println("PDF created"); 

     //Closing the document 
     document.close(); 

    } 
} 

我用上面的代碼爲實踐的研究,但我的編譯器說PDFBOX document.save

未報告的異常COSVisitorException()編譯錯誤;必須被捕獲或宣佈 被拋出document.save(「mypdf.pdf」);

+1

而你的問題是? – Jens

回答

0

變化throws IOExceptionthrows IOException, COSVisitorException

1

作爲消息說:

聲明的方法中部首除外:

public static void main(String args[]) throws IOException , COSVisitorException{ 

或添加try/catch

try { 
//Creating PDF document object 
     PDDocument document = new PDDocument(); 

     for (int i=0; i<10; i++) { 
     //Creating a blank page 
     PDPage blankPage = new PDPage(); 

     //Adding the blank page to the document 
     document.addPage(blankPage); 
     } 

     //Saving the document 
     document.save("C:/PdfBox_Examples/my_doc.pdf"); 
     System.out.println("PDF created"); 

     //Closing the document 
     document.close(); 

} catch (COSVisitorException e) { 
//handle exception 
} 
0

Download PDFBox 2.0,如果您是初學者,請不要使用舊版本。 2.0版本不再拋出COSVisitorException。並使用源代碼下載的例子。