使用pdfbox旋转文本

Rotate text using pdfbox

我正在尝试使用 pdfbox 旋转文本,但我无法实现。我尝试设置 texMatrix,但我的文本没有按预期旋转。

有人知道如何将文本旋转 90 度吗?

这是我的代码:

contentStream.beginText();

 float tx = titleWidth / 2;
 float ty = titleHeight / 2;

contentStream.setTextMatrix(Matrix.getTranslateInstance(tx, ty)); 
contentStream.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(90),tx,ty));
contentStream.setTextMatrix(Matrix.getTranslateInstance(-tx, -ty));

 contentStream.newLineAtOffset(xPos, yPos);

contentStream.setFont(font, fontSize);
contentStream.showText("Tets");
contentStream.endText();

谢谢

这是一个绘制三页的解决方案,一个文本未旋转,一个文本旋转但保持坐标,就像规划横向打印一样,另一个是您想要的(围绕文本中心旋转)。我的解决方案接近于此,它围绕文本中心的底部旋转。

public static void main(String[] args) throws IOException
{
    PDDocument doc = new PDDocument();
    PDPage page1 = new PDPage();
    doc.addPage(page1);
    PDPage page2 = new PDPage();
    doc.addPage(page2);
    PDPage page3 = new PDPage();
    doc.addPage(page3);

    PDFont font = PDType1Font.HELVETICA;
    float fontSize = 20;
    int xPos = 100;
    int yPos = 400;
    float titleWidth = font.getStringWidth("Tets") / 1000;
    float titleHeight = fontSize;
    float tx = titleWidth / 2;
    float ty = titleHeight / 2;

    try (PDPageContentStream contentStream = new PDPageContentStream(doc, page1))
    {
        contentStream.beginText();

        contentStream.newLineAtOffset(xPos, yPos);

        contentStream.setFont(font, fontSize);
        contentStream.showText("Tets");
        contentStream.endText();
    }

    // classic case of rotated page
    try (PDPageContentStream contentStream = new PDPageContentStream(doc, page2))
    {
        contentStream.beginText();

        Matrix matrix = Matrix.getRotateInstance(Math.toRadians(90), 0, 0);
        matrix.translate(0, -page2.getMediaBox().getWidth());

        contentStream.setTextMatrix(matrix);

        contentStream.newLineAtOffset(xPos, yPos);

        contentStream.setFont(font, fontSize);
        contentStream.showText("Tets");
        contentStream.endText();
    }

    // rotation around text
    try (PDPageContentStream contentStream = new PDPageContentStream(doc, page3))
    {
        contentStream.beginText();

        Matrix matrix = Matrix.getRotateInstance(Math.toRadians(90), 0, 0);
        matrix.translate(0, -page3.getMediaBox().getWidth());

        contentStream.setTextMatrix(matrix);

        contentStream.newLineAtOffset(yPos - titleWidth / 2 - fontSize, page3.getMediaBox().getWidth() - xPos - titleWidth / 2 - fontSize);

        contentStream.setFont(font, fontSize);
        contentStream.showText("Tets");
        contentStream.endText();
    }
    doc.save("saved.pdf");
    doc.close();
}

此示例围绕文本的左基线旋转,并使用矩阵平移将文本定位在特定点。 showText() 始终定位在 0,0,即旋转前的位置。然后矩阵平移定位旋转后的文本。

如果您想要文本的另一个旋转点,请重新定位 contentStream.newLineAtOffset(0, 0) 行中的文本旋转位置

float angle = 35;
double radians = Math.toRadians(angle);
for (int x : new int[] {50,85,125, 200}) 
  for (int y : new int[] {40, 95, 160, 300}) {
    contentStream.beginText();
    
    // Notice the post rotation position
    Matrix matrix = Matrix.getRotateInstance(radians,x,y);
    contentStream.setTextMatrix(matrix);

    // Notice the pre rotation position
    contentStream.newLineAtOffset(0, 0);

    contentStream.showText(".(" + x + "," + y + ")");
    contentStream.endText();
  }

要获得要旋转的文本的高度和宽度,请使用 font.getBoundingBox().getHeight()/1000*fontSizefont.getStringWidth(text)/1000*fontSize