Adam,
Thank you for your idea.
If I isolate both streams, they have the same id in debugger (so I
guess that they are the same) :
COSStream stream1 = (COSStream) obj.getObject();
COSStream stream2 = (COSStream) image.getCOSStream();
I also tried the same analysis after the replacement :
COSStream stream1 = (COSStream) obj.getObject();
COSStream stream2 = (COSStream) image.getCOSStream();
COSStream stream3 = (COSStream) newImage.getCOSStream();
Then, things differ a bit. stream1 is still the same object that
stream2. But stream3 is not.
Instead properties of stream3 are the same objects than those of
stream1 or 2, except of "items" (a LinkedHashMap<K, V> object)
This "item" property seams to contain the dictionnary of the
COSObject. Raw debugger values are of both objects seams the same :
stream1,2: {COSName{Type}=COSName{XObject},
COSName{Filter}=COSName{DCTDecode}, COSName{Subtype}=COSName{Image},
COSName{BitsPerComponent}=COSInt{8},
COSName{ColorSpace}=COSName{DeviceRGB}, COSName{Height}=COSInt{239},
COSName{Width}=COSInt{280}}
stream3: {COSName{Type}=COSName{XObject},
COSName{Filter}=COSName{DCTDecode}, COSName{Subtype}=COSName{Image},
COSName{BitsPerComponent}=COSInt{8},
COSName{ColorSpace}=COSName{DeviceRGB}, COSName{Height}=COSInt{239},
COSName{Width}=COSInt{280}}
Futher, when the replacement is done, the "filteredStream" property of
stream1 and 2 changes. Their object id changes to match the property
of stream3.
Based on that data, streams shall be fine, isn't it ?
This seams to match a fact with PD* objects are not cached in PDFBox
(as said in documentation). It looks like PD* objects are variable
wrappers to COS* Objects.
May be did I miss to do something important after invocation of
replaceStreamWith() and before saving the document. Actually, I do
nothing.
An other idea would be that the stream I replace does not contain the
image stream, but rather some attributes of the XObject. But I'm
really not familiar enough with PDFBox (and not really familliar with
Java too). So I may not know some important concepts.
Thanks for your notice. I wanted to base a parsing of page contents
stream but stopped as soon as I realized Xhref worked. I'l continue
parsing. Thus I don't know how to retrieve the PDXObject or COSObject
from the data of the page stream : The object before the
PDOperator{DO} is COSName{FM0,*} or COSName{Im0,*}... It confused me.
Julien PLÉE
7 Avenue Barthélemy Salettes et Jean-Marie Manset
31320 Castanet-Tolosan
France
+33 6.50.00.60.48
Le 26 août 10 à 00:47, [email protected] a écrit :
Julien,
Doesn't this code[1] create a new image object which is in no way
attached
to the PDF? Modifying "(COSStream) obj.getObject()" seems like it'd
do
what you intend. I'm not familiar with PDXObject.createXObject(),
but it
seems like that'd be creating a copy of the data passed it (similar
to a
copy constructor). Obviously modifying a copy isn't going to affect
the
original.
I'm pretty sure that's your problem, but I've never done anything with
streams nor images in PDFs, so I'm afraid I don't know the way to it's
supposed to be done.
Another thing which might be important: some PDF programs don't
write out
anything in the xref table. This doesn't follow the spec, but Adobe
Reader opens them fine either way, so many people don't realize
they're
out of spec (and thus expect your code to process them the same as a
proper PDF).
[1] PDXObjectImage image = (PDXObjectImage)
PDXObject.createXObject((COSStream) obj.getObject() );
----
Thanks,
Adam
From:
Julien Plée <[email protected]>
To:
[email protected]
Date:
08/25/2010 15:00
Subject:
Replacing images contents
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
? Click here to submit conditions
This email and any content within or attached hereto from Sun West
Mortgage Company, Inc. is confidential and/or legally privileged.
The information is intended only for the use of the individual or
entity named on this email. If you are not the intended recipient,
you are hereby notified that any disclosure, copying, distribution
or the taking of any action in reliance on the contents of this
email information is strictly prohibited, and that the documents
should be returned to this office immediately by email. Receipt by
anyone other than the intended recipient is not a waiver of any
privilege. Please do not include your social security number,
account number, or any other personal or financial information in
the content of the email. Should you have any questions, please
call (800) 453 7884.