java pdfbox内存溢出,java - 如何使用PDFBox居中文本 - 堆栈内存溢出

这会将居中文本添加到纵向和横向格式的页面中:

void addCenteredText(String text, PDFont font, int fontSize, PDPageContentStream content, PDPage page, Point2D.Float offset) throws IOException {

content.setFont(font, fontSize);

content.beginText();

// Rotate the text according to the page orientation

boolean pageIsLandscape = isLandscape(page);

Point2D.Float pageCenter = getCenter(page);

// We use the text's width to place it at the center of the page

float stringWidth = getStringWidth(text, font, fontSize);

if (pageIsLandscape) {

float textX = pageCenter.x - stringWidth / 2F + offset.x;

float textY = pageCenter.y - offset.y;

// Swap X and Y due to the rotation

content.setTextMatrix(Matrix.getRotateInstance(Math.PI / 2, textY, textX));

} else {

float textX = pageCenter.x - stringWidth / 2F + offset.x;

float textY = pageCenter.y + offset.y;

content.setTextMatrix(Matrix.getTranslateInstance(textX, textY));

}

content.showText(text);

content.endText();

}

boolean isLandscape(PDPage page) {

int rotation = page.getRotation();

final boolean isLandscape;

if (rotation == 90 || rotation == 270) {

isLandscape = true;

} else if (rotation == 0 || rotation == 360 || rotation == 180) {

isLandscape = false;

} else {

LOG.warn("Can only handle pages that are rotated in 90 degree steps. This page is rotated {} degrees. Will treat the page as in portrait format", rotation);

isLandscape = false;

}

return isLandscape;

}

Point2D.Float getCenter(PDPage page) {

PDRectangle pageSize = page.getMediaBox();

boolean rotated = isRotated(page);

float pageWidth = rotated ? pageSize.getHeight() : pageSize.getWidth();

float pageHeight = rotated ? pageSize.getWidth() : pageSize.getHeight();

return new Point2D.Float(pageWidth / 2F, pageHeight / 2F);

}

float getStringWidth(String text, PDFont font, int fontSize) throws IOException {

return font.getStringWidth(text) * fontSize / 1000F;

}

这是您在旋转页面上创建具有居中文本的PDF的方法:

PDDocument pdf = new PDDocument();

// A5 page in landscape format

PDPage page = new PDPage(PDRectangle.A5);

page.setRotation(90);

pdf.addPage(page);

try (PDPageContentStream content = new PDPageContentStream(pdf, page)) {

int fontSize = 36;

// Put the text at the page's center, no offset

Point2D.Float center = new Point2D.Float(0, 0);

addCenteredText("PDFBox", PDType1Font.HELVETICA_BOLD, fontSize, content, page, center);

// Put the text centered at the lower end of the page

Point2D.Float lowerCenter = new Point2D.Float(0, -165);

addCenteredText("Hi there!", PDType1Font.HELVETICA, fontSize, content, page, lowerCenter);

} catch (IOException e) {

LOG.warn("Exception while creating content", e);

}

结果PDF:

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9CSjF0UC5wbmc=

我使用PDFBox 2.0.0-RC2来创建这个PDF。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值