0

I'm trying to convert a page of a PDF to an image. I'm successful with most PDF's I've tried with but this one in particular always ends up with a lot of whitespace on one side or strange scaling.

I've tried every combination of every fixed media, fixed resolution, fit page, use crop/bleed/trim/art box, etc. parameter to fix the issue but nothing does it. The best I get is the right content size but offset and chopped off.

enter image description here

Here's what it should look like, according to every PDF reader I've tried:

enter image description here

Here's a link to the PDF (8 MB) for testing.

https://drive.google.com/file/d/1ErS3KxADb1YAdzM7FG7T5dO8QnW4l1AQ/view?usp=sharing

Edit 1:

Here's what it looks like using just -dUseCropBox without a cropbox override:

enter image description here

I'm using Ghostscript.NET with very simple code. I create a rasterizer, call Ope(PDF file, ghostscript dll in bytes), then GetPage(DPI, page number). To use other flags I add a custom switch to the rasterizer before calling open

using(var rasterizer = new GhostscriptRasterizer()) {
    //rasterizer.CustomSwitches.Add("-dFIXEDMEDIA");
    //rasterizer.CustomSwitches.Add("-dFIXEDRESOLUTION");
    //rasterizer.CustomSwitches.Add("-dPSFitPage");
    //rasterizer.CustomSwitches.Add("-dFitPage");
    //rasterizer.CustomSwitches.Add("-dPDFFitPage");
    //rasterizer.CustomSwitches.Add("-dUseCropBox");
    //rasterizer.CustomSwitches.Add("-dPrinted");
    //rasterizer.CustomSwitches.Add("-dUseBleedBox");
    //rasterizer.CustomSwitches.Add("-dUseTrimBox");
    //rasterizer.CustomSwitches.Add("-dUseArtBox");
    //rasterizer.CustomSwitches.Add("-sPAPERSIZE=letter");
    //rasterizer.CustomSwitches.Add("-dORIENT1=true");
    //etc

    rasterizer.Open(pdfFilePath, ghostscriptDLL);

    img = rasterizer.GetPage(dpi, pageNumber);
    img.Save(pageFilePath, imageFormat);
}

I'll try again with the latest version of just ghostscript (no .NET) and see if that makes a difference.

Edit 2:

Using just gswin64c version 9.55.0 and -dUseCropBox works as KenS said. Since I don't need Ghostscript.NET to do that, that's a good resolution.

miapuffia
  • 31
  • 4
  • 2
    Your PDF file includes a CropBox (at least on page 1 which is the one you show above). Ghostscript by default uses the MediaBox, if you want to use the CropBox instead then set -dUseCropBox. When I try that here with current code (version 9.56.0, Windows 64-bit) the result is exactly what you say it should be and as imaged by Acrobat and other PDF consumers. If that doesn't work for you then you'll need to be more specific about what the problem is that you see, and you should state the version of GS, the OS and where you got Ghostscript from and the actual command line used. – KenS Mar 26 '22 at 09:44
  • 2
    You really, really, don't want to add all those controls. You've set **all** the box controls, there's no way GS can no which one you mean so if a PDF file has more than one, it will use the last one listed on the command line. You've also set FIXEDMEDIA and PAPERSIZE, which forces the media size to be letter in your case, and overrides all the Box controls. On top of that you've specified FitPage (3 different varieties) which will rescale the 'Box' values so they fit the fixed media size. As K J implies below, when you have a problem, try simplifying, not complexifying, the command line. – KenS Mar 28 '22 at 08:14

1 Answers1

0

Using just gswin64c version 9.55.0 and -dUseCropBox works as KenS said. Since I don't need Ghostscript.NET to do that, that's a good resolution.

miapuffia
  • 31
  • 4