我正在使用 pdfbox 在 pdf 文件中添加一行。但我添加的文字是相反的。
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;
最佳答案
您的代码本身不会导致镜像输出,因此原因必须在您正在加盖的 PDF 内。不幸的是,您没有提供相关的 PDF,因此我们必须在这里猜测。
最有可能的问题是由于预先存在的页面内容将当前变换矩阵设置为镜像仿射变换而没有在最后重置它。
如果确实如此,PDFBox 提供了一个简单的解决方法:
您可以像这样构建 PDPageContentStream
:
PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true);
boolean
参数。如果您使用该构造函数将附加参数设置为 true
,PDFBox 会尝试重置内容的图形状态:PDPageContentStream contentStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,true,true);
contentStream.beginText();
contentStream.setTextMatrix(new Matrix(1f, 0f, 0f, -1f, 0f, 0f));
contentStream.moveTextPositionByAmount
和 contentStream.newLineAtOffset
的第二个参数。moveTextPositionByAmount
和 newLineAtOffset
的作用相同,前者只是不推荐使用的变体,因此您可能希望在两种情况下都使用后者。)
https://stackoverflow.com/questions/46924286/