Home > other >  How to keep comments in a ghostscript pdf conversion to png?
How to keep comments in a ghostscript pdf conversion to png?

Time:11-09

I am converting a page of a PDF to an image, and that is working fine, the issue I am having is that it is leaving out the comments or annotations on the PDF page.

When I use the TIFF format, it keeps them in, but when I output to a PNG it removes them.

How do I keep them in?

Here is the code I am using to convert the PDF:


function Convert-pdf {

    param (
        $PDF_Output, # Output path where converted PDF files will be stored
        $path_to_pdf,
        $PNG_Output  # Output path where the PNG files will be saved
    )

  #Path to your Ghostscript EXE
  $tool = 'C:\Program Files\gs\gs9.55.0\bin\gswin64c.exe'
  $pdf = get-item $path_to_pdf
  $png = $PNG_Output   $pdf.BaseName   "d"   ".png"

  if(test-path $png)
  {
      "png file already exists "   $png
  }
  else
  {
      'Processing '   $pdf.Name
      $param = "-sOutputFile=$png"
      & $tool -q -dNOPAUSE -sDEVICE=png256 $param -r300 $pdf.FullName -c quit
  }
  Move-Item $pdf $PDF_Output
  return $png
}

When I change png256 to tiff64nc it will show the annotations on the PDF, otherwise it does not show them.

Edit: I am using GS version 9.55.0

Here is an example image, of the pdf I am putting through the code:

testPdf.pdf

And here is the output that I get:

testOutput.png

If you want to test for yourself, here is the file:

enter image description here

So in total to get 256 max colours you need 3 lines using your shell path\variables and beware multipage %d values.

mutool draw -o "out-draw.png"  -r 300 "69846382 annots not printing in GS png.pdf"
mutool draw -o "out-draw.pdf"  "out-draw.png"
gswin64c -o "GSout256.png" -sDEVICE=png256 -r300 "out-draw.pdf"

And since the common format handling is akin to PS graphics using that in two steps gave a much faster result.

mutool draw -o "out-draw.ps" -r 300 "69846382 annots not printing in GS png.pdf"
gswin64c -o "GSout256.png" -sDEVICE=png256 -r300 "out-draw.ps"

However the result of double image in-out processes was "Moire" than desired enter image description here , which I should have expected. Whichever method I used to improve the result it was not an even tone enter image description here thus I suspect even if the annotations were good the result would not equal expectation, that is not just Ghostscript, I get similar results saving as 256 colour output from MSpaint etc. in effect its colour "dithering".

Also I tried without success to pipe one into another since GS should input piped data, and MuTool can output to pipe, but hit problems, most probably due to me corrupting GS by testing different calls (beware accidentally overwriting the exe) I dropped trying, as we see, it seemed not worth chasing in this case.

Finally with my trials for the safest shortest command we are currently left with mutool 1023 colours that would need another command line tool to drop those to 8 bit per pixel colours. enter image description here

mutool draw -o"out.png" -r 300 "69846382 annots not printing in GS png.pdf"
irfanview / gimp / other image converter

The alternative is to use a graphics package that can read pdf and output 8 bpp without dithering for a result much like this try right click to download enter image description here

  • Related