【发布时间】:2015-02-21 10:00:11
【问题描述】:
我使用 PDFBox 1.8.8 并尝试向现有 pdf 文件添加新文本。问题是我添加的文本正在为这个特定的 pdf 压缩
original pdf(我认为这个文件是问题的根源)
(它适用于其他 pdf)。
image of problem with explanation
最初并没有显示,但我修复了这个推荐的问题(设置字体显示模式)
content.appendRawCommands("0 Tr ");
我正在使用 pdf 矩阵并检查了几乎所有内容......但看不出我该如何解决这个问题......
content.setTextMatrix(1, 0, 0, 1, 0, 0);
问题:
1) 有什么方法可以强制 pdfbox 使用正常的字体设置并忽略页面上当前的字体选项
2) 有什么方法可以读取当前的字体设置,并且可以使用 content.setTextMatrix(带有高度和宽度)
3) 我的 pdf 中使用什么模式来存档这种转换...
我的代码
private static void writePdfBoxStamp(PDDocument document, int page,
float topLeftX, float topLeftY, float width, float height,
String text, float itemFontSize, int[] color, int rotation) throws IOException {
PDPage pdfPage = (PDPage) document.getDocumentCatalog().getAllPages().get(page);
PDPageContentStream content = new PDPageContentStream(document, pdfPage, true, true);
// COSDictionary pageFonts = pdfPage.getResources().getCOSDictionary();
//border draw
float lineWidth = 2;
float bottomLeftX = topLeftX;
float bottomLeftY = topLeftY - height;
float topRightX = topLeftX + width;
float topRightY = topLeftY;
float radius = 10;
content.setLineWidth(lineWidth);
content.setStrokingColor(new Color(color[0], color[1], color[2]));
content.setNonStrokingColor(new Color(color[0], color[1], color[2]));
PDFont font = PDType1Font.HELVETICA_BOLD;
int fontSize = (int) itemFontSize;
float linesHeight = font.getFontDescriptor().getFontBoundingBox()
.getHeight()
/ 1000 * fontSize;
float txtLineWidth = font.getStringWidth(text)
/ 1000 * fontSize;
content.setFont(font, fontSize);
content.beginText();
content.setFont(font, fontSize);
float posTextX = topLeftX + (width-txtLineWidth)/2;
float posTextY = topLeftY - height/2 - linesHeight/2 + lineWidth;
if (rotation > 0) {
int step = 0;
// clockwise rotation
if (rotation == 90) {
step = 6;
posTextX = topLeftX + (width + linesHeight)/2 - lineWidth;
posTextY = topLeftY - (height + txtLineWidth)/2;
}
if (rotation == 270) {
step = -6;
posTextX = topLeftX + (width - linesHeight/2)/2;
posTextY = topLeftY - (height - txtLineWidth)/2;
}
content.setTextRotation(-step*Math.PI*0.25, posTextX, posTextY);
} else {
content.moveTextPositionByAmount(posTextX,posTextY);
}
content.appendRawCommands("0 Tr "); //set normal text procession
content.drawString(text);
content.endText();
content.stroke();
content.close();
}
【问题讨论】:
-
您可以编辑您的问题以在其中包含链接,有些人不阅读 cmets。