如何从一个PdfFile使用iTextSharp的PdfReader的UserUnit财产 [英] How to get the UserUnit property from a PdfFile using iTextSharp PdfReader

查看:294
本文介绍了如何从一个PdfFile使用iTextSharp的PdfReader的UserUnit财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大堆的PDF文件 - 我阅读这些按要求转化为字节数组,然后也把它传递给一个iTextSharp的PdfReader实例。我想,然后抢在像素,每个像素页面级的尺寸。从我目前看似乎通过PDF文件点 - 点是存储在名为UserUnit元素某种词典的配置单位工作。

I have a bunch of PDF files- I read these as requested into a byte array and then also pass it to a iTextSharp PdfReader instance. I want to then grab the dimensions of each page- in pixels. From what I've read so far it seems by PDF files work in points- a point being a configurable unit stored in some kind of dictionary in an element called UserUnit.

加载我的PDF文件转换成PdfReader,做什么,我需要做的就是在UserUnit每一页(显然它可以一页一页地而异),这样的话,我可以让页面尺寸以像素为单位。

Loading my PDF File into a PdfReader, what do I need to do to get the UserUnit for each page (apparently it can vary from page to page) so I can then get the page dimensions in pixels.

在present我有这个code,它抓住了尺寸为每个页面中的点 - 猜我只需要UerUnit,然后可以由乘这些尺寸得到像素或类似的东西。

At present I have this code, which grabs the dimensions for each page in "points" - guess I just need the UerUnit, and can then multiply these dimensions by that to get pixels or something similar.

//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;
}

干杯!

推荐答案

请允许我从我的书中引述如下:

Allow me to quote from my book:

iText的在行动 - 第二版,第9页:

iText in Action - Second Edition, page 9:

常见问题 什么是在PDF文档中的测量单位?的大多数测量   在PDF中的pssed在用户空间单位EX $ P $。 ISO-32000-1(第8.3.2.3)告诉我们   默认为单元在默认的用户空间的尺寸(72英寸)是   大致相同的点(角),一个单位被广泛应用于印刷   行业。它是不完全一样;有一点没有统一的定义。   总之,1〜=25.4毫米= 72用户单位(大致相当于72磅)。

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).

在接下来的页面中,我解释说,这是可以改变用户单位的默认值,我加了如何创建具有不同的用户单元页的文档的例子。

On the next page, I explain that it’s possible to change the default value of the user unit, and I add an example on how to create a document with pages that have a different user unit.

现在你的问题:假设你有一个现有的PDF,你如何找到使用哪个用户单位?在我们回答这个问题,我们需要看看ISO-32000-1。

Now for your question: suppose you have an existing PDF, how do you find which user unit was used? Before we answer this, we need to take a look at ISO-32000-1.

在部分7.7.3.3Page对象,你会发现UserUnit在表30的描述,在一个页面参赛对象:

In section 7.7.3.3Page Objects, you'll find the description of UserUnit in Table 30, "Entries in a page object":

(可选; PDF 1.6)的正数给予的尺寸   缺省用户空间单位,在1/72英寸的倍数。的范围   支持的值应是实现相关的。默认值:1.0   (用户空间单位为1/72英寸)。

(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).

此键被引入PDF 1.6;你不会发现它在旧文件。它是可选的,所以你不会总是发现它在每一页的字典。在我的书中,我也解释说,UserUnit关键的最大值为75,000

This key was introduced in PDF 1.6; you won't find it in older files. It's optional, so you won't always find it in every page dictionary. In my book, I also explain that the maximum value of the UserUnit key is 75,000.

现在如何检索此值与iTextSharp的?

您已经有矩形黯淡= reader.GetPageSize(我); 返回媒体框。这可能不是页面的视觉部分的大小。如果有在页面中定义一个裁剪框,观众会表现出更小的尺寸比你所拥有的 XY (但你可能已经知道了)。

You already have Rectangle dim = reader.GetPageSize(i); which returns the MediaBox. This may not be the size of the visual part of the page. If there's a CropBox defined for the page, viewers will show a much smaller size than what you have in xy (but you probably knew that already).

您现在需要的是页字典,这样就可以检索UserUnit键的值:

What you need now is the page dictionary, so that you can retrieve the value of the UserUnit key:

PdfDictionary pageDict = reader.GetPageN(i);
PdfNumber userUnit = pageDict.GetAsNumber(PdfName.USERUNIT);

很多时候userUnit将,但如果不是,您可以使用 userUnit.FloatValue

Most of the times userUnit will be null, but if it isn't you can use userUnit.FloatValue.

这篇关于如何从一个PdfFile使用iTextSharp的PdfReader的UserUnit财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