Q: I am trying to rotate the rectangle by 45 degrees using the code
below.
If I do not SetTransform then my box appears but is not rotated. I
tried the rotate math and I do not see my rectangle
What am I doing wrong?
Thanks,
Joseph
private void PlaceFilledRect(Page page, double x1, double y1, double
x2, double y2, ColorPt fillColor, int rotationDegrees)
{
ElementBuilder builder = new ElementBuilder(); //
Used to build new Element objects
ElementWriter writer = new
ElementWriter(); // Used to write Elements to the page
Element element;
writer.Begin(page);
element = builder.CreateRect(x1, y1, x2 - x1 + 1, y2
- y1 + 1);
Matrix2D mtx =
Matrix2D.RotationMatrix(rotationDegrees * deg2rad);
element.SetPathFill(true);
element.SetPathStroke(false);
GState gstate = element.GetGState();
gstate.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
gstate.SetFillColor(fillColor);
gstate.SetTransform(gstate.GetTransform() * mtx);
writer.WriteElement(element);
writer.End();
}
------------------
A: To rotate the rectangle you need to translate the rectangle to the
origin, rotate it, then translate it back to the correct location. For
example:
Element element = builder.CreateRect(x1, y1, x2 - x1 + 1, y2 - y1 +
1);
Matrix2D rot = Matrix2D.RotationMatrix(rotationDegrees * deg2rad);
element.GetGState().SetTransform(Matrix2D(1, 0, 0, 1, -x1, -y1) * rot
* Matrix2D(1, 0, 0, 1, -x1, -y1));