Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

add a logo (png or pdf) to a PDF using only Ghostscript

19 views
Skip to first unread message

Jinsong Zhao

unread,
Jan 19, 2023, 8:44:01 AM1/19/23
to
Hi there,

Is it possible to add a logo (png or pdf) to a PDF using only Ghostscript?

I have learned how to embed text as PDF annotation with Ghostscript,
however, I don't find a way to emb an image to a existing PDF page.

I would appreciate any help or hint.

Best,
Jinsong

ken

unread,
Jan 19, 2023, 1:01:41 PM1/19/23
to
In article <tqbhev$1iols$1...@dont-email.me>, jsz...@yeah.net says...
>
> Hi there,
>
> Is it possible to add a logo (png or pdf) to a PDF using only Ghostscript?

Sort of, but it isn't trivial. For a PNG you would first havr to write a
PostScript program to read PNG files, since Ghostscript doesn't read
PNG.

> I have learned how to embed text as PDF annotation with Ghostscript,
> however, I don't find a way to emb an image to a existing PDF page.
>
> I would appreciate any help or hint.

You need to look at the 'scripting the PDF interpreter' section of the
documentation. You will need to set up the PDF interpreter, set the
media size, draw the page contents, then draw the additioanl content,
then 'show' the page. Depending on the input you may need to open two
PDF files simultaneously to do this.

Note that Ghostscript and the pdfwrite device do not 'add' to existing
files, or modoify files. The outpuit file is completely new and may b
ear no realtyion (in its internal structure) to the original file.


Ken

David Newall

unread,
Feb 3, 2023, 10:16:00 PM2/3/23
to Jinsong Zhao
On 20/1/23 00:43, Jinsong Zhao wrote:
> Is it possible to add a logo (png or pdf) to a PDF using only
> Ghostscript?

Chapman Flack wrote various PostScript libraries, including to allow
including drawing various types of images. See www.anastigmatix.net.

Use it by converting PDF to PS (i.e. pdf2ps), include the Import
resources (plus other prerequisites), translate to where you want the
logo to appear (I generate dynamic output, so use currentpoint
translate), read the file using StatFOO and ReadyFOO, scale as
needed,ExecFOO, then convert back to PDF.

For example, you want something like this at the point you want to
display a JPEG logo:

gsave
/net.anastigmatix.Import /ProcSet findresource begin
10 700 translate % top-left of page
(logo.jpeg)(r)file StatJFIF pop ReadyJFIF
% scale to target size (e.g. 200pt)
% need to divide target size by larger of wx & wy
2 copy gt not {exch} if pop
200 exch div dup scale %
ExecJPEG
end
grestore

(I find that some JPEG files cause errors, so reprocess them with
ImageMagick; convert source.jpeg fixed.jpeg)

This converts source.pdf into output.pdf, displaying logo.jpg on every page:

{
cat anastigmatix.ps # contains required resources
echo "/logo{gsave/net.anastigmatix.Import/ProcSet findresource begin
30 700 translate
(logo.jpg)(r)file StatJFIF pop ReadyJFIF
2 copy gt not{exch}if pop 200 exch div dup scale
ExecJPEG end grestore}def"
pdf2ps - - | sed 's/showpage/logo &/'
} < source.pdf | ps2pdf -dNOSAFER - output.pdf


Note use of -dNOSAFER to allow reading the logo file.
0 new messages