问题描述
在将页面图像发送到打印机之前,我正在使用GhostScript.Net将PDF格式化为页面图像。我这样做是为了让我可以一直光栅化到300dpi。这允许我在合理的时间内打印PDF,无论PDF中的任何图像的大小(主要是扫描的PDF)。
I am using GhostScript.Net to rasterize PDF to page images before sending the page images to the printer. I am doing this so that I can always rasterize to 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).
然而,这让我感到震惊在某些情况下,不需要光栅化高达300dpi。根据页面内容,可以栅格化为200dpi甚至100dpi。
However, it strikes me that in some cases there will not be a need to rasterize as high as 300dpi. It may be possible to rasterize to 200dpi or even 100dpi depending on the content of the page.
是否有人试图确定PDF页面内容的最大DPI?也许使用iTextSharp?
Has anyone attempted to determine the maximum DPI for the content of a PDF page? Perhaps using 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方向上的图像样本的数量,以及页面上的图像的高度除以数量。 y方向上的图像样本。
PDF pages don't have a resolution. 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.
因此,这将计算页面上图像的宽度和高度。这由图像矩阵给出,由电流变换矩阵修改。因此,为了计算页面上的宽度和高度,您需要将内容流解释为渲染图像的点,跟踪图形状态CTM。
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.
对于一般PDF文件,了解这一点的唯一方法是使用PDF解释器。在严格限制的情况下,整个页面内容是单个图像,您可以赌博没有缩放发生并简单地将媒体宽度除以图像宽度,并将媒体高度除以图像高度以给出x和y分辨率。
For general PDF files, the only way to know this is to use a PDF interpreter. 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.
然而,这绝对不适用于一般情况。
However this definitely won't work in the general case.
这篇关于确定PDF页面上的最大分辨率(DPI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!