简体   繁体   English

确定PDF页面上的最大分辨率(DPI)

[英]Determine the max resolution (DPI) on a PDF page

I am using GhostScript.Net to rasterize PDF to page images before sending the page images to the printer. 我正在使用GhostScript.Net将PDF光栅化为页面图像,然后再将页面图像发送到打印机。 I am doing this so that I can always rasterize to 300dpi. 我这样做是为了始终可以将其光栅化为300dpi。 This allows me to print the PDF in a reasonable amount of time regardless of the size of any image in the PDF (mainly scanned PDFs). 这样,无论PDF中任何图像(主要是扫描的PDF)的大小如何,我都可以在合理的时间内打印PDF。

However, it strikes me that in some cases there will not be a need to rasterize as high as 300dpi. 但是,令我惊讶的是,在某些情况下,不需要光栅化高达300dpi。 It may be possible to rasterize to 200dpi or even 100dpi depending on the content of the page. 根据页面的内容,可以将其光栅化为200dpi甚至100dpi。

Has anyone attempted to determine the maximum DPI for the content of a PDF page? 是否有人试图确定PDF页面内容的最大DPI? Perhaps using iTextSharp? 也许使用iTextSharp?

My current code is this: 我当前的代码是这样的:

        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();
        }

PDF pages don't have a resolution. PDF页面没有分辨率。 Images within them can be considered to have a resolution, which is given 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. 可以认为其中的图像具有分辨率,该分辨率由页面上图像的宽度除以x方向上图像样本的数量得出,页面上图像的高度除以x的数量得出。 y方向上的图像样本。

So this leaves calculating the width and height of the image on the page. 这样就可以计算页面上图像的宽度和高度。 This is given by the image matrix, modified by the Current Transformation Matrix. 这由图像矩阵给定,由当前变换矩阵修改。 So in order to work out the width and height on the page, you need to interpret the content stream up to the point where the image is rendered, tracking the graphics state CTM. 因此,为了计算页面上的宽度和高度,您需要解释内容流直至渲染图像的位置,并跟踪图形状态CTM。

For general PDF files, the only way to know this is to use a PDF interpreter. 对于一般的PDF文件,唯一的了解方法是使用PDF解释器。 In the strictly limited case where the whole page content is a single image you can gamble that there is no scaling taking place and simply divide the media width by the image width, and the media height by the image height to give the x and y resolutions. 在整个页面内容都是一幅图像的严格限制情况下,您可以赌博没有进行缩放,只需将介质宽度除以图像宽度,然后将介质高度除以图像高度即可得到x和y分辨率。

However this definitely won't work in the general case. 但是,这在一般情况下绝对不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM