1
votes

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

I tried to attach what I see as an inline screenshot but don't have the reputation yet. You can see it here:

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!

1
To me it looks like iText is positioning the the bounding box of the rotated template at the given absolute coordinates, i.e. the rotated template is positioned so that its minimum x coordinate is xStart and its minimum y coordinate is yStart.mkl
As an aside, your textTemplate.rectangle(0, 0, boxWidth, boxWidth) should have been textTemplate.rectangle(0, 0, boxWidth, boxHeight), i.e. not boxWidth for both dimensions.mkl
Yes sorry - typo from when I was tidying up the code.Sujay J

1 Answers

0
votes

Shouldn't there be a common origin between the two shapes?? (It looks like there is an unwanted translation too)

Your implicit assumption here seems to be that the template is first positioned and then rotated around some obvious special point, e.g. the lower left corner of the template.

This is not the case. Instead you can imagine that the template is rotated, then the bounding box (with edges parallel to the page edges) is determined, and the lower left corner of this bounding box is positioned at the coordinates you set using Image.setAbsolutePosition.

This becomes more obvious by drawing more rectangles, e.g. for 0°, 15°, 30°, 45°, 60°, 75°, and 90°:

enter image description here