I use GhostScript.Net to rasterize PDF images on a page before sending page images to a printer. I do it so that I can always rasterize up to 300 dpi. This allows me to print PDF in a reasonable amount of time regardless of the size of any image in PDF (mostly scanned PDF files).
However, it seems to me that in some cases there is no need to rasterize up to 300 dpi. Depending on the contents of the page, up to 200 dpi or even 100 dpi can be rasterized.
Has anyone tried to determine the maximum DPI for the contents of a PDF page? Perhaps using iTextSharp?
My current code is:
var dpiList = new List<int> {50, 100, 150, 200, 250, 300, 350, 400, 450, 500}; string inputPdfPath = @"C:\10page.pdf"; string outputPath = @"C:\Print\"; var lastInstalledVersion = GhostscriptVersionInfo.GetLastInstalledVersion( GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL); var rasterizer = new GhostscriptRasterizer(); rasterizer.Open(inputPdfPath, lastInstalledVersion, true); var imageFiles = new List<string>(); for (int pageNumber = 1; pageNumber <= 10; pageNumber++) { foreach (var dpi in dpiList) { string pageFilePath = System.IO.Path.Combine(outputPath, string.Format("{0}-{1}-{2}.png", pageNumber, Guid.NewGuid().ToString("N").Substring(0, 8), dpi)); System.Drawing.Image img = rasterizer.GetPage(dpi, dpi, pageNumber); img.Save(pageFilePath, ImageFormat.Png); imageFiles.Add(pageFilePath); Console.WriteLine(pageFilePath); } } var imageCount = 0; var pd = new PrintDocument(); pd.PrintPage += delegate(object o, PrintPageEventArgs args) { var i = System.Drawing.Image.FromFile(imageFiles[imageCount]); var pageBounds = args.PageBounds; var margin = 48; var imageBounds = new System.Drawing.Rectangle { Height = pageBounds.Height - margin, Width = pageBounds.Width - margin, Location = new System.Drawing.Point(margin / 2, margin / 2) }; args.Graphics.DrawImage(i, imageBounds); imageCount++; }; foreach (var imagefile in imageFiles) { pd.Print(); }
c # dpi ghostscript itextsharp rasterizing
Paul
source share