开发者

PDF Calculating the Height & Width of a drawed image

开发者 https://www.devze.com 2023-02-04 07:37 出处:网络
My program reads PDF files and needs to know which size (in user space units) an image drawed to a page has.

My program reads PDF files and needs to know which size (in user space units) an image drawed to a page has.

Can someone help me with the following example?

MediaBox = [0 0 612 792] (Page size in user space units) Original Size (W/H) = 155/76 (As specified in the image dictionary)

Image draw instructions:

q 74.400131 0 0 -36.000061 249.100266 224.240173 cm /Im1 Do

Q

So the values a-f are:

a = 74.400131

b = 0

c = 0

d = -36.000061

e = 249.100266

f = 224.240173

Which results in the following Matrix:

74.400131 0 0

0 -36.000061 0

249.100266 224.240173 1

The following formulas are also given:

x′ = a × x + c × y + e y′ = b × x + d × y + f

So, what I thought to do now, is to calculate the coordinates from the TopLeft-, TopRight- and BottomLeft-corners 开发者_JS百科using the formulas above and then use this formula:

w = TopRightX-TopLeftX h = BottomLeftY-TopLeftY

BUT: How does calculating those coordinates work? I tried this to calculate the BottomRight corner for example:

X = 74.400131*155+0*76+249.100266 = 11`781.120

This result can´t be correct...

Can someone help me calculating it right?

Thanks for your help!


You forgot transformation from the user space into device space. Matrix from the picture is concatenated with previous matrix (which usually represent page size and zoom)

Values in the image matrix are calculated as follow

width        0             0
  0       height           0
  x   page_h-(y+height)    1


In PDF an image is considered to be 1x1 in user space units. The transformation matrix is used to scale the image to be displayed at the desired size. The a and d elements in the transformation matrix represent the scaling factors so in your situation the size (in user space units) of the displayed image is 74.400131*36.000061 points (the default user space units). Your example is the simplest situation because I assume there is nothing else before the q operator. But in PDF the transformation matrices are concatenated so you have to execute the entire content stream in order to see the actual transformation matrix that is in place when the image is displayed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号