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

    From Jinsong Zhao@21:1/5 to All on Thu Jan 19 21:43:27 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ken@21:1/5 to All on Thu Jan 19 18:01:11 2023
    In article <tqbhev$1iols$1@dont-email.me>, jszhao@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Newall@21:1/5 to Jinsong Zhao on Sat Feb 4 14:15:47 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)