文字在生成的pdf中是相反的 [英] Text is reverse in generated pdf

查看:201
本文介绍了文字在生成的pdf中是相反的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pdfbox在pdf文件中添加一行.但是我要添加的文字是相反的.

I am using pdfbox to add a line to pdf file. but the text i am adding is reversed.

File file = new File(filePath);
PDDocument document = PDDocument.load(file);

PDPage page = document.getPage(0);
PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true);

int stampFontSize = grailsApplication.config.pdfStamp.stampFontSize ? grailsApplication.config.pdfStamp.stampFontSize : 20
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_ROMAN, stampFontSize);

int leftOffset = grailsApplication.config.pdfStamp.leftOffset ? grailsApplication.config.pdfStamp.leftOffset : 10
int bottomOffset = grailsApplication.config.pdfStamp.bottomOffset ? grailsApplication.config.pdfStamp.bottomOffset : 20
contentStream.moveTextPositionByAmount(grailsApplication.config.xMove,grailsApplication.config.yMove)
contentStream.newLineAtOffset(leftOffset, bottomOffset)

String text = "i have added this line...!!!!";
contentStream.showText(text);
contentStream.endText();

contentStream.close();

document.save(new File(filePath));
document.close();

byte[] pdfData;
pdfData = Files.readAllBytes(file.toPath());
return pdfData;

我尝试使用moveTextPositionByAmount方法,但这似乎对文本没有任何影响.为什么我的文字被颠倒了,我如何将其设置为正确的方向.

i tried using moveTextPositionByAmount method but this does not seem to have any effect on text. why is my text reversed and how can i set it to correct orientation.

推荐答案

您的代码本身并不是导致镜像输出的原因,因此原因必须在您要加盖印章的PDF中.不幸的是,您没有提供相关的PDF,因此我们必须在这里进行猜测.

Your code is not causing the mirrored output by itself, so the cause must be inside the PDF you are stamping. Unfortunately you did not provide the PDF in question, so we have to guess here.

该问题很可能是由预先存在的页面内容引起的,该页面内容已将当前转换矩阵设置为仿射仿射转换而没有在最后将其重置.

Most likely the issue is caused by the pre-existing page content having set the current transformation matrix to a mirroring affine transformation without resetting it at the end.

如果确实是这样,PDFBox提供了一种简单的解决方法:

If that indeed is the case, PDFBox provides an easy work-around:

您可以这样构造PDPageContentStream:

PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true);

还有另一个接受附加boolean参数的构造函数.如果使用该构造函数将其他参数设置为true,则PDFBox会尝试重置内容的图形状态:

There is another constructor accepting an additional boolean argument. If you use that constructor setting the additional argument to true, PDFBox attempts to reset the graphics state of the content:

PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true,true);

当心::如果这确实可以解决问题,则当前使用的坐标和偏移量将取决于更改的转换矩阵.在这种情况下,您将必须对其进行相应的更新.

Beware: If this indeed fixes the issue, the coordinates and offsets you currently use rely on the transformation matrix being changed as it is. In that case you will have to update them accordingly.

或者,引入反镜像可能会有所帮助,例如通过在每个文本对象的开头设置这样的文本矩阵:

Alternatively introducing a counter-mirroring may help, e.g. by setting the text matrix like this at the start of each of your text objects:

contentStream.beginText();
contentStream.setTextMatrix(new Matrix(1f, 0f, 0f, -1f, 0f, 0f));

此后,所有 y 坐标更改都需要取消,尤其是contentStream.moveTextPositionByAmountcontentStream.newLineAtOffset的第二个参数.

Thereafter all y coordinate changes need to be negated, in particular the second argument of contentStream.moveTextPositionByAmount and contentStream.newLineAtOffset.

(顺便说一句,moveTextPositionByAmountnewLineAtOffset做同样的事情,前者只是不推荐使用的变体,因此您可能希望在两种情况下都使用后者.)

(By the way, moveTextPositionByAmount and newLineAtOffset do the same, the former merely is the deprecated variant, so you might want to use the latter in both cases.)

这篇关于文字在生成的pdf中是相反的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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