如何使用pdfbox在java的pdf页面上的特定位置绘制字符串? [英] How to draw a string at a specific position on a pdf page in java using pdfbox?

查看:651
本文介绍了如何使用pdfbox在java的pdf页面上的特定位置绘制字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pdf坐标(x,y)作为输入.我需要在给定的输入坐标处绘制一个字符串[Eg:-(x,y)=(200,250)].我正在使用pdfbox,当我使用以下方法moveTextPositionByAmount时,我没有得到确切的位置.即使我已经尝试过moveTo().请帮我如何在精确的位置画线?

I have a pdf coordinate (x, y) as input . I need to draw a string at the given input coordinate[Eg :- (x,y)=(200,250)]. I am using pdfbox , When I am using the below method moveTextPositionByAmount I am not getting the exact position.Even i have tried with moveTo(). Please help me how to draw the string at an exact position ?

PDPageContentStream contentStream = new PDPageContentStream(document, page,true,true);
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
contentStream.moveTextPositionByAmount(xindex, yindex);
contentStream.setNonStrokingColor(color);
contentStream.drawString(comment);                      
contentStream.stroke();
contentStream.endText();

谢谢.

推荐答案

摆脱现有页面内容的图形状态更改

您将PDPageContentStream构造函数与两个boolean参数一起使用:

Getting rid of graphic state changes from the existing page content

You use the PDPageContentStream constructor with two boolean arguments:

new PDPageContentStream(document, page,true,true);

此构造函数的实现方式为:

This constructor is implemented as:

this(document, sourcePage, appendContent, compress, false);

即它使用最后三个false调用带有三个boolean自变量的构造函数.最终的boolean自变量记录为:

i.e. it calls the constructor with three boolean arguments using false for the final one. This final boolean argument is documented as:

* @param resetContext Tell if the graphic context should be reseted.

因此,无需附加图形上下文即可将其添加到页面内容.这意味着在现有页面内容中对当前转换矩阵所做的任何更改仍会转换您的坐标.为了防止这种情况的发生,您应该将PDPageContentStream构造函数与三个boolean参数一起使用:

Thus, you append to the page content without resetting the graphic context. This means that any changes to the current transformation matrix done in the existing page content still transforms your coordinates. To prevent that from happening you should use the PDPageContentStream constructor with three boolean arguments:

new PDPageContentStream(document, page, true, true, true);

使用此按钮可以轻松定位文本.

Using this one can easily position text.

OP提到他成功绘制了矩形,但没有绘制文本.

The OP mentioned that he was successful drawing rectangles but not drawing text.

以下代码

PDPage firstPage = allPages.get(0);
PDRectangle pageSize = firstPage.findMediaBox();

float x = 121;
float y = 305;
float w = 262;
float h = 104;

PDPageContentStream contentStream = new PDPageContentStream(document, firstPage, true, true, true);

contentStream.setNonStrokingColor(Color.yellow);
contentStream.fillRect(pageSize.getLowerLeftX() + x, pageSize.getLowerLeftY() + y, w, h);

contentStream.beginText();
contentStream.moveTextPositionByAmount(pageSize.getLowerLeftX() + x, pageSize.getLowerLeftY() + y);
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
contentStream.setNonStrokingColor(Color.red);
contentStream.drawString("My Text Here");
contentStream.endText();
contentStream.close();

产生

如预期的那样.

OP在评论中还提到了 X:-121,Y:-305,W:-262,h:-104 作为来自外部应用程序的坐标.

The OP also mentioned X:-121,Y:-305,W:-262,h:-104 as coordinates from external application in his comments.

由于PDF最常在媒体框内使用正坐标,因此这些X和Y坐标通常对PDF毫无意义.

As PDFs most often have positive coordinates inside the media box, these X and Y coordinates make no sense for PDFs in general.

此外,OP 无法共享文档.

因此,无法确定这些负坐标是否适合他的特殊PDF.

Therefore, it could not be found out whether or not those negative coordinates make sense for his special PDF.

矩形的绘制操作还接受宽度和高度的负值,但是如果用于文本,它们可能表示Y坐标不表示基线,或者不希望文本以X开头但以X开头结束于此,或者文本将被镜像,或者,或者,或者,或者...

Additionally negative values for widths and height are accepted by the rectangle drawing operations, but if used for text, they might imply that the Y coordinate does not denote the baseline, or that the text is not expected to start at X but to end there, or that the text shall be mirrored, or, or, or...

因此,必须首先解释这些负坐标和尺寸的含义. 是这些坐标的原点,是上方或下方的正y坐标,是矩形左下角的X,Y,负宽度或高度的含义是什么,相对于X,Y应为绘制字符串?

Thus, the meaning of those negative coordinates and dimensions must first be explained. Which is the origin of those coordinates, are the positive y coordinates above or below, is X,Y the lower left of the rectangle, what is the meaning of a negative width or height, where in relation to X, Y shall the string be drawn?

这篇关于如何使用pdfbox在java的pdf页面上的特定位置绘制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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