Reputation: 125
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.
Upvotes: 0
Views: 2633
Reputation: 9816
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.
Upvotes: 4