I am using iText for Java (5.5.13) and I am experimenting with rotating PDFTemplates using the Image class. The problem is that I can't understand what iText is using for the origin when rotating the images (and I apologise in advance if I am being stupid).
Attached is the code I am using
- I create a PDFTemplate
- Fill it with some arbitrary colour
- Create an image from this template
- Rotate image 90 degrees
- Set absolute coords for the image
- Add to the writer
Repeat again with a second rectangle but this time rotated only 30 degrees.
Shouldn't there be a common origin between the two shapes?? (It looks like there is an unwanted translation too)
// 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();
Just to add background, I am doing this as I'd like to be able to wrap up text and images inside a rotatable and positionable contained (the image class which has fixed dimensions) which I can then use for building up a model of what is laid out where within the page (with a view to trying a word-art algorithm rather like wordle's).
Thanks!
xStart
and its minimum y coordinate isyStart
. – mkltextTemplate.rectangle(0, 0, boxWidth, boxWidth)
should have beentextTemplate.rectangle(0, 0, boxWidth, boxHeight)
, i.e. notboxWidth
for both dimensions. – mkl