Hello,
I have to put a watermark stamp on images stored in PDF files and I'm
having hard times trying to embed images back into the PDF.
I'm using the XrefTable to filter images. For embedding, I'm trying to
replace the stream of the original object but with no luck, the saved
PDF always looks the same.
Here is my method code focused on the PDXObjectImage :
/**
* Replaces a PDF image content with content from an image file on file
* system identified by the object id.
*
* (this.doc : PDDocument)
* @param obj
* @throws IOException
*/
protected void embedImageBack(COSObject obj) throws IOException
{
String path = "img/";
PDXObjectImage image = (PDXObjectImage) PDXObject.createXObject(
(COSStream) obj.getObject() );
File inputFile = new File(
path+obj.getObjectNumber()+"."+image.getSuffix() );
PDXObjectImage newImage = null;
if (image.getSuffix().equals("jpg"))
newImage = new PDJpeg( this.doc, new FileInputStream(inputFile)
);
else
newImage = new PDCcitt( this.doc,
(RandomAccess) new RandomAccessFile( inputFile,
"r" ) );
image.getCOSStream().replaceWithStream(newImage.getCOSStream());
this.shouldSaveDoc = true;
}
After all images have been processed, I save the document in a new
file, but except that the file size changes, nothing else visible
happens.
Thanks for any help.
Julien PLÉE