我有一堆 PDF 文件 - 我按要求将它们读入字节数组,然后将其传递给 iTextSharp PdfReader 实例。然后我想获取每个页面的尺寸——以像素为单位。从我目前所读的内容来看,PDF 文件似乎以点为单位工作——一个点是一个可配置的单元,存储在某种字典中的一个名为 UserUnit 的元素中。
将我的 PDF 文件加载到 PdfReader 中,我需要做什么来获取每个页面的 UserUnit(显然它可能因页面而异),然后我可以获得以像素为单位的页面尺寸。
目前我有这段代码,它以“点”为单位获取每个页面的尺寸 - 我猜我只需要 UerUnit,然后可以将这些尺寸乘以它以获得像素或类似的东西。
//Create an object to read the PDF
PdfReader reader = new iTextSharp.text.pdf.PdfReader(file_content);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
Rectangle dim = reader.GetPageSize(i);
int[] xy = new int[] { (int)dim.Width, (int)dim.Height }; // returns page size in "points"
page_data[objectid + '-' + i] = xy;
}
干杯!
最佳答案
请允许我引用我的书:
iText 实战 - 第二版,第 9 页:
FAQ What is the measurement unit in PDF documents? Most of the measurements in PDFs are expressed in user space units. ISO-32000-1 (section 8.3.2.3) tells us “the default for the size of the unit in default user space (1/72 inch) is approximately the same as a point (pt), a unit widely used in the printing industry. It is not exactly the same; there is no universal definition of a point.” In short, 1 in. = 25.4 mm = 72 user units (which roughly corresponds to 72 pt).
在下一页,我解释了可以更改用户单位的默认值,并添加了一个示例,说明如何创建包含具有不同用户单位的页面的文档。
现在回答您的问题:假设您有一个现有的 PDF,您如何找到使用了哪个用户单位?在回答这个问题之前,我们需要了解一下 ISO-32000-1。
在第 7.7.3.3 节页面对象中,您将在表 30“页面对象中的条目”中找到对 UserUnit 的描述:
(Optional; PDF 1.6) A positive number that shall give the size of default user space units, in multiples of 1⁄72 inch. The range of supported values shall be implementation-dependent. Default value: 1.0 (user space unit is 1⁄72 inch).
该 key 在 PDF 1.6 中引入;你不会在旧文件中找到它。它是可选的,因此您不会总能在每页词典中找到它。在我的书中,我也解释了 UserUnit 键的最大值是 75,000。
现在如何使用 iTextSharp 检索此值?
您已经有了返回 MediaBox 的 Rectangle dim = reader.GetPageSize(i);
。这可能不是页面可视部分的大小。如果为页面定义了 CropBox,查看器将显示比您在 xy
中拥有的尺寸小得多的尺寸(但您可能已经知道)。
您现在需要的是页面字典,以便您可以检索 UserUnit 键的值:
PdfDictionary pageDict = reader.GetPageN(i);
PdfNumber userUnit = pageDict.GetAsNumber(PdfName.USERUNIT);
大多数时候 userUnit 将为 null
,但如果不是,您可以使用 userUnit.FloatValue
。
关于c# - 如何使用 iTextSharp PdfReader 从 PdfFile 获取 UserUnit 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14586315/