通过pdfbox向pdf中添加图像,但在添加后pdf变成空白的。

huangapple go评论78阅读模式
英文:

Adding an image to a pdf via pdfbox but after adding the pdf get blank

问题

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

public class One {
    public static void main(String[] args) throws IOException {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter base64 string to be converted to an image");
        String base64 = s.nextLine();
        byte[] base64Val = convertToImg(base64);
        writeByteToImageFile(base64Val, "image.png");
        System.out.println("Saved the base64 as an image in the current directory with the name image.png");
        addImageToPDF();
    }

    public static byte[] convertToImg(String base64) throws IOException {
        return Base64.decodeBase64(base64);
    }

    public static void writeByteToImageFile(byte[] imgBytes, String imgFileName) throws IOException {
        File imgFile = new File(imgFileName);
        BufferedImage img = ImageIO.read(new ByteArrayInputStream(imgBytes));
        ImageIO.write(img, "png", imgFile);
    }

    public static void addImageToPDF() throws IOException {
        File file = new File("C:\\Users\\user\\Downloads\\Risk Template(RiskTemplate).pdf");
        PDDocument doc = PDDocument.load(file);
        PDPage page = doc.getPage(0);
        PDImageXObject pdImage = PDImageXObject.createFromFile("D:\\Development\\Workspace\\1\\image.png", doc);
        PDPageContentStream contents = new PDPageContentStream(doc, page);
        contents.drawImage(pdImage, 5, 5);
        System.out.println("Image inserted");
        contents.close();
        doc.save("D:\\Development\\Workspace\\1\\InsertImage_OP.pdf");
        doc.close();
    }
}
英文:
 import java.awt.image.BufferedImage;  
 import java.io.ByteArrayInputStream;  
 import java.io.File;  
 import java.io.IOException;  
 import java.util.Scanner;  
 import javax.imageio.ImageIO;  
 import org.apache.commons.codec.binary.Base64;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import org.apache.pdfbox.pdmodel.PDDocument; 
 import org.apache.pdfbox.pdmodel.PDPage; 
 import org.apache.pdfbox.pdmodel.PDPageContentStream; 
 import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

 public class One {
      /**  
       * @param args  
       */  
      public static void main(String[] args) throws IOException {
           // TODO Auto-generated method stub  
           Scanner s=new Scanner(System.in);  
           System.out.println("Enter base64 string to be converted to image");  
           String base64=s.nextLine();  
           byte[] base64Val=convertToImg(base64);
           writeByteToImageFile(base64Val, "image.png");
           System.out.println("Saved the base64 as image in current directory with name image.png");
           addImageToPDF();
      }
      public static byte[] convertToImg(String base64) throws IOException  
      {  
           return Base64.decodeBase64(base64);  
      }  
      public static void writeByteToImageFile(byte[] imgBytes, String imgFileName) throws IOException  
      {  
           File imgFile = new File(imgFileName);
           BufferedImage img = ImageIO.read(new ByteArrayInputStream(imgBytes));
           ImageIO.write(img, "png", imgFile);
      }
      
      public static void addImageToPDF() throws IOException {
          File file = new File("C:\\Users\\user\\Downloads\\Risk Template(RiskTemplate).pdf"); 
          PDDocument doc = PDDocument.load(file);  
          PDPage page = doc.getPage(0);
          PDImageXObject pdImage = PDImageXObject.createFromFile("D:\\Development\\Workspace\\1\\image.png", doc); 
          PDPageContentStream contents = new PDPageContentStream(doc, page);
          contents.drawImage(pdImage, 5, 5);     
          System.out.println("Image inserted");  
          contents.close();       
          doc.save("D:\\Development\\Workspace\\1\\InsertImage_OP.pdf"); 

          doc.close();
      }
 }

I am creating an image from base64 string and then trying to attach that image to a pdf. The image is creating successfully and the image is being added to the pdf as well but the pdf (output) contains just the image at a corner and the content of the original pdf is now blank.

答案1

得分: 4

public static void addImageToPDF() throws IOException {
    File file = new File("C:\\Users\\user\\Downloads\\Risk Template(RiskTemplate).pdf"); 
    PDDocument doc = PDDocument.load(file);  
    PDPage page = doc.getPage(0);
    PDImageXObject pdImage = PDImageXObject.createFromFile("D:\\Development\\Workspace\\1\\image.png", doc); 
    PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
    contents.drawImage(pdImage, 5, 5);     
    System.out.println("Image inserted");  
    contents.close();       
    doc.save("D:\\Development\\Workspace\\1\\InsertImage_OP.pdf"); 
    doc.close();
}

Disclaimer: This answer was given by MKL/Tilman but for further reference an answer is more "visible" than a comment.

英文:

As MKL & Tilman have written in their comment you have to use

PDPageContentStream(document, page, AppendMode.APPEND, true, true);

public static void addImageToPDF() throws IOException {
File file = new File("C:\\Users\\user\\Downloads\\Risk Template(RiskTemplate).pdf"); 
PDDocument doc = PDDocument.load(file);  
PDPage page = doc.getPage(0);
PDImageXObject pdImage = PDImageXObject.createFromFile("D:\\Development\\Workspace\\1\\image.png", doc); 
PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
contents.drawImage(pdImage, 5, 5);     
System.out.println("Image inserted");  
contents.close();       
doc.save("D:\\Development\\Workspace\\1\\InsertImage_OP.pdf"); 
doc.close();
}

Disclaimer: This answer was given by MKL/Tilman but for further reference an answer is more "visible" than a comment.

huangapple
  • 本文由 发表于 2020年8月16日 19:35:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63436366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定