You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Philip Poupart <ph...@gmail.com> on 2015/05/01 15:07:43 UTC

Re: Can't stamp newly created contents stream to Resources - XObjects

Hi, I'm using version 1.8.9. The original PDF has a content stream like
this:


q
Q
q
q 1 0 0 1 0 0 cm /Xf1 Do Q
Q
q
BT
36 1125 Td
ET
Q


/Xf1 is a reference to a longer content stream stored in Resources /
XObject. This content stream in the XObject looks normal:

q
Q
q
1 i
1089.013 1143.523 -1215.499 -982.486 re
-126.486 1143.523 m
W n
180 180 m
783 180 -603 801 re
180 180 m
W n
963 1161 -963 -1161 re
0 1161 m
W n
0 1161.015 963 -1161 re
W n
/GS1 gs
q
603.597 0 0 801.561 179.707 179.69 cm
/Im1 Do
.... etc


I get this longer stream out, edit it and am trying to put it back in
place.

Map<String,PDXObject> xObjectMap;

PDStream pdEmbeddedStream = new PDStream(pdf);
OutputStream out = pdEmbeddedStream.createOutputStream();
out.write(contentBytesThatWereEmbeddedInXObject);
out.close();


PDXObjectForm form = new PDXObjectForm(pdEmbeddedStream);
PDXObject pdxObj = PDXObject.createXObject(form.getCOSObject()); <— This
turns out to be null

xObjectMap.put(trimmedXDict, pdxObj);

resources.setXObjects(xObjectMap); ;



I have a COSStream or PDStream, but am trying to make it a PDXObject so I
can store it back in the xObjectMap (which requires a PDXObject).


Hope this makes sense. It is a complex project.


Thanks for any help.

Phil Poupart
phil.poupart@gmail.com



On Thu, Apr 30, 2015 at 1:02 AM, Tilman Hausherr <TH...@t-online.de>
wrote:

> Hi,
>
> I'd like to help, but to be honest, I haven't understood you. What do you
> mean with "stamping it back"?
>
> The usual way to write in a COSStream is like this:
>
> os = cosStream.createUnfilteredStream();
>
> os.write(....)
> os.close()
>
>
>
> If you need more help, mention what version you are using. The official
> one is 1.8.9.
>
> Tilman
>
>
> Am 29.04.2015 um 16:52 schrieb Philip Poupart:
>
>> I'm wondering if anyone can point me in the right direction here. I have a
>> series of PDFs that have short content streams that just reference /Xf1 in
>> the PDF body. These were PDFs modified with iText. It is essentially a PDF
>> placed on a page of a PDF.
>>
>> I'm extracting the nested contents stream just fine. I can modify that
>> contents stream, but am having difficulty stamping it back to the
>> Resources->XObjects dictionary.
>>
>>
>>
>> Original content stream is a reference to /Xf1 object in pdf body.
>>
>> 1 0 0 1 0 0 cm /Xf1 Do Q
>>
>>
>> Below are some snippets of my code.
>>
>>
>> -----------get the nested content stream (this works)
>>
>> String xDict = "Xf1" ;
>>
>> Map<String,PDXObject> trimMap = resources.getXObjects();
>>
>> PDXObject xObjects = trimMap.get(xDict);
>>
>> PDStream nestedContentStream = xObjects.getPDStream();
>>
>>
>>
>>    ... modify nestedContentStream ...
>>
>>
>> -----------stamp it back to resources (fails to create COSStream)
>>
>>
>> byte[] modContentBytes =
>> StringUtils.getBytesIso8859_1(newlyModifiedContentsStr);
>>
>> RandomAccessBuffer rab = new RandomAccessBuffer();
>>
>> rab.write(modContentBytes, 0, modContentBytes.length);
>>
>>
>> COSStream cosStream  = new COSStream(rab); <------ cosStream length is 0
>> even though rab is 218727 bytes
>>
>>
>> PDXObject modifiedPdxObj =
>> PDXObject.createXObject(cosStream.getCOSObject());
>>
>> trimMap.put(trimmedXDict, modifiedPdxObj);
>>
>> resources.setXObjects(trimMap); <------ this blows up because my COSStream
>> length is 0 (I think)
>>
>>
>> Any help would be appreciated. Thanks,
>>
>> Phil Poupart
>> phil.poupart@gmail.com
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Can't stamp newly created contents stream to Resources - XObjects

