How to adjust for page rotation when I overlay/stamp a page with a logo on my PDF document?

Q:

I'm trying to overlay/stamp one pdf page containing a logo
(Logo.pdf) onto my pdf document (TestInput.pdf). The result is as you
see it in TestOutput.pdf. The first page has a different rotation and
hence the overlay fails to work. Here's the test code I used gleaned
from sample code on your website.

        private void TechniqueUsingOverlayPDF ()
        {
            PDFNet.Initialize();
            PDFNet.SetResourcesPath( @"..\.." );

            // Relative path to the folder containing test files.
            string input_path = @"..\..\";
            string output_path = @"..\..\";

            try
            {
                PDFDoc over = new PDFDoc( input_path + "Logo.pdf" );
                PDFDoc back = new PDFDoc( input_path + "TestInput.pdf"
);

                // Import the overly page into the background doc
                PageIterator op_itr = over.PageFind( 1 );
                back.PagePushFront( op_itr.Current() );

                // Overlay page
                Page op = back.PageFind( 1 ).Current();
                //MessageBox.Show( "Overlay Rotation: " +
op.GetRotation().ToString() );

                PageIterator begin = back.PageFind( 2 ), end =
back.PageEnd();
                PageIterator itr = begin;
                while (itr != end)
                {
                    // Background page
                    Page bp = itr.Current();

                    if (op.GetRotation() != bp.GetRotation())
                    {
                        MessageBox.Show( "Overlay Rotation: " +
op.GetRotation().ToString() + "\nPage Rotation: " +
bp.GetRotation().ToString() );
                        //align rotation of page
                        //bp.SetRotation( Page.Rotate.e_90 );
                        //op.SetRotation( Page.Rotate.e_180 );
                    }

                    // Copy Elements from Overlay page to
                    // Background page ElementReader
                    ElementReader reader = new ElementReader();
                    reader.Begin( op );
                    ElementWriter writer = new ElementWriter();
                    writer.Begin( bp );
                    Element element;
                    while ((element = reader.Next()) != null)
                    {
                        writer.WriteElement( element );
                    }

                    writer.End();
                    reader.End();
                    itr.Next();
                }
                // You can now optionaly remove the overly page
                back.PageRemove( back.PageFind( 1 ) );
                back.Save( output_path + "TestOutput.pdf",
Doc.SaveOptions.e_linearized );
            }
            catch (Exception ex)
            {
                //Console.WriteLine( "Exception caught:\n{0}", e );
            }

            PDFNet.Terminate();
        }

I tried to correct / adjust the rotation of the logo page via
SetRotation without any success.
---

A:

A simpler way to merge pages (i.e. place a page from Logo.pdf on a
page in TestInput.pdf) is as illustrated in Imposition sample project
(http://www.pdftron.com/net/samplecode.html#Imposition).

Regarding rotation you can obtain the rotation parameter for the
source and destination page using page.GetRotation(). This information
can be used to properly place the form XObject (i.e. the imported page
logo) on the destination page.

double angle = 0;
Page.Rotate r = page.GetRotation();
if (r == Page.Rotate.e_90) angle = 90;
else if (r == Page.Rotate.e_180) angle = 180; else if (r ==
Page.Rotate.e_270) angle = 270;

// Create a transformation matrix for this rotation...
double deg2rad = 3.1415926535 / 180.0;
Matrix2D mtx = new Matrix2D.RotationMatrix( angle * deg2rad )) // ?
mtx.Scale(s1, s2); mtx.Translate(x, y); // ...
element.GetGState().SetTransform(mtx);