PDFbox 中的计算字符串宽度似乎只计算字符? [英] Calculation String-Width in PDFbox seems only to count characters?

查看:48
本文介绍了PDFbox 中的计算字符串宽度似乎只计算字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题.我尝试计算 PDFbox 中字符串的宽度,使其以矩形为中心.现在我的问题是,12 的宽度与 32 的宽度相同,但 1 小于 3,因此我的文本是 12 不居中.我该如何解决这个问题?

I've the following issue. I try to calculate the width of a string in PDFbox to center it in a rectangle. Now my problem is, that I get the same width for 12 as for 32, but the 1 is smaller than the 3 so that my text is for 12 not centered. How can I solve this issue?

谢谢!

推荐答案

来自 这个问题:

PDFont font = PDType1Font.HELVETICA_BOLD; // Or whatever font you want.
int fontSize = 16; // Or whatever font size you want.
float width = font.getStringWidth(text.substring(start,i)) / 1000 * fontSize;

这篇关于PDFbox 中的计算字符串宽度似乎只计算字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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