Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java PDFBOX找不到包含特定字符的像素数_Java_Pdfbox - Fatal编程技术网

Java PDFBOX找不到包含特定字符的像素数

Java PDFBOX找不到包含特定字符的像素数,java,pdfbox,Java,Pdfbox,我正在使用PDFBOX创建pdf。在pdfbox中是否有任何函数可以给出以像素为单位的字体大小?例如,字母A和A将占用不同的打印空间。其中显然A比A需要更多像素。我怎样才能找到一个字符或一个单词的像素数呢?首先,像素的概念有点模糊。 通常,文档具有一定的大小,例如英寸/厘米等 JavadocsforPDFBox显示PDFont有几种方法来确定字符串或字符的宽度。 以这些页面为例: 这些单位是一个单位的1/1000。也看到这个 举一个完整的例子: float fontSize = 12; S

我正在使用
PDFBOX
创建pdf。在pdfbox中是否有任何函数可以给出以像素为单位的字体大小?例如,字母
A
A
将占用不同的打印空间。其中显然
A
A
需要更多像素。我怎样才能找到一个字符或一个单词的像素数呢?

首先,像素的概念有点模糊。 通常,文档具有一定的大小,例如英寸/厘米等

JavadocsforPDFBox显示PDFont有几种方法来确定字符串或字符的宽度。 以这些页面为例:

这些单位是一个单位的1/1000。也看到这个

举一个完整的例子:

float fontSize = 12;
String text = "a";

PDRectangle pageSize = PDRectangle.A4;
PDFont font = PDType1Font.HELVETICA_BOLD;


PDDocument doc = new PDDocument();
PDPage page = new PDPage(pageSize);
doc.addPage(page);

PDPageContentStream stream = new PDPageContentStream(doc,page);
stream.setFont( font, fontSize );

// charWidth is in points multiplied by 1000.
double charWidth = font.getStringWidth(text);
charWidth *= fontSize; // adjust for font-size.

stream.beginText();
stream.moveTextPositionByAmount(0,10);

float widthLeft = pageSize.getWidth();
widthLeft *= 1000.0; //due to charWidth being x1000.

while(widthLeft > charWidth){
    stream.showText(text);
    widthLeft -= charWidth;
}

stream.close();
// Save the results and ensure that the document is properly closed:
doc.save( "example.pdf");
doc.close();

首先,像素的概念有点模糊。 通常,文档具有一定的大小,例如英寸/厘米等

JavadocsforPDFBox显示PDFont有几种方法来确定字符串或字符的宽度。 以这些页面为例:

这些单位是一个单位的1/1000。也看到这个

举一个完整的例子:

float fontSize = 12;
String text = "a";

PDRectangle pageSize = PDRectangle.A4;
PDFont font = PDType1Font.HELVETICA_BOLD;


PDDocument doc = new PDDocument();
PDPage page = new PDPage(pageSize);
doc.addPage(page);

PDPageContentStream stream = new PDPageContentStream(doc,page);
stream.setFont( font, fontSize );

// charWidth is in points multiplied by 1000.
double charWidth = font.getStringWidth(text);
charWidth *= fontSize; // adjust for font-size.

stream.beginText();
stream.moveTextPositionByAmount(0,10);

float widthLeft = pageSize.getWidth();
widthLeft *= 1000.0; //due to charWidth being x1000.

while(widthLeft > charWidth){
    stream.showText(text);
    widthLeft -= charWidth;
}

stream.close();
// Save the results and ensure that the document is properly closed:
doc.save( "example.pdf");
doc.close();

您尝试过PDFont.getStringWidth()吗?它以1/1000单位的文本空间返回字符串的宽度。您尝试过PDFont.getStringWidth()吗?它以1/1000单位的文本空间返回字符串的宽度。可以说像厘米、英寸或毫米这样的单位是
getStringWidth(字符串文本)
返回的吗?@@Tilman Hausher is pdfbox遵循所有单位。像页面宽度和所有单位返回一样?各种方法返回各种类型的单位。例如,getStringWidth是宽度的1/1000,而页面是1x。最好的方法是仔细阅读javadocs。您可以说像cm、inch或mm这样的单位返回多少?@@code>getStringWidth(字符串文本)返回多少?@@Tilman Hausher is pdfbox以单位跟随所有单位。像页面宽度和所有单位返回一样?各种方法返回各种类型的单位。例如,getStringWidth是宽度的1/1000,而页面是1x。最好的方法是仔细阅读javadocs。