How do I add a rotate rectangle to a PDF page?

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

There was a typo. The last translation matrix to be applied should
have positive x, y:

static 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 rot = Matrix2D.RotationMatrix(rotationDegrees *
0.017453292519);

  element.SetPathFill(true);
  element.SetPathStroke(false);
  GState gstate = element.GetGState();
  gstate.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
  gstate.SetFillColor(fillColor);
  //Comment out the line below to see the red rectangle
  //With line the rectangle is not seen
  gstate.SetTransform(new Matrix2D(1, 0, 0, 1, x1, y1) * rot * new
Matrix2D(1, 0, 0, 1, -x1, -y1));
  writer.WriteElement(element);
  writer.End();
}

Thanks,
Got me confused. Was running same problem before.
Ziggy

Q: How would a I place rotated image on a page? I tired to tweak the
above code byt my image is skewed.
Thanks for looking in to this for me.
--------------------
A: You can use the following helper function:

The logic as follows:

- The unit image is first scaled using the scale factor (relative to
the original pixel dimensions).
- The image is then rotated
- The final (leftmost) matrix translates the image to the desired
location:

static private void PlaceImage(Page page, Image img, double x, double
y, double scale, int rotationDegrees)
{
  using (ElementBuilder builder = new ElementBuilder())
  {
    using(ElementWriter writer = new ElementWriter())
    {
      writer.Begin(page);
      Element element = builder.CreateImage(img, new Matrix2D(1, 0, 0, 1,
x, y)
        * Matrix2D.RotationMatrix(rotationDegrees * 0.017453292519)
        * new Matrix2D(img.GetImageWidth()*scale, 0, 0,
img.GetImageHeight()*scale, 0, 0));
      writer.WritePlacedElement(element);
      writer.End();
    }
  }
}

For example:

PDFNet.Initialize();
using (PDFDoc doc = new PDFDoc("in.pdf")) {
doc.InitSecurityHandler();
pdftron.PDF.Image img = pdftron.PDF.Image.Create(doc, "peppers.jpg");
PlaceImage(doc.GetPage(1), img, 100, 100, 0.5, 45);
doc.Save("out.pdf", 0);
}

For example of how to add a rotated text please see:

  How can I stamp all PDF pages with a rotated and centered text
stamp? Options
  http://groups.google.com/group/pdfnet-sdk/browse_thread/thread/d017db32b85c78e9/8f66636628202344