1

I'm trying to merge images in JPG format with standard PDF documents, while keeping images in the same size. Earlier I was using ImageMagick's convert, but it results in huge quality drop since it converts everything into images, so I'm switching to ghostscript (or eventually itextpdf).

I found this code which inserts scaled image into A4 page:

gs \
 -sDEVICE=pdfwrite \
 -o foo.pdf \
  /usr/local/share/ghostscript/8.71/lib/viewjpeg.ps \
 -c \(my.jpg\) viewJPEG

PdfWriter from itextpdf in this way or that way could be alternative but it also adds an image into a page.

After inspecting ImageMagick's behavioral, I found out command it was using which I think is closest to my solution, but it doesn't seem to work when I'm trying to modify or use it. How should I modify it?

gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r72x72  -sOutputFile=out_gs.pdf fox_big.jpg
Community
  • 1
  • 1
Naan
  • 521
  • 1
  • 11
  • 28
  • 1
    That's strange: when you talk about iText, you refer to articles on two obscure web sites that aren't related to iText in any way. I'd expect you to link to articles on the [official iText web site](http://developers.itextpdf.com/). Is there a reason why you aren't using the iText web site when working with iText? – Bruno Lowagie Apr 14 '16 at 08:26
  • I have the impression that there is an entire _ecosystem_ of bad tutorials, usually obsolete code copy/pasted together, with the only intention of generating pageviews... – Amedee Van Gasse Apr 14 '16 at 08:50
  • 1
    I've found first link on another SO question related to itext. When searching "itext jpg pdf" on google I've found second link as first result when your page was 9th. I promise to do better resarch before asking a question in future. – Naan Apr 14 '16 at 09:01
  • Just in passing, the reason the stated command doesn't work is because it doesn't use viewjpeg.ps and so can't read a JPEG file directly. You need to include the "/usr/local/....../viewjpeg.ps -c (fox_big.jpg) viewJPEG" Of course the remainder of that command produces a PNG but perhaps that's what you expected. And if you are going to use Ghostscript, better to use something a little newer, 8.71 is (IIRC) 6 years old. – KenS Apr 14 '16 at 16:12

1 Answers1

2

The answer to your question can be found on the official iText web site: How to add multiple images into a single PDF?

In the MultipleImages example, we take a selection of images, and we convert them to a PDF: multiple_images.pdf

The page size is set to match the size of the image:

For the first image:

Image img = Image.getInstance(IMAGES[0]);
Document document = new Document(img);

As you can see, we pass the img to the Document constructor. This works, because Image extends the Rectangle class.

For the subsequent images, we change the page size:

document.setPageSize(img);

Note that we also need to set the absolute position:

img.setAbsolutePosition(0, 0);

Please go to the official web site when you want to find info and examples on iText. I've spent many months writing all that content and putting it on the web site. It's frustrating when I see that people don't take advantage of all that work. (It feels like I wasted my time.)

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165