简体   繁体   English

如何使用pdfbox在Java的pdf页面上的特定位置绘制字符串?

[英]How to draw a string at a specific position on a pdf page in java using pdfbox?

I have a pdf coordinate (x, y) as input . 我有一个pdf坐标(x,y)作为输入。 I need to draw a string at the given input coordinate[Eg :- (x,y)=(200,250)]. 我需要在给定的输入坐标处绘制字符串[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(). 我正在使用pdfbox,当我使用以下方法moveTextPositionByAmount时,我没有得到确切的位置。即使我已经尝试过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();

Thanks. 谢谢。

Getting rid of graphic state changes from the existing page content 从现有页面内容中摆脱图形状态更改

You use the PDPageContentStream constructor with two boolean arguments: 您将PDPageContentStream构造函数与两个boolean参数一起使用:

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

This constructor is implemented as: 该构造函数实现为:

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

ie it calls the constructor with three boolean arguments using false for the final one. 即,它使用三个boolean参数调用构造函数,最后一个使用false This final boolean argument is documented as: 最终的boolean参数记录为:

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

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: 为了防止这种情况的发生,您应该将PDPageContentStream构造函数与三个boolean参数一起使用:

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

Using this one can easily position text. 使用此功能可以轻松定位文本。

Drawing rectangles and test 绘制矩形并测试

The OP mentioned that he was successful drawing rectangles but not drawing text. OP提到他成功绘制了矩形,但没有绘制文本。

The following code 以下代码

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

results in 结果是

结果截图

as would be expected. 不出所料。

Meaning of input coordinates must be explained 必须解释输入坐标的含义

The OP also mentioned X:-121,Y:-305,W:-262,h:-104 as coordinates from external application in his comments. OP在其评论中还提到了X:-121,Y:-305,W:-262,h:-104作为外部应用程序的坐标

As PDFs most often have positive coordinates inside the media box, these X and Y coordinates make no sense for PDFs in general. 由于PDF通常在媒体框内具有正坐标,因此这些X和Y坐标通常对PDF毫无意义。

Furthermore the OP was unable to share the document . 此外,OP 无法共享文档

Therefore, it could not be found out whether or not those negative coordinates make sense for his special PDF. 因此,无法确定这些负坐标对于他的特殊PDF是否有意义。

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开头但要以X结尾,或文本应被镜像,或,或,或,...

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? 这是这些坐标的原点,是上方或下方的正y坐标,是矩形左下角的X,Y,负宽度或高度的含义是什么,相对于X,Y表示字符串为画?

I found that this one worked for me . 我发现这对我有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM