pdfbox to overwrite image on a pdf
I’ve been trying to make some changes on the size of pdfs.
My first step on my strategy was to extract all images from the pdf and compact them.
I made it successfully.
Now, What I need to do is to replace the image ‘I’ with the compressedImage ‘CI’ using the same graphicstate (coordinates and scale basically), so visually the pdf would be the same(or almost), the difference: with lower quality images.
the thing is that I could not replace the image on the pdfstream.
what I've tried already:
to createOutputStream() on the image and read the stream of the compacted one putting it on the image.
to drawImage() with the contentStream, but it seemed that it printed both images. And besides, I didn’t manage a way to get the graphicstate of the “old” image.
to set a new Image, which obviously would not work, but I had to try, just in case.
Now What I’m trying to do:
Pass the dictionary tree for a new PDDocument and when the COSBase is an image, replace with acompacted one. At the end save this new document.
I know how I can iterate over each element of this tree, however I could not create a new dictionary and put each new element on it.
So I was wondering if you guys can give me some light on my strategy or maybe to advice about another way of doing it.
Re: pdfbox to overwrite image on a pdf
Did you ever get anywhere with this?
We are also looking to downsize the images in pdf documents, and would prefer not to have to licence another pdf library like itext for a closed source project.