PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

DrawString on Landspace Pdf turns it to Portrait
http://forum.pdfsharp.com/viewtopic.php?f=3&t=992
Page 1 of 1

Author:  rauhryan [ Tue Dec 15, 2009 3:13 pm ]
Post subject:  DrawString on Landspace Pdf turns it to Portrait

If you open an existing pdf that is Landscape and write text on it.
It will save it as Portrait and cut off the right side.

But if you switch the page orientation to Portrait before writing the text and save, the pdf is saved as landscape as everything is fine.

Any idea as to why this would happen?

Example below.

Code:
    class Program
    {
        static void Main(string[] args)
        {
            const string filename = @"C:\Documents\LandscapePdf.pdf";


            PdfDocument document =
                PdfReader.Open(filename, PdfDocumentOpenMode.Modify);

            PdfPage page = document.Pages[0];


            XGraphics gfx = XGraphics.FromPdfPage(page);

            WriteSomeText(page, gfx);

            gfx.Dispose();

            // Save the document...
            document.Save("Temp_Test.pdf");
            // ...and start a viewer.
            Process.Start("Temp_Test.pdf");


        }

        private static void WriteSomeText(PdfPage page, XGraphics gfx)
        {
            XFont font = new XFont("Times New Roman", 10, XFontStyle.Bold);

            XStringFormat format = new XStringFormat
                                       {
                                           Alignment = XStringAlignment.Near,
                                           LineAlignment = XLineAlignment.BaseLine
                                       };

            XRect rect = new XRect(new XPoint(0, page.Height), new XSize(0, 0));
            rect.Offset(10, -25);
           
            gfx.RotateAtTransform(-90, rect.BottomLeft);

            gfx.DrawString(" -- Some Text -- ", font, XBrushes.Black, rect, format);
        }
    }


This example works, note the orientation change right before gfx.DrawString

Code:
    class Program
    {
        static void Main(string[] args)
        {
            const string filename = @"C:\Documents\LandscapePdf.pdf";


            PdfDocument document =
                PdfReader.Open(filename, PdfDocumentOpenMode.Modify);

            PdfPage page = document.Pages[0];


            XGraphics gfx = XGraphics.FromPdfPage(page);

            WriteSomeText(page, gfx);

            gfx.Dispose();

            // Save the document...
            document.Save("Temp_Test.pdf");
            // ...and start a viewer.
            Process.Start("Temp_Test.pdf");


        }

        private static void WriteSomeText(PdfPage page, XGraphics gfx)
        {
            XFont font = new XFont("Times New Roman", 10, XFontStyle.Bold);

            XStringFormat format = new XStringFormat
                                       {
                                           Alignment = XStringAlignment.Near,
                                           LineAlignment = XLineAlignment.BaseLine
                                       };

            XRect rect = new XRect(new XPoint(0, page.Height), new XSize(0, 0));
            rect.Offset(10, -25);
           
            gfx.RotateAtTransform(-90, rect.BottomLeft);

            // Note orientation change
            page.Orientation = PageOrientation.Portrait;

            gfx.DrawString(" -- Some Text -- ", font, XBrushes.Black, rect, format);
        }
    }

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