Home > Net >  PDFBox PDPageContentstream.setTextMatrix() making text disappear
PDFBox PDPageContentstream.setTextMatrix() making text disappear

Time:02-26

I am converting POSPrint-data to a PDF. At one Point i need to strech the Text over 2 Lines, but with the width of the normal text. I'm trying to archive that like this:

CONTENT.beginText();
Matrix textMatrix = new Matrix();
textMatrix.scale(1f, 2f);
CONTENT.setTextMatrix(textMatrix);
CONTENT.newLineAtOffset(50, 50);
CONTENT.setCharacterSpacing(line.getLineSpacing());
CONTENT.showText(restOfLine);
CONTENT.endText();

sadly this results in the text not showing up at all. If i remove the lines for adding the textmatrix, or setting the matrix scale values to 1 this workes without any problem:

CONTENT.beginText();
Matrix textMatrix = new Matrix();
textMatrix.scale(1f, 1f);
CONTENT.setTextMatrix(textMatrix);
CONTENT.newLineAtOffset(50, 50);
CONTENT.setCharacterSpacing(line.getLineSpacing());
CONTENT.showText(restOfLine);
CONTENT.endText();

or

CONTENT.beginText();
CONTENT.newLineAtOffset(50, 50);
CONTENT.setCharacterSpacing(line.getLineSpacing());
CONTENT.showText(restOfLine);
CONTENT.endText();

Does anybody know why this happens?

I use PDFBox 2.0.25

CodePudding user response:

If you are scaling the TextMatrix, also the position of the matrix is affected. To fix this unwanted behavior of moving the scaled text, you have to divide the textposition also by the scale.

CONTENT.beginText();
Matrix textMatrix = new Matrix();
textMatrix.scale(1f, 2f);
CONTENT.setTextMatrix(textMatrix);
CONTENT.newLineAtOffset(50, 50/2f); //devide y position by scale
CONTENT.setCharacterSpacing(line.getLineSpacing());
CONTENT.showText(restOfLine);
CONTENT.endText();
  • Related