簡體   English   中英

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

要獲得要旋轉的文本的高度和寬度,請使用font.getBoundingBox().getHeight()/1000*fontSizefont.getStringWidth(text)/1000*fontSize

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM