Determine the maximum resolution (DPI) on the PDF page

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(); } 
+1
c # dpi ghostscript itextsharp rasterizing
source share
1 answer

PDF pages do not have permission. Images inside them can be considered as having a resolution, which is determined by the width of the image on the page divided by the number of image samples in the x direction and the height of the image on the page divided by the number of Image samples in the y direction.

Thus, it allows you to calculate the width and height of the image on the page. This is determined by the image matrix modified by the current transformation matrix. Therefore, in order to work out the width and height on the page, you need to interpret the content flow to the point where the image is displayed, monitoring the state of CTM graphics.

For shared PDF files, the only way to find out is to use a PDF interpreter. In the strictly limited case when the entire content of the page is a single image, you can gamble so that there is no scaling and simply divide the width of the medium by the width of the image and the height of the medium by the height of the image to give x and y resolutions.

However, this will definitely not work in the general case.

+1
source share

All Articles