PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Oct 21, 2019 11:44 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Mon Jan 28, 2019 7:06 pm 
Offline

Joined: Mon Jan 28, 2019 6:51 pm
Posts: 8
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 28, 2019 8:58 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 483
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 29, 2019 11:07 am 
Offline

Joined: Mon Jan 28, 2019 6:51 pm
Posts: 8
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 29, 2019 12:15 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2980
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 29, 2019 2:36 pm 
Offline

Joined: Mon Jan 28, 2019 6:51 pm
Posts: 8
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 1078 times ]
Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 29, 2019 2:43 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2980
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 30, 2019 10:15 am 
Offline

Joined: Mon Jan 28, 2019 6:51 pm
Posts: 8
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?


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 30, 2019 10:24 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2980
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 30, 2019 10:44 am 
Offline

Joined: Mon Jan 28, 2019 6:51 pm
Posts: 8
Same effect.
The image is bigger than the page


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 30, 2019 11:05 am 
Offline

Joined: Mon Jan 28, 2019 6:51 pm
Posts: 8
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);
        }


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 31, 2019 1:31 pm 
Offline

Joined: Mon Jan 28, 2019 6:51 pm
Posts: 8
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);
        }


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 375 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group