VintaSoft PDF .NET Plug-in Discussions

Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.

Board index < VintaSoft Imaging < VintaSoft PDF .NET Plug-in Discussions

We are migrating to new forums engine, no new registration or posting currently available. TIA for your patience.

Adding PdfFreeTextAnnotation independent of page rotation



Adding PdfFreeTextAnnotation independent of page rotation

Post by Znippy »

Hello,
I am currently trying to add a PdfFreeTextAnnotation to a page independent of the page rotation.
Everything is working fine if the page has no rotation. This is my code:
Vintasoft.Imaging.Pdf.Tree.Annotations.PdfFreeTextAnnotation annotation = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfFreeTextAnnotation(page);

annotation.Color = System.Drawing.Color.Transparent;
annotation.IsReadOnly = true;
annotation.IsReadOnly = true;
annotation.Contents = "Text Content";
annotation.TextQuadding = Vintasoft.Imaging.Pdf.Tree.InteractiveForms.TextQuaddingType.Centered;
annotation.Font = page.Document.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.Helvetica);
annotation.TextColor = System.Drawing.Color.Red;
annotation.FontSize = 12;

Vintasoft.Imaging.Pdf.Drawing.PdfGraphics pdfGraphics = page.GetGraphics();

float width;
float height;

pdfGraphics.MeasureString(annotation.Contents,
                                            annotation.Font,
                                            annotation.FontSize,
                                            500,
                                            false,
                                            out width,
                                            out height);

width = width + width * 0.1f + annotation.BorderWidth * 2 + annotation.TextPadding.Horizontal;
height = height + height * 0.1f + annotation.BorderWidth * 2 + annotation.TextPadding.Vertical;

SizeF sizeF = new SizeF(width, height);

PointF location = new PointF((page.CropBox.Width - sizeF.Width) / 2, page.CropBox.Height - 25);

annotation.Rectangle = new RectangleF(page.MediaBox.X + location.X,
                                                        page.MediaBox.Y + location.Y,
                                                        sizeF.Width,
                                                        sizeF.Height);
                
                
page.Annotations.Add(annotation);
If the page has a rotation the annotation is rotated with the page. I don't want this.
Changing the location of the annotation if the page is rotated is easy but I can't figure out how to rotate the annotation itself so it stays horizontally.

Thanks!
Matt


Re: Adding PdfFreeTextAnnotation independent of page rotation

Post by Alex »

Hello Matt,

The PdfAnnotation.IsNoRotate property allows to specify that annotation must not be rotated on PDF page. You can set this flag and annotation will not be rotated if you will view PDF page in Adobe Reader. Unfortunately, current version of our PDF renderer does not support PdfAnnotation.IsNoRotate property. We will add support in future.

For solving your task, please rotate annotation before adding annotation to PDF page using the following code:
//....
RotateAnnotation(annotation, page.Rotate);
page.Annotations.Add(annotation);
//....


private void RotateAnnotation(Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotation annotation, int pageAngle)
{
    if (pageAngle != 0)
    {
        Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource appearance = annotation.GetNormalAppearance(true);
        System.Drawing.RectangleF sourceRect = annotation.Rectangle;
        if (pageAngle == 90 || pageAngle == 270)
        {
            annotation.Rectangle = new System.Drawing.RectangleF(annotation.Rectangle.X, annotation.Rectangle.Y, annotation.Rectangle.Height, annotation.Rectangle.Width);
        }
        Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource newAppearance = new Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource(annotation.Document, new RectangleF(0, 0, annotation.Rectangle.Width, annotation.Rectangle.Height));
        using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics g = Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromForm(newAppearance))
        {
            Vintasoft.Imaging.AffineMatrix matrix = null;
            switch (pageAngle)
            {
                case 90:
                    matrix = new Vintasoft.Imaging.AffineMatrix(0, 1, -1, 0, sourceRect.Height, 0);
                    break;
                case 180:
                    matrix = new Vintasoft.Imaging.AffineMatrix(-1, 0, 0, -1, sourceRect.Width, sourceRect.Height);
                    break;
                case 270:
                    matrix = new Vintasoft.Imaging.AffineMatrix(0, -1, 1, 0, 0, sourceRect.Width);
                    break;
            }
            g.MultiplyTransform(matrix);
            g.DrawForm(appearance);
        }
        annotation.Appearances.Normal = newAppearance;
    }
}
Best regards, Alexander


Re: Adding PdfFreeTextAnnotation independent of page rotation

Post by Znippy »

Hello Alex,

first of all, thank you for your fast reply!
This worked perfectly!

Greetings,
Matt


Re: Adding PdfFreeTextAnnotation independent of page rotation

Post by Znippy »

Hello Alex,

sadly it turned out I cant use the AffineMatrix class on the client system for a pretty specific technical reason.
In the documentation I saw that you can also use MultiplyTransform with a System.Drawing.Drawing2D.Matrix.

Sadly I can't figure out how to create this matrix to get the same effect.
Yes there is a Transform Method from AffineMatrix to Matrix but since I can't use the AffineMatrix class this won't help me.

Any hints how to use System.Drawing.Drawing2D.Matrix?

Thanks!
Matt


Re: Adding PdfFreeTextAnnotation independent of page rotation

Post by Alex »

Hello Matt,

The Vintasoft.Imaging.AffineMatrix class (https://www.vintasoft.com/docs/vsimagin ... atrix.html) is the base class of VintaSoft Imaging .NET SDK. Why you cannot use the Vintasoft.Imaging.AffineMatrix class?

Best regards, Alexander


Page 1 from 1: 1