PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
Page rotation https://forum.pdfsharp.net/viewtopic.php?f=2&t=1882 |
Page 1 of 1 |
Author: | rciemins [ Tue Jan 10, 2012 2:06 pm ] |
Post subject: | Page rotation |
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 [ 31.16 KiB | Viewed 10896 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 |
Author: | Thomas Hoevel [ Wed Jan 11, 2012 12:40 pm ] |
Post subject: | Re: Page rotation |
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. |
Author: | rciemins [ Thu Jan 12, 2012 12:22 pm ] |
Post subject: | Re: Page rotation |
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 [ 37.11 KiB | Viewed 10881 times ] After calling the method: Attachment:
File comment: After calling the method.
After_call.PNG [ 47.55 KiB | Viewed 10881 times ] |
Author: | rciemins [ Thu Jan 12, 2012 12:24 pm ] |
Post subject: | Re: Page rotation |
Forgot... Thomas, thank you. It helped a lot |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |