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

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

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

问题

  1. import java.awt.image.BufferedImage;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. import javax.imageio.ImageIO;
  7. import org.apache.commons.codec.binary.Base64;
  8. import org.apache.pdfbox.pdmodel.PDDocument;
  9. import org.apache.pdfbox.pdmodel.PDPage;
  10. import org.apache.pdfbox.pdmodel.PDPageContentStream;
  11. import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
  12. public class One {
  13. public static void main(String[] args) throws IOException {
  14. Scanner s = new Scanner(System.in);
  15. System.out.println("Enter base64 string to be converted to an image");
  16. String base64 = s.nextLine();
  17. byte[] base64Val = convertToImg(base64);
  18. writeByteToImageFile(base64Val, "image.png");
  19. System.out.println("Saved the base64 as an image in the current directory with the name image.png");
  20. addImageToPDF();
  21. }
  22. public static byte[] convertToImg(String base64) throws IOException {
  23. return Base64.decodeBase64(base64);
  24. }
  25. public static void writeByteToImageFile(byte[] imgBytes, String imgFileName) throws IOException {
  26. File imgFile = new File(imgFileName);
  27. BufferedImage img = ImageIO.read(new ByteArrayInputStream(imgBytes));
  28. ImageIO.write(img, "png", imgFile);
  29. }
  30. public static void addImageToPDF() throws IOException {
  31. File file = new File("C:\\Users\\user\\Downloads\\Risk Template(RiskTemplate).pdf");
  32. PDDocument doc = PDDocument.load(file);
  33. PDPage page = doc.getPage(0);
  34. PDImageXObject pdImage = PDImageXObject.createFromFile("D:\\Development\\Workspace\\1\\image.png", doc);
  35. PDPageContentStream contents = new PDPageContentStream(doc, page);
  36. contents.drawImage(pdImage, 5, 5);
  37. System.out.println("Image inserted");
  38. contents.close();
  39. doc.save("D:\\Development\\Workspace\\1\\InsertImage_OP.pdf");
  40. doc.close();
  41. }
  42. }
英文:
  1. import java.awt.image.BufferedImage;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. import javax.imageio.ImageIO;
  7. import org.apache.commons.codec.binary.Base64;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.net.MalformedURLException;
  12. import org.apache.pdfbox.pdmodel.PDDocument;
  13. import org.apache.pdfbox.pdmodel.PDPage;
  14. import org.apache.pdfbox.pdmodel.PDPageContentStream;
  15. import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
  16. public class One {
  17. /**
  18. * @param args
  19. */
  20. public static void main(String[] args) throws IOException {
  21. // TODO Auto-generated method stub
  22. Scanner s=new Scanner(System.in);
  23. System.out.println("Enter base64 string to be converted to image");
  24. String base64=s.nextLine();
  25. byte[] base64Val=convertToImg(base64);
  26. writeByteToImageFile(base64Val, "image.png");
  27. System.out.println("Saved the base64 as image in current directory with name image.png");
  28. addImageToPDF();
  29. }
  30. public static byte[] convertToImg(String base64) throws IOException
  31. {
  32. return Base64.decodeBase64(base64);
  33. }
  34. public static void writeByteToImageFile(byte[] imgBytes, String imgFileName) throws IOException
  35. {
  36. File imgFile = new File(imgFileName);
  37. BufferedImage img = ImageIO.read(new ByteArrayInputStream(imgBytes));
  38. ImageIO.write(img, "png", imgFile);
  39. }
  40. public static void addImageToPDF() throws IOException {
  41. File file = new File("C:\\Users\\user\\Downloads\\Risk Template(RiskTemplate).pdf");
  42. PDDocument doc = PDDocument.load(file);
  43. PDPage page = doc.getPage(0);
  44. PDImageXObject pdImage = PDImageXObject.createFromFile("D:\\Development\\Workspace\\1\\image.png", doc);
  45. PDPageContentStream contents = new PDPageContentStream(doc, page);
  46. contents.drawImage(pdImage, 5, 5);
  47. System.out.println("Image inserted");
  48. contents.close();
  49. doc.save("D:\\Development\\Workspace\\1\\InsertImage_OP.pdf");
  50. doc.close();
  51. }
  52. }

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

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

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);

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

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:

确定