2016-10-03 1 views

Répondre

2

Vous pouvez regarder le pdfbox examples directory in the Apache SVN repository, en particulier la classe exemple porte bien son nom AddImageToPDF avec cette méthode clé:

public void createPDFFromImage(String inputFile, String imagePath, String outputFile) 
     throws IOException 
{ 
    // the document 
    PDDocument doc = null; 
    try 
    { 
     doc = PDDocument.load(new File(inputFile)); 

     //we will add the image to the first page. 
     PDPage page = doc.getPage(0); 

     // createFromFile is the easiest way with an image file 
     // if you already have the image in a BufferedImage, 
     // call LosslessFactory.createFromImage() instead 
     PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc); 
     PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true); 

     // contentStream.drawImage(ximage, 20, 20); 
     // better method inspired by http://stackoverflow.com/a/22318681/535646 
     // reduce this value if the image is too large 
     float scale = 1f; 
     contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth()*scale, pdImage.getHeight()*scale); 

     contentStream.close(); 
     doc.save(outputFile); 
    } 
    finally 
    { 
     if(doc != null) 
     { 
      doc.close(); 
     } 
    } 
} 
+0

Bonjour, quand je chemin de l'image locale, il travaille bien, quand je mets en ligne le chemin de l'image (http://www.foodnavigator.com/var/plain_site/storage/images/publications/food-beverage-nutrition/foodnavigator.com/science-nutrition/high-cost-of- fruits-et-légumes-liés-à-haut-corps-gros-dans-jeunes-enfants-étude/8800391-3-fra-GB/Coût élevé-de-fruit-et-légumes-lié-à-plus haut body-fat-in-young-enfants-S tudy.jpg) alors il ne montrera pas l'image. S'il vous plaît me suggérer ce que je devrais faire. – Manojkumar

+0

@Manojkumar s'il vous plaît partager le résultat PDF. Si votre code est différent de celui ci-dessus, veuillez créer une nouvelle question. –

+0

String imageUrls = "F: /me.png"; String imageUrls = "www.foodnavigator.com/var/plain_site/storage/images/publications/food-beverage-nutrition/foodnavigator.com/science-nutrition/high-cost-of-fruit-and-vegetables-linked-to -higher-corps-gros-dans-jeune-enfants-étude/8800391-3-fra-GB/Coût élevé-de-fruits-et-légumes-liés-à-haut-corps-gros-chez-jeunes-enfants -Study.jpg "; PDImageXObject pdImage = PDImageXObject.createFromFile (imageUrls, document); contentStream.drawImage (pdImage, margin + 5, texty, 170, 100); – Manojkumar