pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: resize inline images
Date Fri, 22 Feb 2019 19:15:45 GMT
Am 22.02.2019 um 09:41 schrieb Matteo Gamboz:
> Hi Tilman,
>
>> Did you understand them?
> sort of...
> (please be patient, I think I'm making some confusion :-)
>
> following your suggestion, I was able to create and add another Inline
> Image with the desired resolution into my pdf (I check with "pdfimages
> -list" from poppler-utils):
>
> PDImageXObject helper_img = LosslessFactory.createFromImage(document, scaledImage);
> InputStream img_data_stream = helper_img.createInputStream();
> byte [] img_data = IOUtils.toByteArray(img_data_stream);
> PDInlineImage replacement_img = new PDInlineImage(operator.getImageParameters(),
>                                                    img_data,
>                                                    resources);
>
> I'm not sure about the "filters" you mentioned...
> I used the same parameters as the original (operator.getImageParameters()).


The filters are the compression... for inline images, shorter names are 
available, but I just saw that this is not required, so don't bother.

> Then I appended my new image to the page stream but this way I get two
> inline images... (if I use Appended.OVERWRITE I end up removing all
> other operators from the stream)


Yes, that is not surprising. (both) So you have to choose which one.


>
> PDPageContentStream pageContentStream = new PDPageContentStream(document,
>                                                                  currentPage,
>                                                                  AppendMode.APPEND,
>                                                                  false,
>                                                                  true);
> pageContentStream.drawImage(replacement_img, 0, 0);
> pageContentStream.close();
>
>
>
> I tried other approaches:
> . (right after creating my replacement_img)
>    take the content stream of the current page,
>    parse it, get a list of tokens
>    run through the list of tokens
>    and build a new list
>    omitting "BI" operator & co.
>    create a new stream from the tokens
>    and set it to the page
>    (BUT: I have an error about some not-closed stream when saving)


Then you should investigate that. For example, in

helper_img.createInputStream();

You didn't close the stream there.

IMHO you could insert the new BI stuff at the place where the old one was.


>
> . adapt example RemoveAllText.java
>    similar to the above: run through the tokens and modify those
>    related to images
>    (BUT: I'm not sure how to get to the image XObject from the token)


That is a name, just before "Do". That one you'll find in the resources.


>
> . extend GraphicsOperatorProcessor
>    (copying from operator.graphics.BeginInlineImage)
>    i.e. create a StreamEngine to process my adapted "BeginInlineImage" operator
>    (BUT: I get a ClassCastException on the StreamEngine I use)

Hmm, don't know about that one. Postpone that idea.


>
> I think some of the problems are because I've used java ~15 years ago
> and forgot about it until now :-)
>
>
> Since I think I don't really get the hang of the library,
> I have a different (exploratory) proposition.
>
> Let's say that I generally want to
> "change resolution of raster images"
> would you (or anyone else) be interested in writing the appropriate
> code under compensation?

Maybe someone else, but not me.

No because it sounds like a project with risks, and because I have a day 
job. But I'll still try to help a bit here (for free!). I think it is 
possible, but I expect some surprises. E.g. if the size changes.

The problem is that PDF isn't really made for editing.

Please retry there strategy where you had the closing error (fix the bug 
I mentioned), and post enough code and upload the source and result PDFs.

Tilman


> I think that the code could be added to the examples in the source.
> At the moment I do not have any budget, but if we can come up with a
> sum I could try and ask my boss...
>
> I like this kind of approach because I would be able to:
> . get what I want in the short term,
> . learn the best way to do it (so that next time I can do it myself),
> . contribute materially to pdfbox
> . and add valuable piece of "documentation" (the example) to the library.
> . (bonus) have someone else maintain that code
> (I just hope my boss will feel the same :-)
>
> If you (or anyone else) feel this proposal is inappropriate, please
> let me know it and just forget about this.
>
> m
>
>
> On Thu, 21 Feb 2019 18:49:16 +0100,
> Tilman Hausherr wrote:
>> Hi, did you try the two things I mentioned on stackoverflow? (Did you
>> understand them?) Did you create a PDF file with it? If yes, please
>> share it, I might be able to tell what is missing.
>>
>> Tilman
>>
>> Am 21.02.2019 um 09:59 schrieb Matteo Gamboz:
>>> Hi all,
>>>
>>> I must resize (change the resolution) of some Inline Images in a PDF.
>>>
>>> I'm a bit stuck on how best to proceed.
>>>
>>> I've tried by extending PDFStreamEngine and overriding method
>>> processOperator (https://stackoverflow.com/q/54744038/1581629) but
>>> without good results.
>>>
>>> Since I want to modify a stream (I think), I would like to ask if the
>>> approach is correct or if there are better methods (e.g. extend
>>> GraphicsOperatorProcessor or something...)
> ---------------------------------------------------------------------
> 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


Mime
View raw message