Unplanned
Last Updated: 15 Feb 2023 13:08 by ADMIN
David
Created on: 24 Aug 2022 10:16
Category: PdfProcessing
Type: Feature Request
2
PdfProcessing: Add API allowing users to easily draw on a rotated page
Currently, when users need to add content to an existing page that is rotated, they should manually calculate the positions of the new content if they need to align it with the appearance of the page. Add API allowing them to easily transform the position respecting the top left edge of the page after its rotation.
1 comment
ADMIN
Dimitar
Posted on: 15 Feb 2023 13:08

Hello,

A possible workaround is the following approach: 

internal static Matrix CalculateRotatedPageTransformationInDip(RadFixedPage page)
{
    Rect visibleContentBox = new Rect(page.Size);

    double rotationAngle = 0;
    var rotationCenter = new System.Windows.Point();

    switch (page.Rotation)
    {
        case Rotation.Rotate90:
            rotationAngle = 90;
            rotationCenter = new System.Windows.Point(visibleContentBox.Height / 2, visibleContentBox.Height / 2);
            break;
        case Rotation.Rotate180:
            rotationAngle = 180;
            rotationCenter = new System.Windows.Point(visibleContentBox.Width / 2, visibleContentBox.Height / 2);
            break;
        case Rotation.Rotate270:
            rotationAngle = 270;
            rotationCenter = new System.Windows.Point(visibleContentBox.Width / 2, visibleContentBox.Width / 2);
            break;
        default:
            rotationAngle = 0;
            rotationCenter = new System.Windows.Point();
            break;
    }

    Matrix matrix = new Matrix();
    matrix.RotateAt(rotationAngle, rotationCenter.X, rotationCenter.Y);

    return matrix;
}
public Matrix InverseMatrix(Matrix m)
{
    var det = CalculateDeterminant(m);
    var det1 = 1 / det;
    return new Matrix(m.M22 * det1, -m.M12 * det1, -m.M21 * det1, m.M11 * det1, ((m.M21 * m.OffsetY) - (m.OffsetX * m.M22)) * det1, ((m.OffsetX * m.M12) - (m.M11 * m.OffsetY)) * det1);
}

public double CalculateDeterminant(Matrix m)
{
    return (m.M11 * m.M22) - (m.M12 * m.M21);
}

Then use it like this: 

if (page.Rotation == Telerik.Windows.Documents.Fixed.Model.Data.Rotation.Rotate270)
{  
    var m = CalculateRotatedPageTransformationInDip(page);
    var matrix = new Matrix(1, 0, 0, 1, pointInDoc.X, pointInDoc.Y) * InverseMatrix(m);
    editor.Position = new MatrixPosition(matrix);
}

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.