iText image.setRotationDegrees()不保持一致的原点

问题描述 投票:1回答:1

我正在使用iText for Java(5.5.13),我正在尝试使用Image类旋转PDFTemplates。问题是,当旋转图像时,我无法理解iText用于原点的内容(如果我是愚蠢的,我会事先道歉)。

附上我正在使用的代码

  • 我创建了一个PDFTemplate
  • 用一些任意颜色填充它
  • 从此模板创建图像
  • 将图像旋转90度
  • 为图像设置绝对坐标
  • 添加到作者

再次使用第二个矩形重复,但这次只旋转了30度。

这两种形状之间不应该有共同的起源吗? (看起来也有不需要的翻译)

// step 1
Rectangle pageSize = PageSize.A4;
Document document = new Document(pageSize);

// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(OUTPUT_FILENAME));

// step 3
document.open();

// step 4
float boxWidth = 200;
float boxHeight = 50;
float xStart = pageSize.getWidth()/2;
float yStart = pageSize.getHeight()/2;

// Add one filled rectangle rotated 90 degrees
{
    PdfContentByte canvas = writer.getDirectContent();
    PdfTemplate textTemplate = canvas.createTemplate(boxWidth, boxHeight);
    textTemplate.saveState();
    textTemplate.setColorFill(BaseColor.RED);
    textTemplate.rectangle(0, 0, boxWidth, boxWidth);
    textTemplate.fill();
    textTemplate.restoreState();

    Image img = Image.getInstance(textTemplate);
    img.setInterpolation(true);
    img.scaleAbsolute(boxWidth, boxHeight);
    img.setAbsolutePosition(xStart, yStart);
    img.setRotationDegrees(90);
    writer.getDirectContent().addImage(img);
}

// And another rotated 30 degrees
{
    PdfContentByte canvas = writer.getDirectContent();
    PdfTemplate textTemplate = canvas.createTemplate(boxWidth, boxHeight);
    textTemplate.saveState();
    textTemplate.setColorFill(BaseColor.BLACK);
    textTemplate.rectangle(0, 0, boxWidth, boxWidth);
    textTemplate.fill();
    textTemplate.restoreState();

    Image img = Image.getInstance(textTemplate);
    img.setInterpolation(true);
    img.scaleAbsolute(boxWidth, boxHeight);
    img.setAbsolutePosition(xStart, yStart);
    img.setRotationDegrees(30);
    writer.getDirectContent().addImage(img);
}

// step 5
document.close();

只是为了添加背景,我正在这样做,因为我希望能够将文本和图像包装在可旋转且可定位的内容(具有固定尺寸的图像类)中,然后我可以使用它来构建模型在页面内的位置(为了尝试word-art算法,而不是像wordle)。

谢谢!

java itext
1个回答
0
投票

这两种形状之间不应该有共同的起源吗? (看起来也有不需要的翻译)

你在这里隐含的假设似乎是模板首先被定位然后围绕一些明显的特殊点旋转,例如,模板的左下角。

不是这种情况。相反,您可以想象旋转模板,然后确定边界框(边缘平行于页面边缘),并且此边界框的左下角位于使用Image.setAbsolutePosition设置的坐标处。

通过绘制更多的矩形,例如,这变得更加明显。适用于0°,15°,30°,45°,60°,75°和90°:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.