Here is the information of images in a single-page pdf file:
$ pdfimages -list p1.pdf
page num type width height color comp bpc enc interp object ID
---------------------------------------------------------------------
1 0 image 900 1100 rgb 3 8 image yes 4 0
1 1 mask 900 1100 - 1 1 image no 4 0
I extract images from the pdf using pdfimages
:
$ pdfimages -p p1.pdf ./p1
$ ls
p1-001-000.ppm p1-001-001.pbm p1.pdf
The output
p1-001-000.ppm
and p1-001-001.pbm
correspond to "image" and "mask" shown earlier resp.
Then I try to recover the original pdf from the "image" and "mask", using convert
and pdftk
in two different ways ("stamp" and "background"):
$ for i in *.{ppm,pbm}; do convert $i $i.pdf; done
$ ls
p1-001-000.ppm p1-001-000.ppm.pdf p1-001-001.pbm p1-001-001.pbm.pdf p1.pdf
$ pdftk p1-001-000.ppm.pdf stamp p1-001-001.pbm.pdf output p1stamp.pdf
$ pdftk p1-001-000.ppm.pdf background p1-001-001.pbm.pdf output p1bkg.pdf
p1stamp.pdf
is blank (very different from originalp1.pdf
), whilep1bkg.pdf
and originalp1.pdf
look similar but still different around the right and low borders.The output of
pdfimages
shown at beginning has a column named "type"type
The image type. Possible values are: image (an opaque image), mask (a monochrome image mask), smask (a soft-mask image) and stencil (a monochrome mask image used for painging a color or a pattern).
Note: Tranparency in PDF for images is created by using two separate PDF objects: one for the image and one for the mask or smask. The mask/smask belonging to a transparent image always directly follows image in the listing.
Are "mask", "smask" and "stencile" used as foreground or background in a pdf file?
How are they different from "watermark"? Is "watermark" always used as background?
Another issue is that the two new pdf files
p1stamp.pdf
andp1bkg.pdf
are both 476 KB, much larger than the originalp1.pdf
which is only 94 KB. Even if I compress the new pdf files by following command, their sizes don't change:$ pdftk p1bkg.pdf output p1bkgcomp.pdf compress
How can we realistically do the opposite of the splitting work by
pdfimages
, i.e. recover original pdf file from their images
extracted by pdfimages
. so that the appearance and size are the same or close to the original pdf file?
Thanks.