Posted by Philip Poupart <ph...@gmail.com>.
I figured it out. I avoided creating a new resources Map. This was my
biggest problem, because my new resource Map was incomplete. I just used
the resources Map I extracted from the PDF, made my modification to the
PDXObject, and put the new PDXObject back in. Also, this is by far the best
experience I've ever had interacting with support/developers. Thanks for
all your help, even though my description may have been a bit vague.



On Fri, May 1, 2015 at 8:48 AM, Tilman Hausherr <TH...@t-online.de>
wrote:

> Am 01.05.2015 um 15:07 schrieb Philip Poupart:
>
>> Hi, I'm using version 1.8.9. The original PDF has a content stream like
>> this:
>>
>>
>> q
>> Q
>> q
>> q 1 0 0 1 0 0 cm /Xf1 Do Q
>> Q
>> q
>> BT
>> 36 1125 Td
>> ET
>> Q
>>
>>
>> /Xf1 is a reference to a longer content stream stored in Resources /
>> XObject. This content stream in the XObject looks normal:
>>
>> q
>> Q
>> q
>> 1 i
>> 1089.013 1143.523 -1215.499 -982.486 re
>> -126.486 1143.523 m
>> W n
>> 180 180 m
>> 783 180 -603 801 re
>> 180 180 m
>> W n
>> 963 1161 -963 -1161 re
>> 0 1161 m
>> W n
>> 0 1161.015 963 -1161 re
>> W n
>> /GS1 gs
>> q
>> 603.597 0 0 801.561 179.707 179.69 cm
>> /Im1 Do
>> .... etc
>>
>>
>> I get this longer stream out, edit it and am trying to put it back in
>> place.
>>
>> Map<String,PDXObject> xObjectMap;
>>
>> PDStream pdEmbeddedStream = new PDStream(pdf);
>> OutputStream out = pdEmbeddedStream.createOutputStream();
>> out.write(contentBytesThatWereEmbeddedInXObject);
>> out.close();
>>
>>
>> PDXObjectForm form = new PDXObjectForm(pdEmbeddedStream);
>> PDXObject pdxObj = PDXObject.createXObject(form.getCOSObject()); <— This
>> turns out to be null
>>
>
> I looked at the 1.8 source... PDXObject.createXObject() expects a
> COSStream which dictionary must have the subtype PDXObjectForm.SUB_TYPE.
> But PDXObject.createXObject() then calls new PDXObjectForm() which you
> already did...
>
> Why not just stay with the xform that you already have, store the stream
> somewhere, alter it, and then with xform.createOutputStream() write back
> into it, and then save your file with a new name?
>
> Btw, the null problem is because you have no COSDictionary. You started
> with an empty dictionary when doing
>
> new PDStream(pdf);
>
>
> An Xform looks like this in a PDF:
>
> 15 0 obj
> <<
>   /Name /Form1
>   /Type /XObject
>   /Subtype /Form
>   /FormType 1
>   /Matrix [0.001845 0 0 0.00198 0 0]
>   /Length 19 0 R
>   /Filter /FlateDecode
>   /BBox [0 0 542 505]
>   /Resources 13 0 R
> >>
> stream
> ...
> endstream
> endobj
>
>
> I hope this helps somewhat... if not, keep asking :-)
>
>
> Tilman
>
>
>
>> xObjectMap.put(trimmedXDict, pdxObj);
>>
>> resources.setXObjects(xObjectMap); ;
>>
>>
>>
>> I have a COSStream or PDStream, but am trying to make it a PDXObject so I
>> can store it back in the xObjectMap (which requires a PDXObject).
>>
>>
>> Hope this makes sense. It is a complex project.
>>
>>
>> Thanks for any help.
>>
>> Phil Poupart
>> phil.poupart@gmail.com
>>
>>
>>
>> On Thu, Apr 30, 2015 at 1:02 AM, Tilman Hausherr <TH...@t-online.de>
>> wrote:
>>
>>  Hi,
>>>
>>> I'd like to help, but to be honest, I haven't understood you. What do you
>>> mean with "stamping it back"?
>>>
>>> The usual way to write in a COSStream is like this:
>>>
>>> os = cosStream.createUnfilteredStream();
>>>
>>> os.write(....)
>>> os.close()
>>>
>>>
>>>
>>> If you need more help, mention what version you are using. The official
>>> one is 1.8.9.
>>>
>>> Tilman
>>>
>>>
>>> Am 29.04.2015 um 16:52 schrieb Philip Poupart:
>>>
>>>  I'm wondering if anyone can point me in the right direction here. I
>>>> have a
>>>> series of PDFs that have short content streams that just reference /Xf1
>>>> in
>>>> the PDF body. These were PDFs modified with iText. It is essentially a
>>>> PDF
>>>> placed on a page of a PDF.
>>>>
>>>> I'm extracting the nested contents stream just fine. I can modify that
>>>> contents stream, but am having difficulty stamping it back to the
>>>> Resources->XObjects dictionary.
>>>>
>>>>
>>>>
>>>> Original content stream is a reference to /Xf1 object in pdf body.
>>>>
>>>> 1 0 0 1 0 0 cm /Xf1 Do Q
>>>>
>>>>
>>>> Below are some snippets of my code.
>>>>
>>>>
>>>> -----------get the nested content stream (this works)
>>>>
>>>> String xDict = "Xf1" ;
>>>>
>>>> Map<String,PDXObject> trimMap = resources.getXObjects();
>>>>
>>>> PDXObject xObjects = trimMap.get(xDict);
>>>>
>>>> PDStream nestedContentStream = xObjects.getPDStream();
>>>>
>>>>
>>>>
>>>>     ... modify nestedContentStream ...
>>>>
>>>>
>>>> -----------stamp it back to resources (fails to create COSStream)
>>>>
>>>>
>>>> byte[] modContentBytes =
>>>> StringUtils.getBytesIso8859_1(newlyModifiedContentsStr);
>>>>
>>>> RandomAccessBuffer rab = new RandomAccessBuffer();
>>>>
>>>> rab.write(modContentBytes, 0, modContentBytes.length);
>>>>
>>>>
>>>> COSStream cosStream  = new COSStream(rab); <------ cosStream length is 0
>>>> even though rab is 218727 bytes
>>>>
>>>>
>>>> PDXObject modifiedPdxObj =
>>>> PDXObject.createXObject(cosStream.getCOSObject());
>>>>
>>>> trimMap.put(trimmedXDict, modifiedPdxObj);
>>>>
>>>> resources.setXObjects(trimMap); <------ this blows up because my
>>>> COSStream
>>>> length is 0 (I think)
>>>>
>>>>
>>>> Any help would be appreciated. Thanks,
>>>>
>>>> Phil Poupart
>>>> phil.poupart@gmail.com
>>>>
>>>>
>>>>  ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>
>>>
>>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Can't stamp newly created contents stream to Resources - XObjects

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 01.05.2015 um 15:07 schrieb Philip Poupart:
> Hi, I'm using version 1.8.9. The original PDF has a content stream like
> this:
>
>
> q
> Q
> q
> q 1 0 0 1 0 0 cm /Xf1 Do Q
> Q
> q
> BT
> 36 1125 Td
> ET
> Q
>
>
> /Xf1 is a reference to a longer content stream stored in Resources /
> XObject. This content stream in the XObject looks normal:
>
> q
> Q
> q
> 1 i
> 1089.013 1143.523 -1215.499 -982.486 re
> -126.486 1143.523 m
> W n
> 180 180 m
> 783 180 -603 801 re
> 180 180 m
> W n
> 963 1161 -963 -1161 re
> 0 1161 m
> W n
> 0 1161.015 963 -1161 re
> W n
> /GS1 gs
> q
> 603.597 0 0 801.561 179.707 179.69 cm
> /Im1 Do
> .... etc
>
>
> I get this longer stream out, edit it and am trying to put it back in
> place.
>
> Map<String,PDXObject> xObjectMap;
>
> PDStream pdEmbeddedStream = new PDStream(pdf);
> OutputStream out = pdEmbeddedStream.createOutputStream();
> out.write(contentBytesThatWereEmbeddedInXObject);
> out.close();
>
>
> PDXObjectForm form = new PDXObjectForm(pdEmbeddedStream);
> PDXObject pdxObj = PDXObject.createXObject(form.getCOSObject()); <— This
> turns out to be null

