1

I am using PDFBox 1.8.13 and found link extract images from pdf using pdfbox for extracting all images in page. I want to modify the image and place the modified image where it was located. The code snippet :

  Map<String,PDXObject> map = page.getResources().getXObjects();
for(String key : map.keySet())
{
    byte[] b = map.get(key).getPDStream().getByteArray();

    PDMetadata data =map.get(key).getMetadata();
    if(b.length >=2 * 1024)
    {
       //get the image bytes and modify byte array as per required
        //Place the modified image bytes into the location where
         //  the original image was (Please help with the code)
    }
}

Please help.

Regards and Thanks

Kris

chiku
  • 443
  • 1
  • 5
  • 17
  • What kind of modifications do you want to make? In particular, will the eventual dimensions of the image change where it is drawn on a page? – mkl Aug 27 '17 at 10:20
  • That been said, you are aware that the code you seem to use as a template only looks for images in page resources, not in resources of xobjects or patterns used on that page let alone inline images? – mkl Aug 27 '17 at 10:27
  • modifications is only in terms of a text embedded into image nothing else, not the image size , Please help me with the code to replace the image with new byte array. Kris – chiku Aug 27 '17 at 15:03
  • 1
    IMHO what should be done is to create the image, then reassign this image to the xobject dictionary in the resource dictionary with the same key. Replacing the bytes could be done too, but this might not be optimal if filters are different. Can you share the PDF? – Tilman Hausherr Aug 28 '17 at 12:28
  • 1
    Just like @Tilman says, usually you will extract the image resource as a file of some appropriate format, edit it using some image processing API, and replace the resource by a new one built from the processed image. As the image position and dimensions don't change, you do not need to edit the content stream. There might be an issue, though, if the image in the PDF uses some interesting color spaces, in that case there might be no export routine to a standard image format yet. – mkl Aug 28 '17 at 13:46

0 Answers0