我想为我要绘制为pdf的每个字符计算一堆边框,以获取一堆不同的字体和大小。对于不同的字体,它的工作方式似乎有所不同,例如arial mt和Elephant Regular。
例如,我一直在使用-号来感受这一点,在Arial中,大象的左下角是65440,大象是7165,但是大象被拉高了,我找不到这种运动的来龙去脉来自。
大多数字体的工作方式与Arial相似,其中字体的顶部为height / 2,但这不适用于大象字体。
需要包括哪些因素才能准确计算边界框?
package com.nexiaem.nemocr.generatepdf;
import java.awt.Color;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.util.Vector;
/**
*
* @author bjames
*/
public class DrawBoundngBox {
public static void main(String[] args){
try{
PDPage thisPage = new PDPage(new PDRectangle(100, 40));
PDDocument document = new PDDocument();
document.addPage(thisPage);
//PDTrueTypeFont font = PDTrueTypeFont.loadTTF(document, new File("C:\\windows\\fonts\\elephnt.ttf"));
PDTrueTypeFont font = PDTrueTypeFont.loadTTF(document, new File("C:\\windows\\fonts\\arial.ttf"));
PDPageContentStream contentStream = new PDPageContentStream(document, thisPage);
int thisSize = 10;
float currentX = 0;
float width = 0;
String charactersToWrite = "aj";
char[] characters = charactersToWrite.toCharArray();
for(char thisChar: characters){
String thisCharString = Character.toString(thisChar);
width = (font.getWidthFromFont((int)thisChar) / 1000.0f) * thisSize;
GeneralPath charPath = font.getPath((int)thisChar);
Rectangle2D charRect = charPath.getBounds2D();
float charMinX = thisSize * (float)charRect.getMinX() / 1000.0f;
float charMaxX = thisSize * (float)charRect.getMaxX() / 1000.0f;
float charMinY = thisSize * (float)charRect.getMinY() / 1000.0f;
float charX = thisSize * (float)charRect.getX() / 1000.0f;
float charY = thisSize * (float)charRect.getY() / 1000.0f;
Vector displacement = font.getDisplacement((int)thisChar);
float charWidth = thisSize * (float)charRect.getWidth()/ 1000.0f;
width = charMaxX - charMinX;
float height = ((float)charRect.getHeight()/ 1000.0f) * thisSize;
contentStream.setStrokingColor(Color.red);
contentStream.moveTo(currentX + charMinX/2, 10 + charMinY/2);
contentStream.lineTo(currentX + charMinX/2, 10 + charMinY/2 + height/2);
contentStream.lineTo(currentX + charMinX/2 + width/2, 10 + charMinY/2 + height/2);
contentStream.lineTo(currentX + charMinX/2 + width/2, 10 + charMinY/2);
contentStream.lineTo(currentX + charMinX/2, 10 + charMinY/2);
contentStream.stroke();
contentStream.setStrokingColor(Color.green);
contentStream.moveTo(currentX + charX, 10 + charY);
contentStream.lineTo(currentX + charX, 10 + charY + height);
contentStream.lineTo(currentX + charX + width, 10 + charY + height);
contentStream.lineTo(currentX + charX + width, 10 + charY);
contentStream.lineTo(currentX + charX, 10 + charY);
contentStream.stroke();
contentStream.setStrokingColor(Color.yellow);
contentStream.moveTo(currentX, 10);
contentStream.lineTo(currentX, 10 + height);
contentStream.lineTo(currentX + width, 10 + height);
contentStream.lineTo(currentX + width, 10);
contentStream.lineTo(currentX, 10);
contentStream.stroke();
contentStream.beginText();
contentStream.setFont(font, thisSize);
contentStream.newLineAtOffset(currentX, 10);
currentX = currentX + 30;
contentStream.showText(thisCharString);
contentStream.endText();
}
contentStream.close();
document.save("C:\\temp\\DrawBB.pdf");
document.close();
}catch(IOException ex) {
Logger.getLogger(DrawBoundngBox.class.getName()).log(Level.SEVERE, null, ex);
}
}
}