Reset PDF Page Rotation to 0

Q: We’ve noticed that some applications will generate PDF files with initial rotation values (in this case it’s the CutePDF print driver). When you open the PDF in Adobe, the orientation is correct (landscape), but there is a stored rotation value of 90. If we remove that value with CosEdit and reopen the PDF, it’s now rotated into portrait mode.

Is there any way to reset the rotation value of a page to 0 while maintaining the correct orientation?

I’ve attached a PDF which displays this behavior.

A:

As a solution you can call page.SetRotation(Page.Rotate.e_270), and rotate page media & crop box (similar to Rect sample - pdftron.com/pdfnet/samplecode.html#Rect) and also add a transform matrix that will rotate all page content. The last part is a bit more tricky… You can use ElementEdit approach (http://pdftron.com/pdfnet/samplecode.html#ElementEdit), where you adjust the transform on the initial element (you could surround all content with e_group_begin/end element; i.e q/Q). Perhaps a simpler approach is to approach shown in Imposition sample (http://pdftron.com/pdfnet/samplecode.html#Imposition))… where you place an existing page on another page (in your case that would be rotated page).

Here is sample code for keeping the correct orientation while resetting the rotation to 0:

for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next())
{
    Page page = itr.Current();
    Rect m = page.GetMediaBox();
    Page.Rotate rot = page.GetRotation();
    if (rot == Page.Rotate.e_0) continue;
    if (rot != Page.Rotate.e_180)
    {
        double w = m.x2 - m.x1;
        double h = m.y2 - m.y1;
        m.x2 = m.x1 + h;
        m.y2 = m.y1 + w;
    }

    Page new_page = in_doc.PageCreate(m);
    in_doc.GetSDFDoc().Swap(page.GetSDFObj().GetObjNum(), new_page.GetSDFObj().GetObjNum());
    using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, 1.0, 1.0))
    {
        s.StampPage(in_doc, new_page, new PageSet(itr.GetPageNumber()));
    }
}

Thanks for this Code. It resolved the rotation reset issue… However, this will also flatten all the annotations. May you please guide us on how to do the same without flattening?