Setting correct transformation when placing one PDF page on another (imposition).

Q:

I am having trouble placing one PDF on another. I thought I fixed
this earlier but right now the added pdf is coming in upside down. I
had this working at one point but, then it started happening again. I
am not sure what to look for. If I dont do the setTransform then it
comes out right side up but when I try to position and scale the
element it turns upside down. Any thoughts?

double clipartScaleX = 1;
double clipartScaleY = 1;
double clipartPositionX = 500;
double clipartPositionY = 500;

clipartElement.GetGState().SetTransform(clipartScaleX, 0, 0,
clipartScaleY, clipartPositionX, clipartPositionY);
---
A:

The most likely problem is that PDF pages are additionally rotated
(page.GetRotation() parameter). This rotation parameter controls the
orientation of the entire page.

You can obtain the rotation (and possibly translation matrix) for the
page using page.GetDefaultMatrix(flip).

You can then multiply this matrix by any additional transforms (such
as scale, offset, etc) before you pass the final matrix to
SetTransform(...).

C#: element.GetGState().SetTransform(new
pdftron.Common.Matrix2D(scalex, 0, 0, scaley, offsetx, offset) *
page.GetDefaultMatrix(flip));

C++: element->GetGState()->SetTransform(Common::Matrix2D(scalex, 0, 0,
scaley, offsetx, offset) * page.GetDefaultMatrix(flip))