PDFBox将英寸或厘米转换为坐标系 [英] PDFBox converting inches or centimeters into the coordinate system

查看:413
本文介绍了PDFBox将英寸或厘米转换为坐标系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PDFBox(和PDF生成)的新手,我很难生成自己的PDF.

I am new to PDFBox (and PDF generation) and I am having difficulty to generate my own PDF.

我的文本确实具有以英寸/厘米为单位的特定坐标,因此我需要将其转换为PDFBox使用的单位.有什么建议/实用工具可以自动执行此操作吗?

I do have text with certain coordinates in inches/centimeters and I need to convert them to the units PDFBox uses. Any suggestions/utilities than can do this automatically?

PDPageContentStream.moveTextPositionByAmount(x,y)对我来说毫无意义.

推荐答案

通常,PDFBox在创建PDF时会使用PDF用户空间坐标.这意味着:

In general PDFBox uses the PDF user space coordinates when creating a PDF. This means:

  1. 页面的坐标由其 CropBox 分隔,默认为 MediaBox ,其值从左到右和从下到上递增.因此,如果使用new PDPage()new PDPage(PDPage.PAGE_SIZE_*)创建页面,则坐标系的原点将从页面的左下角开始.

  1. The coordinates of a page are delimited by its CropBox defaulting to its MediaBox, the values increasing left to right and bottom to top. Thus, if you create a page using new PDPage() or new PDPage(PDPage.PAGE_SIZE_*) the origin of the coordinate system starts in the lower left corner of the page.

用户空间中的单位开始为默认用户空间单位,该默认空间由页面的 UserUnit 定义.最常见的情况是(例如,如果您使用任何PDPage构造函数创建页面并且未明确更改该值),则未明确设置该页面,因此其默认行距为1⁄72英寸.

The unit in user space starts as the default user space unit which is defined by the UserUnit of the page. Most often (e.g. if you create a page using any of the PDPage constructors and don't explicitly change that value) it is not explicitly set and, therefore, its default kicks in which is 1⁄72 inch.

可以通过级联任意更改用户空间坐标系 一些矩阵到当前的变换矩阵.当前的变换矩阵从恒等矩阵开始.

The user space coordinate system can be changed pretty arbitrarily by concatenating some matrix to the current transformation matrix. The current transformation matrix starts as the identity matrix.

在PDFBox中,您可以使用PDPageContentStream.concatenate2CTM()重载之一进行此操作.

In PDFBox you do this using one of the PDPageContentStream.concatenate2CTM() overloads.

使用PDPageContentStream.beginText()切换到文本模式后,使用的坐标系还会受到文本矩阵引入的变换的影响.

As soon as you switch to text mode using PDPageContentStream.beginText(), the coordinate system used is furthermore influenced by the transformation introduced by the text matrix.

在PDFBox中,您可以使用PDPageContentStream.setTextMatrix()重载之一设置文本矩阵.

In PDFBox you set the text matrix using one of the PDPageContentStream.setTextMatrix() overloads.

由于您不熟悉PDFBox(如您所说),而又不熟悉PDF(如我所假定的那样,因为否则您可能已经识别出坐标),因此我建议您在开始时尽可能避免使用转换,因此,保持为坐标系从左下角开始,既不旋转也不歪斜且单位长度为1/72英寸的状态.

As you are new to PDFBox (as you say) and new to PDF in general (as I presume because otherwise you would likely have recognized the coordinates), I would advise you to initially refrain from using transformations wherever possible and, therefore, remain in state where the coordinate system starts in the lower left, is neither rotated nor skewed, and has a unit length of 1/72 inch.

对于这种情况,您实际上可以使用PDFBox提供的常量进行转换:

For this context you actually can use constants provided by PDFBox for conversion:

  • 将英寸坐标乘以PDPage.DEFAULT_USER_SPACE_UNIT_DPI以获取默认的用户空间坐标.
  • 将以毫米为单位的坐标乘以PDPage.MM_TO_UNITS以获取默认的用户空间坐标.
  • Multiply coordinates in inch by PDPage.DEFAULT_USER_SPACE_UNIT_DPI to get default user space coordinates.
  • Multiply coordinates in mm by PDPage.MM_TO_UNITS to get default user space coordinates.

但是,如果您想玩坐标,请查看PDF规范

If you want to have fun with coordinates, though, look at the PDF specification ISO-32000-1 and study the sections 8.3 Coordinate Systems and 9.4.4 Text Space Details.

上面提到的PDPage常量以前在早期的PDFBox 1.8.x版本中都可以访问,但是后来被隐藏了(private),最终在过渡到PDFBox 2.x时被删除了.

The PDPage constants pointed to above used to be accessible in early PDFBox 1.8.x versions but then got hidden (private), and eventually were removed in the transition to PDFBox 2.x.

作为参考,常量定义为

private static final int DEFAULT_USER_SPACE_UNIT_DPI = 72;

private static final float MM_TO_UNITS = 1/(10*2.54f)*DEFAULT_USER_SPACE_UNIT_DPI;

这篇关于PDFBox将英寸或厘米转换为坐标系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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