2017-03-27 1 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(); 

    } 
} 

je suis en utilisant le code ci-dessus pour PRACTIVE mais mon compilateur ditPDFBox document.save() d'erreur de compilation

exception non déclarée COSVisitorException; doit être capturé ou déclaré à lancer document.save ("mypdf.pdf");

+1

Et votre question est? – Jens

Répondre

0

changement throws IOException-throws IOException, COSVisitorException

1

Comme les messages dit:

Déclarez l'exception dans la méthode en-tête:

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

ou ajouter un bloc 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, n'utilisez pas les anciennes versions si vous êtes débutant. La version 2.0 ne lance plus COSVisitorException. Et utilisez les exemples du téléchargement du code source.