简体   繁体   English

"使用 pdfbox 旋转文本"

[英]Rotate text using pdfbox

I'm trying to rotate text using pdfbox by I couldn't achieve it.我正在尝试使用 pdfbox 旋转文本,但我无法实现。 I tried to set the texMatrix but my text is not rotating as intended.我试图设置 texMatrix 但我的文本没有按预期旋转。

Does someone have an idea of how I could turn at 90 degrees my text?有人知道我如何将文本旋转 90 度吗?

This is my code :这是我的代码:

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();

Here's a solution that draws three pages, one with text unrotated, one with text rotated but keeping the coordinates as if planning landscape printing, and one that is what you wanted (rotated around the center of the text).这是一个绘制三页的解决方案,一个文本未旋转,一个文本旋转但保持坐标,就像计划横向打印一样,另一个是您想要的(围绕文本中心旋转)。 My solution is close to that, it rotates around the bottom of the center of the text.我的解决方案与此接近,它围绕文本中心的底部旋转。

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();
}

This example rotates around the left baseline of the text and uses the matrix translation to position the text at the specific point.此示例围绕文本的左基线旋转,并使用矩阵平移将文本定位在特定点。 The showText() is always positioned at 0,0 , which is the position before the rotation. showText()始终位于0,0 ,即旋转前的位置。 The matrix translation then positions the text after the rotation.矩阵平移然后在旋转后定位文本。

If you want another rotation point of your text relocation the text rotation position in the contentStream.newLineAtOffset(0, 0) -line如果您想要文本的另一个旋转点重定位contentStream.newLineAtOffset(0, 0) -line 中的文本旋转位置

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();
  }

To get the height and the width of the text you want to rotate use font.getBoundingBox().getHeight()/1000*fontSize and font.getStringWidth(text)/1000*fontSize .要获得要旋转的文本的高度和宽度,请使用font.getBoundingBox().getHeight()/1000*fontSizefont.getStringWidth(text)/1000*fontSize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM