java - 如何使用pdfbox获取曲线的当前位置和位置

标签 java pdf pdf-generation pdfbox

我正在尝试使用 pdfbox 获取立方贝塞尔曲线的位置。 我试图拦截“c”操作并试图通过从列表中提取它们来获取曲线的参数。我在这里有两个问题。首先,当前位置始终是 (0,0)。要获取我使用的当前位置

float x = getGraphicsState().getCurrentTransformationMatrix().getXPosition();
float y = getGraphicsState().getCurrentTransformationMatrix().getYPosition();

当前的变换矩阵始终是一个单位矩阵。我应该做一个 AffineTransform 吗?但是去哪儿呢? 第二个是“c”操作的参数大于页面尺寸。我应该将它们除以 x 和 y 比例吗?

最佳答案

背景

通过考虑当前变换矩阵,您做了一件必要的事情,但还有另一个需要考虑的数据,当前路径当前点。比照。 specification :

The path currently under construction is called the current path. In PDF (unlike PostScript), the current path is not part of the graphics state and is not saved and restored along with the other graphics state parameters. PDF paths shall be strictly internal objects with no explicit representation. After the current path has been painted, it shall become no longer defined; there is then no current path until a new one is begun with the m or re operator.

The trailing endpoint of the segment most recently added to the current path is referred to as the current point. If the current path is empty, the current point shall be undefined. Most operators that add a segment to the current path start at the current point; if the current point is undefined, an error shall be generated.

[...]

x1 y1 x2 y2 x3 y3 c Append a cubic Bézier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x2, y2) as the Bézier control points (see 8.5.2.2, "Cubic Bézier Curves"). The new current point shall be (x3, y3).

(section 8.5.2 Path Construction Operators, page 132)

例如看看这个

3 0 0 3 300 300 cm
10 0 m
10 5.52 5.52 10 0 10 c
-5.52 10 -10 5.52 -10 0 c
-10 -5.52 -5.52 -10 0 -10 c
5.52 -10 10 -5.52 10 0 c
S 

mcS 操作围绕原点绘制一个直径为 10 的圆:

  • 10 0 m当前点移动到(10, 0);
  • 10 5.52 5.52 10 0 10 c 添加从当前点到(0, 10)的四分之一圆路径元素;现在当前点是 (0, 10);
  • -5.52 10 -10 5.52 -10 0 c 添加从当前点到(-10, 0)的四分之一圆路径元素;现在当前点是 (-10, 0);
  • -10 -5.52 -5.52 -10 0 -10 c 添加从当前点到(0, -10)的四分之一圆路径元素;现在当前点是(0, -10);
  • 5.52 -10 10 -5.52 10 0 c 添加从当前点 到 (10, 0) 结束圆的四分之一圆路径元素;现在当前点是 (10, 0);
  • S 沿着给定路径元素定义的路径绘制,即圆线。

但由于之前设置的变换矩阵 3 0 0 3 300 300 cm 缩放了 3 倍并将缩放后的内容移动到 (300, 300),所以圆圈实际上是围绕(300, 300),半径为 30。

您可以通过乘法得到最终坐标,例如对于第一个圆弧段 (10, 0) 的起点:

             ┌   3   0   0 ┐
[ 10 0 1 ] x │   0   3   0 │ = [ 330 300 1]
             └ 300 300   1 ┘

所以它实际上是 (330, 300)。

你的任务

因此,要找到曲线的坐标,您必须关注的不仅仅是c 操作。相反,您必须使用 mre 运算符找到路径构建过程的开始位置,并跟踪当前点。当您到达您感兴趣的曲线时,它从当前转换点开始,到明确给定的转换曲线结束点结束。

根据您的代码,转换矩阵可能由框架提供,或者您可能还必须跟踪它。

如果您通过使用派生自 PDFStreamEngine 的某个类来拦截“c”操作,您可以让该引擎通过注册适当的 OperatorProcessors.

另一方面,如果您处理 PDFStreamParser.getTokens() 返回的列表,则必须手动跟踪它。

无论哪种情况,重要的操作是:

q Save the current graphics state on the graphics state stack (see 8.4.2, "Graphics State Stack").

Q Restore the graphics state by removing the most recently saved state from the stack and making it the current state (see 8.4.2, "Graphics State Stack").

a b c d e f cm Modify the current transformation matrix (CTM) by concatenating the specified matrix (see 8.3.2, "Coordinate Spaces"). Although the operands specify a matrix, they shall be written as six separate numbers, not as an array.

(section 8.4.4 Graphics State Operators, page 127)

关于java - 如何使用pdfbox获取曲线的当前位置和位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20318681/

相关文章:

html - 将 PDF 显示为 HTML 表单

pdf - 在 PDF 中有 2 个位图分辨率

java - 如何打印使用 iText 创建的 PDF?

java - 如何使用可执行 Jar 包含文本文件

java - 没有 System.out 怎么办,在控制台上打印?

Java - 将字节转换为字符串并与另一个进行比较

ruby-on-rails - Wicked pdf 在两页中呈现最后一行

java - Hibernate 为带有 JoinTable 的可选双向 OneToOne 生成错误查询

html - 在 HTML 中嵌入矢量化 PDF 图像

ios - 在 iOS 默认 pdf 阅读器上查看时,PDF4NET pdf 不显示绑定(bind)内容