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

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

Thanks.

解决方案

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

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.

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.

Drawing rectangles and test

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

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

0ERJj.png

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.

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

Furthermore the OP was unable to share the document.

Therefore, it could not be found out whether or not those negative coordinates make sense for his special 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...

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?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值