How to add images to existing PDF?

This Forum is for the use of Software Developers requiring help and assistance for Tracker Software's PDF-Tools SDK of Library DLL functions(only) - Please use the PDF-XChange Drivers API SDK Forum for assistance with all PDF Print Driver related topics or PDF-XChange Viewer SDK if appropriate.

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

Post Reply
omascia
User
Posts: 48
Joined: Thu Mar 11, 2010 7:07 pm

How to add images to existing PDF?

Post by omascia »

How would you proceed to add images to an existing PDF ?
Here's the background for the request. I may need to create many different rather small PDF files, each including nearly always the same 1, 2 or 3 images, everything else is text and is different on each PDF. To make it real life : consider those PDF files are for-archive copies of sales invoices. The bitmaps used as decorum are the same on each invoice. Without images, such PDF are about 5 KB in size. With images, they are about 35 KB. Suppose 10,000 invoices a year, you store 10,000 times the same images in all those PDF. The pursued trick would be NOT to add the images in those PDF, add some DocumentInfo strings to describe what image should have been placed at which position and size and save those images (once) in some DB. The process which needs to retrieve one archived PDF can read it, see the properties, fetch the image(s) and add them before handing the resulting "re-constructed" PDF to whatever post-treatment is expected (print or view or email).
The downside is "How to add images to existing PDF?"...
The closest I could come to is to add them as watermarks. Works fine... visually.
On the details side it is a failure because those PDF with added watermarks are MUCH bigger (let's says 180 KB instead of 30 KB) than what the original PDF would have been if images had been added to it.

My question is then two-fold:
- A) Is there a bug or a technical limitation explaining why adding some color jpeg as a watermark or adding it at the same coordinates and size as an image leads to very different fPDF ile sizes ? Is there a workaround to this issue ?
- B) Is there some way (even quite complex) to add images to an existing PDF (loaded through PXCp_ReadDocument), pretty much like one can do it with PXC_AddImage and PXC_PlaceImage ?

Thanks a lot for sharing ideas on the matter.
--
Olivier Mascia
omascia
User
Posts: 48
Joined: Thu Mar 11, 2010 7:07 pm

Re: How to add images to existing PDF?

Post by omascia »

Answering myself... after some more ideas and quick and dirty code testing...
To add those images to the original PDF, I create a new PDF using PXC_ library made of blank pages of the same dimensions as the original PDF, add and place images on this one, taking care of adding once even if placing the same image multiple times (to keep that intermediate file as neat as possible).
Then I open it using PXCp_ and use PlaceContents to add those pages hosting the images as backgrounds to the pages of the original file.
A bit more lengthly to write, but does the work nicely : the resulting merged PDF has a byte-size very close the one which would have been built at first if images had been added to it directly.

I will probably use the same 'technique' to solve my next problem (which I had not yet posted about) : how to, while creating a PDF file using PXC_ and having created some pages of content, append some pages from another PDF, and then continue on creating other pages ?

I think I will add empty pages as place-holders for the pages which should have been appended, then use PXCp_PlaceContents to merge in those pages.

Any other idea or recommendation of path to follow ?
omascia
User
Posts: 48
Joined: Thu Mar 11, 2010 7:07 pm

Re: How to add images to existing PDF?

Post by omascia »

Even though I have a workaround for my needs of adding background images after the facts, the part one of my initial post might still see an answer. But there is no hurry. It may be a bug in the library or an inherent limitation of the PDF file formats. I don't know (else I would not use PDF Tools to do the job! : :D )

- A) Is there a bug or a technical limitation explaining why adding some color jpeg as a watermark or adding it at the same coordinates and size as an image leads to very different fPDF ile sizes ? Is there a workaround to this issue ?
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17929
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: How to add images to existing PDF?

Post by Tracker Supp-Stefan »

Hello omascia,

Glad to see you've managed to get our PDF Tools SDK working as you need it to!

And to answer your remaining question - yes placing an image as part of the base contents of the PDF allows it to be optimized, while adding a watermark "bloats" the file as the watermark is a separate type of structure which requires a specific dictionary (part of the PDF Structure) to describe it and this then adds more than just the image data + coordinates to the PDF file. So building up a new page(s) is always going to be more optimal size wise.

Regards,
Stefan
Post Reply