Question

I am using GhostScript.NET to print a PDF. When I print it at 96DPI, the PDF prints fine, but is a little blurry. If I try to print the document at 600DPI, the page that prints extremely magnified.

using GhostScript.NET.Rasterizer;
using System.Drawing.Printing;

PrintDocument doc = new PrintDocument();
doc.PrinterSettings.PrinterName="<printer name>";
doc.PrinterSettings.Copies=(short)1;
GhostScriptRasterizer rasterizer = new GhostScriptRasterizer();
rasterizer.Open("abc.pdf");
//Image page = rasterizer.GetPage(96,96); <-- this one prints ok
Image page = rasterizer.GetPage(600,600);
doc.Graphics.DrawImage(page, new Point());

One thing that I noticed when looking at the page object was that although I passed GetPage() 600, 600 - the image returned had a HorizontalResolution of 96 and a VerticalResolution of 96.

So I tried the following:

Bitmap b = new Bitmap(page.Width,page.Height);
b.SetResolution(600,600);
Graphics g = Graphics.FromImage(b);
g.DrawImage(page,0,0);
page = b; 

This has a HorizontalResolution of 600 and VerticalResolution of 600, but this printed the image even larger!

Can anyone give advice here?

Was it helpful?

Solution

hi i got the same Problem.

The Rasterizer only zoom to the dpi...

you have to use the GhostScriptProcessor.

private List<string> GetImageWithGhostGhostscriptProcessor(string psFilename, string outputPath, int dip = 300)
    {
        if (!Directory.Exists(outputPath))
            Directory.CreateDirectory(outputPath);

        GhostscriptVersionInfo gv = GhostscriptVersionInfo.GetLastInstalledVersion(
            GhostscriptLicense.GPL | GhostscriptLicense.AFPL,
            GhostscriptLicense.GPL);

        using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true))
        {
            processor.Processing += new GhostscriptProcessorProcessingEventHandler(processor_Processing);

            List<string> switches = new List<string>();
            switches.Add("-empty");
            switches.Add("-dSAFER");
            switches.Add("-dBATCH");
            switches.Add("-dNOPAUSE");
            switches.Add("-dNOPROMPT");
            switches.Add(@"-sFONTPATH=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts));
            switches.Add("-dFirstPage=" + 1);
            switches.Add("-dLastPage=" + 1);
            //switches.Add("-sDEVICE=png16m");
            switches.Add("-sDEVICE=tiff24nc");
            //switches.Add("-sDEVICE=pdfwrite");

            switches.Add("-r" + dip);
            switches.Add("-dTextAlphaBits=4");
            switches.Add("-dGraphicsAlphaBits=4");

            switches.Add(@"-sOutputFile=" + outputPath + "\\page-%03d.tif");
            //switches.Add(@"-sOutputFile=" + outputPath + "page-%03d.png");
            switches.Add(@"-f");
            switches.Add(psFilename);



            processor.StartProcessing(switches.ToArray(), null);
        }

        return Directory.EnumerateFiles(outputPath).ToList();
    }

OTHER TIPS

I think that the parameters that you are passing into the DrawImage method should be the size you want the image on the paper rather than leaving them by default, the DrawImage command will then take care of the scaling for you. Probably the easiest way is to use the following override of the DrawImage command.

Note: This will skew the image if the proportions of the image are not the same as the rectangle. Some simple math on the size of the image and paper size will allow you to create a new rectangle that fits in the bounds of the paper without skewing the image.

This is what works for me:

int desired_x_dpi = 600;
int desired_y_dpi = 600;

Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);

System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
pd.PrintPage += (sender, args) =>
{
    args.Graphics.DrawImage(img, args.MarginBounds);
};
pd.Print();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top