I looked at the 1.8 source... PDXObject.createXObject() expects a 
COSStream which dictionary must have the subtype PDXObjectForm.SUB_TYPE. 
But PDXObject.createXObject() then calls new PDXObjectForm() which you 
already did...

Why not just stay with the xform that you already have, store the stream 
somewhere, alter it, and then with xform.createOutputStream() write back 
into it, and then save your file with a new name?

Btw, the null problem is because you have no COSDictionary. You started 
with an empty dictionary when doing

new PDStream(pdf);


An Xform looks like this in a PDF:

15 0 obj
<<
   /Name /Form1
   /Type /XObject
   /Subtype /Form
   /FormType 1
   /Matrix [0.001845 0 0 0.00198 0 0]
   /Length 19 0 R
   /Filter /FlateDecode
   /BBox [0 0 542 505]
   /Resources 13 0 R
 >>
stream
...
endstream
endobj


I hope this helps somewhat... if not, keep asking :-)


Tilman

>
> xObjectMap.put(trimmedXDict, pdxObj);
>
> resources.setXObjects(xObjectMap); ;
>
>
>
> I have a COSStream or PDStream, but am trying to make it a PDXObject so I
> can store it back in the xObjectMap (which requires a PDXObject).
>
>
> Hope this makes sense. It is a complex project.
>
>
> Thanks for any help.
>
> Phil Poupart
> phil.poupart@gmail.com
>
>
>
> On Thu, Apr 30, 2015 at 1:02 AM, Tilman Hausherr <TH...@t-online.de>
> wrote:
>
>> Hi,
>>
>> I'd like to help, but to be honest, I haven't understood you. What do you
>> mean with "stamping it back"?
>>
>> The usual way to write in a COSStream is like this:
>>
>> os = cosStream.createUnfilteredStream();
>>
>> os.write(....)
>> os.close()
>>
>>
>>
>> If you need more help, mention what version you are using. The official
>> one is 1.8.9.
>>
>> Tilman
>>
>>
>> Am 29.04.2015 um 16:52 schrieb Philip Poupart:
>>
>>> I'm wondering if anyone can point me in the right direction here. I have a
>>> series of PDFs that have short content streams that just reference /Xf1 in
>>> the PDF body. These were PDFs modified with iText. It is essentially a PDF
>>> placed on a page of a PDF.
>>>
>>> I'm extracting the nested contents stream just fine. I can modify that
>>> contents stream, but am having difficulty stamping it back to the
>>> Resources->XObjects dictionary.
>>>
>>>
>>>
>>> Original content stream is a reference to /Xf1 object in pdf body.
>>>
>>> 1 0 0 1 0 0 cm /Xf1 Do Q
>>>
>>>
>>> Below are some snippets of my code.
>>>
>>>
>>> -----------get the nested content stream (this works)
>>>
>>> String xDict = "Xf1" ;
>>>
>>> Map<String,PDXObject> trimMap = resources.getXObjects();
>>>
>>> PDXObject xObjects = trimMap.get(xDict);
>>>
>>> PDStream nestedContentStream = xObjects.getPDStream();
>>>
>>>
>>>
>>>     ... modify nestedContentStream ...
>>>
>>>
>>> -----------stamp it back to resources (fails to create COSStream)
>>>
>>>
>>> byte[] modContentBytes =
>>> StringUtils.getBytesIso8859_1(newlyModifiedContentsStr);
>>>
>>> RandomAccessBuffer rab = new RandomAccessBuffer();
>>>
>>> rab.write(modContentBytes, 0, modContentBytes.length);
>>>
>>>
>>> COSStream cosStream  = new COSStream(rab); <------ cosStream length is 0
>>> even though rab is 218727 bytes
>>>
>>>
>>> PDXObject modifiedPdxObj =
>>> PDXObject.createXObject(cosStream.getCOSObject());
>>>
>>> trimMap.put(trimmedXDict, modifiedPdxObj);
>>>
>>> resources.setXObjects(trimMap); <------ this blows up because my COSStream
>>> length is 0 (I think)
>>>
>>>
>>> Any help would be appreciated. Thanks,
>>>
>>> Phil Poupart
>>> phil.poupart@gmail.com
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org