PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Parmenant rotaton of 90 degress
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3907
Page 1 of 1

Author:  ora8 [ Mon Jan 28, 2019 7:06 pm ]
Post subject:  Parmenant rotaton of 90 degress

Hello

Wenn I set page.Rotate, only the view will be rotated, in the pdf statys page.Rotate = 1.
I would like to have is a rotated pdf with page.Rotate = 0 in the pdf.
How can I make this?

Thanks.

Urs

Author:  TH-Soft [ Mon Jan 28, 2019 8:58 pm ]
Post subject:  Re: Parmenant rotaton of 90 degress

ora8 wrote:
How can I make this?
I'm not sure what you want.
You can use DrawImage to draw an "old" PDF page onto a new PDF page and apply any rotation you want.

Author:  ora8 [ Tue Jan 29, 2019 11:07 am ]
Post subject:  Re: Parmenant rotaton of 90 degress

Ok I do not like images.
And there is no way like this

_graphics.RotateAtTransform(-90.0, new XPoint(_page.Width / 2, _page.Height / 2));
var temp = _page.Width;
_page.Width = _page.Height;
_page.Height = temp;

// graphis operations ....

I receive a mess.

Author:  Thomas Hoevel [ Tue Jan 29, 2019 12:15 pm ]
Post subject:  Re: Parmenant rotaton of 90 degress

ora8 wrote:
Ok I do not like images.
PDF pages can be drawn like images, but they remain PDF pages (vector format, not raster image).

RotateAtTransform and DrawImage would be my approach.

Author:  ora8 [ Tue Jan 29, 2019 2:36 pm ]
Post subject:  Re: Parmenant rotaton of 90 degrees

I tried it like this
Code:
        public void Rotate(string filename, int desiredRotation)
        {
            var tempFilename = @"C:\temp\rotate.pdf";
            using (var image = XImage.FromFile(filename))
            {
                var document = new PdfSharp.Pdf.PdfDocument();
                var page = document.AddPage();
                var graphics = XGraphics.FromPdfPage(page);
                graphics.RotateAtTransform(desiredRotation, new XPoint(page.Width / 2, page.Height / 2));
                graphics.DrawImage(image, new XPoint(0.0, 0.0));
                document.Save(tempFilename);
            }

            File.Delete(filename);
            File.Move(tempFilename, filename);
        }


The result is not exaclty that what I need.

Attachments:
pdf.png
pdf.png [ 51.65 KiB | Viewed 829 times ]

Author:  Thomas Hoevel [ Tue Jan 29, 2019 2:43 pm ]
Post subject:  Re: Parmenant rotaton of 90 degrees

ora8 wrote:
The result is not exaclty that what I need.
Transformations make things complicated.
I think point (0, 0) gets transformed and happens to be outside the visible region.

Author:  ora8 [ Wed Jan 30, 2019 10:15 am ]
Post subject:  Re: Parmenant rotaton of 90 degress

Hello

I have rewritten it.
Code:
        public void Rotate(string filename, int desiredRotation)
        {
            if (desiredRotation % 90 != 0)
                throw new ArgumentException("Rotation must be multiple of 90");

            var tempFilename = Path.Combine(Path.GetDirectoryName(filename), "rotate.pdf");
            using (var image = XImage.FromFile(filename))
            {
                var newDocument = new PdfSharp.Pdf.PdfDocument();
                var newPage = newDocument.AddPage();
                var graphics = XGraphics.FromPdfPage(newPage);
                var document = PdfSharp.Pdf.IO.PdfReader.Open(filename);
                if (desiredRotation / 90 == 1 || desiredRotation / 90 == 3)
                {
                    if (document.PageCount > 0)
                    {
                        var page = document.Pages[0];
                        var temp = page.Width;
                        page.Width = page.Height;
                        page.Height = temp;
                    }
                }

                graphics.RotateAtTransform(desiredRotation, new XPoint(newPage.Width / 2.0, newPage.Height / 2.0));
                graphics.DrawImage(image, (newPage.Width - image.PixelWidth) / 2.0,
                       (newPage.Height - image.PixelHeight) / 2.0, image.PixelWidth, image.PixelHeight);
                newDocument.Save(tempFilename);
            }

            File.Delete(filename);
            File.Move(tempFilename, filename);
        }


Now with this the content is centered and rotated. But the content is stretched. Do I use the wrong DrawImage methods?

Author:  Thomas Hoevel [ Wed Jan 30, 2019 10:24 am ]
Post subject:  Re: Parmenant rotaton of 90 degress

ora8 wrote:
But the content is stretched. Do I use the wrong DrawImage methods?
Maybe the destination page has the wrong size.
What happens when you invoke DrawImage with the position only?

You do nothing with "document", so these 11 lines can be deleted without consequences.

Author:  ora8 [ Wed Jan 30, 2019 10:44 am ]
Post subject:  Re: Parmenant rotaton of 90 degress

Same effect.
The image is bigger than the page

Author:  ora8 [ Wed Jan 30, 2019 11:05 am ]
Post subject:  Re: Parmenant rotaton of 90 degress

This seems to work

Code:
        public void Rotate(string filename, int desiredRotation)
        {
            if (desiredRotation % 90 != 0)
                throw new ArgumentException("Rotation must be multiple of 90");

            var tempFilename = Path.Combine(Path.GetDirectoryName(filename), "rotate.pdf");
            using (var image = XImage.FromFile(filename))
            {
                var newDocument = new PdfSharp.Pdf.PdfDocument();
                var newPage = newDocument.AddPage();
                if (desiredRotation % 180 == 0)
                {
                    newPage.Width = image.PointWidth;
                    newPage.Height = image.PointHeight;
                }
                else
                {
                    newPage.Width = image.PointHeight;
                    newPage.Height = image.PointWidth;
                }

                var graphics = XGraphics.FromPdfPage(newPage);
                graphics.RotateAtTransform(desiredRotation, new XPoint(newPage.Width / 2.0, newPage.Height / 2.0));
                graphics.DrawImage(image, (newPage.Width - image.PointWidth) / 2,
                    (newPage.Height - image.PointHeight) / 2.0);
                newDocument.Save(tempFilename);
            }

            File.Delete(filename);
            File.Move(tempFilename, filename);
        }

Author:  ora8 [ Thu Jan 31, 2019 1:31 pm ]
Post subject:  Re: Parmenant rotaton of 90 degress

I have forgotten that I have also A3 pdf files.
This is a better solution
Code:
        public void Rotate(string filename, int desiredRotation)
        {
            if (desiredRotation % 90 != 0)
                throw new ArgumentException("Rotation must be multiple of 90");

            var tempFilename = Path.Combine(Path.GetDirectoryName(filename), "rotate.pdf");
            using (var image = XImage.FromFile(filename))
            {
                var newDocument = new PdfSharp.Pdf.PdfDocument();
                var newPage = newDocument.AddPage();
                if (desiredRotation % 180 == 0)
                {
                    if (newPage.Width < image.PointWidth)
                        newPage.Size = PdfSharp.PageSize.A3;
                }
                else
                {
                    if (newPage.Width < image.PointHeight)
                        newPage.Size = PdfSharp.PageSize.A3;
                }

                var graphics = XGraphics.FromPdfPage(newPage);
                graphics.RotateAtTransform(desiredRotation, new XPoint(newPage.Width / 2.0, newPage.Height / 2.0));
                graphics.DrawImage(image, (newPage.Width - image.PointWidth) / 2,
                    (newPage.Height - image.PointHeight) / 2.0);
                newDocument.Save(tempFilename);
            }

            File.Delete(filename);
            File.Move(tempFilename, filename);
        }

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/