我正在使用iText for Java(5.5.13),我正在尝试使用Image类旋转PDFTemplates。问题是,当旋转图像时,我无法理解iText用于原点的内容(如果我是愚蠢的,我会事先道歉)。
附上我正在使用的代码
再次使用第二个矩形重复,但这次只旋转了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)。
谢谢!