Я пытаюсь из TextPosition нарисовать соответствующую ограничивающую рамку глифа, как показано в документации PDF 32000.
![enter image description here](https://i.stack.imgur.com/NO0bo.png)
Вот моя функция, которая выполняет вычисления от пространства глифов до пространства пользователя
@Override
protected void processTextPosition(TextPosition text) {
PDFont font = pos.getFont();
BoundingBox bbox = font.getBoundingBox();
Rectangle2D.Float rect = new Rectangle2D.Float(bbox.getLowerLeftX(), bbox.getUpperRightY(),
bbox.getWidth(), bbox.getHeight());
AffineTransform at = pos.getTextMatrix().createAffineTransform();
if (font instanceof PDType3Font) {
at.concatenate(font.getFontMatrix().createAffineTransform());
} else {
at.scale(1 / 1000f, 1 / 1000f);
}
Shape shape = at.createTransformedShape(rect);
rectangles.add(fillBBox(text));
super.processTextPosition(text);
}
А вот функция, которая рисует извлеченные прямоугольники:
private void drawBoundingBoxes() throws IOException {
String fileNameOut = path.substring(0, path.lastIndexOf(".")) + "_OUT.pdf";
log.info("Drawing Bounding Boxes for TextPositions");
PDPageContentStream contentStream = new PDPageContentStream(document,
document.getPage(document.getNumberOfPages()-1),
PDPageContentStream.AppendMode.APPEND, false , true );
contentStream.setLineWidth(1f);
contentStream.setStrokingColor(Color.RED);
try{
for (Shape p : rectangles) {
p = all.get(0);
double[] coords = new double[6];
GeneralPath g = new GeneralPath(p.getBounds2D());
for (PathIterator pi = g.getPathIterator(null);
!pi.isDone();
pi.next()) {
System.out.println(Arrays.toString(coords));
switch (pi.currentSegment(coords)) {
case PathIterator.SEG_MOVETO:
System.out.println("move to");
contentStream.moveTo ((float)coords[0], (float) coords[1]);
break;
case PathIterator.SEG_LINETO:
System.out.println("line to");
contentStream.lineTo ((float)coords[0], (float) coords[1]);
break;
case PathIterator.SEG_CUBICTO:
System.out.println("cubc to");
contentStream.curveTo((float)coords[0], (float) coords[1],
(float)coords[2], (float) coords[3],
(float)coords[4],(float) coords[5]);
break;
case PathIterator.SEG_CLOSE:
System.out.println("close");
contentStream.closeAndStroke();
break;
default:
System.out.println("no shatt");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
contentStream.close();
document.save(new File(fileNameOut));
}
}
Затем, когда я пытаюсь нарисовать на PDF, я получаю следующий результат для первой буквы (заглавная V)
Я не могу понять, что я делаю неправильно. Есть идеи?