簡體   English   中英

確定PDF頁面上的最大分辨率(DPI)

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

我正在使用GhostScript.Net將PDF光柵化為頁面圖像,然后再將頁面圖像發送到打印機。 我這樣做是為了始終可以將其光柵化為300dpi。 這樣,無論PDF中任何圖像(主要是掃描的PDF)的大小如何,我都可以在合理的時間內打印PDF。

但是,令我驚訝的是,在某些情況下,不需要光柵化高達300dpi。 根據頁面的內容,可以將其光柵化為200dpi甚至100dpi。

是否有人試圖確定PDF頁面內容的最大DPI? 也許使用iTextSharp?

我當前的代碼是這樣的:

        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頁面沒有分辨率。 可以認為其中的圖像具有分辨率,該分辨率由頁面上圖像的寬度除以x方向上圖像樣本的數量得出,頁面上圖像的高度除以x的數量得出。 y方向上的圖像樣本。

這樣就可以計算頁面上圖像的寬度和高度。 這由圖像矩陣給定,由當前變換矩陣修改。 因此,為了計算頁面上的寬度和高度,您需要解釋內容流直至渲染圖像的位置,並跟蹤圖形狀態CTM。

對於一般的PDF文件,唯一的了解方法是使用PDF解釋器。 在整個頁面內容都是一幅圖像的嚴格限制情況下,您可以賭博沒有進行縮放,只需將介質寬度除以圖像寬度,然后將介質高度除以圖像高度即可得到x和y分辨率。

但是,這在一般情況下絕對不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM