0

I am converting a PDF to a Tiff File using GhostScript and ImageMagick in a C# winform app, but I am not getting the correct settings in the result.

DPI is set to 300 with GhostScript, and 8-Bit Depth with ImageMagick. The file is returning as 40-Bit with 96 DPI.

// GhostScript PDF to TIFF Conversion
private void GS_ConvertPdfToTiff(string pdfPath, string outputFolder) {
    using (var rasterizer = new GhostscriptRasterizer()) {
        rasterizer.Open(pdfPath);

        for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++) {
            string outputPath = Path.Combine(outputFolder, $"GhostMagick_{pageNumber}.tif");

            using (Bitmap img = new Bitmap(rasterizer.GetPage(300, pageNumber))) // 300 DPI
            {
                ImgMgkConvertToCMYK(img, outputPath);
            }
        }
    }
}

// Convert Bitmap to CMYK and save with ImageMagick
private void ImgMgkConvertToCMYK(Bitmap image, string outputPath) {
    using (var memoryStream = new MemoryStream()) {
        // Save the Bitmap to MemoryStream
        image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
        memoryStream.Seek(0, SeekOrigin.Begin); // Reset stream position

        // Load the image from the memory stream into MagickImage
        using (var magickImage = new MagickImage(memoryStream)) {
            // Set to CMYK color space
            magickImage.ColorSpace = ColorSpace.CMYK;
            magickImage.Depth = 8; // 8-bit depth for printing
            magickImage.Write(outputPath); // Save to the provided path (Map.tif)
        }
    }
}

Conversion Requirements: 300 dpi CMYK (color encoding) Bit Depth - 8 bit Anti-Aliased

Any insights on to why this might be happening and how to get the correct conversion?

Thanks.

Resulting Settings from Conversion

3
  • 1
    I removed the Ghostscript tag as this is not a Ghostscript question; or there is insufficient information for it to be so. It could be a Ghostscript.NET question, but there's no Ghostscript command line, or API calls here. Why are you using ImageMagick at all ? If you want a CMYK output then use a Ghostscript CMYK output device (eg tiff32nc). If you want anti-aliased output then use the anti-aliasing tiff options (eg DownscaleFactor). Commented Mar 10 at 16:05
  • ^^ Also: image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); - why Png? Commented Mar 10 at 16:06
  • Post your PDF input. Which of your input PDF or output TIFF has the 96 dpi. Commented Mar 10 at 22:20

1 Answer 1

-1

Image Magic has always considered PDF (IMHO incorrectly) as having a basic scalar unit of 72x72 DPI.

There are no internal fixed units in a PDF as a PDF unit can be upscaled or downscaled by transformations. Hence we nominally use 72 core unscaled linear units to a nominal inch.

To place an image of one pixel as the width and height of a page is allowed (and often used as such) So say that pixel is 8.5 x 11 linear inches, does that make it approx. 0.1 DPI and in which plane?

PDF thus allows for raster import and export at a desired density / resolution but at specified /Width and /Height.

ImageMagick also uses the term density for resolution. Thus it requires to pass that info onto GhostScript's temporary generated image files before any further modifications.

If you have alpha it will be problematic using IM on the GhostScript rendering thus I suggest:

magick -density 300 -units pixelsperinch page1.pdf -alpha off 300dpi-whatever.tif

enter image description here

If you don't address the -alpha one way or another it will produce a black area (NOTE: With it off (above) there is an extra vertical line at the boundary).

Without -alpha off the bits are now 64 per pixel and also not CMYK! so other controls are needed.

enter image description here

The more direct method using just GS without any magick introducing added problems is possibly:
(I was corrected by @KenS, suggesting up and down scale, with 32bit and Alpha bits can be altered 2, 3, or 4 depending on range so here =4 is required):

READ THE MANUAL

tiff12nc tiff24nc tiff32nc tiff48nc tiff64nc tiffcrle tiffg3 tiffg32d tiffg4 tiffgray tifflzw tiffpack tiffscaled tiffscaled24 tiffscaled32 tiffscaled4 tiffscaled8 tiffsep tiffsep1

NOTE THE presence of CMYK range @ 32 bits per 300DPI pixels

GS -sDEVICE=tiffscaled32 -r1200 -dDownScaleFactor=4 -dPDFFitPage -ooutput%d.tif -dTextAlphaBits=4 -f input.pdf

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Rather than using (Text|Graphics)AlphaBits, use the tiffscaled drivers and -dDownScaleFactor to achieve the desired resolution. It's slower but much more reliable, especially where the input involves transparency. For example set -r1200 -dDownScaleFactor=4 to get 300 dpi with 4 bits of alpha for anti-aliasing. Use 2 or 3 for lower values of anti-aliasing. Note that tiff48nc produces 3x16-bit planes and is thus an RGB device, not CMYK. tiff64nc is CMYK. tiffscaaled32 produces 4x8-bit planes; CMYK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.