PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 12:32 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Page rotation
PostPosted: Tue Jan 10, 2012 2:06 pm 
Offline

Joined: Tue Jan 10, 2012 1:22 pm
Posts: 4
Hello,
Sorry for another topic about Rotation, but I couldn't find anything which works for me.

Business case:
We are using Microsoft Reporting 2005. A PDF document is created using ReportViewer.
Code:
string[] streamIds;
            string mimeType;
            string encoding;
            string extension;
            byte[] bytes;

            ReportViewer viewer = new ReportViewer();
            ... //Report server URL, path to report, credentials etc.
            bytes = viewer.ServerReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);


It works, but Page.Orientation property is set to Portrait for all pages. Doesn't matter the page dimensions in Report. Page is displayed correctly (landscape) in Acrobat reader and Foxit reader, but when I loop through document's pages, this property is 'Portrait'. Page.Width and Page.Height properties are correct.

Now I have to create a single PDF document from 2 reports (one is Portrait, one is Landscape) and send it to a partner. The partner uses their own software which requires all pages to be Portrait. I have to Rotate Lanscape pages by 90 degrees clockwise. When printing Landscape pages printer turns them oposite direction and that's not OK.

I have made the page rotation this way. Acrobat reader and Foxit reader shows and prints the document correctly, but when I send the PDF to our partner, their system treates rotated pages as they are Landscape.
The partner sent me this -> MediaBox:
1 ( 23 0 R): [ 0 0 595.276 841.89 ] //Portrait page from 1st report
2 ( 28 0 R): [ 0 0 841 595.276 ] // Rotated page from 2nd report

Code:
           // This will be the resulting PDF file.
                    PdfDocument document = PdfReader.Open(filepath, PdfDocumentOpenMode.Import);
                    //Loop through pages
                    foreach (PdfPage page in reportDoc.Pages)
                    {
                        //Rotate Landscape pages
                        if (page.Width > page.Height)
                        {
                            double width = page.Width;
                            double height = page.Height;
                            //This only rotates text, paper remains Landscape
                            page.Rotate = 90;
                            //Let's change the paper size
                            page.Width = Math.Min(height, width);
                            page.Height = Math.Max(height, width);
                            document.AddPage(page);
                        }
                        else
                        {
                            document.AddPage(page);
                        }
                    }

This is how the Page properties look when I add the page:
Attachment:
File comment: page properties aftes I have set Page.Rotate = 90
pdfPage.png
pdfPage.png [ 31.16 KiB | Viewed 9936 times ]


This code didn't do anything...
Code:
                    PdfDocument reportModDoc = PdfReader.Open(reportStream, PdfDocumentOpenMode.Modify);
                    foreach (PdfPage page in reportModDoc.Pages)
                    {
                        if (page.Width > page.Height)
                        {
                            XGraphics xGraph = XGraphics.FromPdfPage(page);
                            //Tried both of theese - nothing. Both page and text remains landscape
                            //xGraph.RotateAtTransform(90, new XPoint(400, 400));
                            //xGraph.RotateTransform(90);
                            xGraph.Save();
                            xGraph.Dispose();
                           
                        }
                    }
                    reportModDoc.Save(filepath);
                    reportModDoc.Dispose();

It looks like only representation is changed while page remains the same under the bonnet :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Page rotation
PostPosted: Wed Jan 11, 2012 12:40 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!
rciemins wrote:
This code didn't do anything...
I'll start with that: AFAIK the RotateTransforms etc. only affect items that are drawn after setting the rotation. You draw nothing new, so they have no effect.

Judging from what I see, all your PDF files are in Portrait mode. Some have pages that are higher than wide (what we normally call Portrait), some have pages that are wider than high (what we normally call Landscape).

You can treat pages that require rotation like the TwoPagesOnOne sample does: create a new page and draw the existing page with DrawImage - RotateTransforms will then have an effect (but maybe you don't even need them). Don't shrink them (like the sample does), just draw them with their actual size.
This will add a few bytes for pages rotated this way.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Page rotation
PostPosted: Thu Jan 12, 2012 12:22 pm 
Offline

Joined: Tue Jan 10, 2012 1:22 pm
Posts: 4
Still had problems with rotation, but finally made it work. Ugly, but working. :)
Result is aproved by our partner.

Yet there are still some questions unanswered...
If I call this method only once only the second page is converted into a tiny square. Why?
If I call this method twice second page (that tiny square) is replaced with a normal rotated page. Portrait pages I just redraw :) How can it fix?

Why did PdfDocument lost Embeded fonts? It happened on aprox 400th page... Several pages with dots instead of letters... and it's fine again :)
Is it a known problem?

Anyway - here is the method...
Code:
private void RotatePagesBy90(string filePath)
        {
            // Create the output document
            PdfDocument outputDocument = new PdfDocument();

            // Show single pages
            // (Note: one page contains two pages from the source document)
            outputDocument.PageLayout = PdfPageLayout.SinglePage;

            XFont font = new XFont("Verdana", 8, XFontStyle.Bold);
            XStringFormat format = new XStringFormat();
            format.Alignment = XStringAlignment.Center;
            format.LineAlignment = XLineAlignment.Far;
            XGraphics gfx;
            XRect box;

            // Open the external document as XPdfForm object
            XPdfForm form = XPdfForm.FromFile(filePath);

            for (int idx = 0; idx < form.PageCount; idx ++)
            {
                // Add a new page to the output document
                PdfPage page = outputDocument.AddPage();
                page.Orientation = PageOrientation.Portrait;
                double width = page.Width;
                double height = page.Height;

                int rotate = page.Elements.GetInteger("/Rotate");

                gfx = XGraphics.FromPdfPage(page);

                // Set page number (which is one-based)
                form.PageNumber = idx + 1;

                //Rotate landscape pages
                if (form.Page.Width > form.Page.Height)
                {
                    //Need 595-842 = 247. Otherwise the page contents are moved outside the page after the rotation
                    box = new XRect(-247, 0, form.Page.Width, form.Page.Height);
                    form.Page.Rotate = 90;

                    // Draw the page identified by the page number like an image
                    gfx.DrawImage(form, box);
                   
                    //gfx.RotateTransform(90); // Still does nothing :(
                }
                //Just draw portrait pages
                else
                {
                    box = new XRect(0, 0, form.Page.Width, form.Page.Height);
                    gfx.DrawImage(form, box);
                }
                //If you have embeded fonts - embed them again
                //new InvoiceEmailController().EmbedFonts(page, gfx);
            }
           
            outputDocument.Save(filePath);
        }


Before calling the method:
Attachment:
File comment: Pdf document before calling the method
Before_call.PNG
Before_call.PNG [ 37.11 KiB | Viewed 9921 times ]

After calling the method:
Attachment:
File comment: After calling the method.
After_call.PNG
After_call.PNG [ 47.55 KiB | Viewed 9921 times ]


Top
 Profile  
Reply with quote  
 Post subject: Re: Page rotation
PostPosted: Thu Jan 12, 2012 12:24 pm 
Offline

Joined: Tue Jan 10, 2012 1:22 pm
Posts: 4
Forgot... Thomas, thank you. It helped a lot :)


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

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider] and 122 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