PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Feb 26, 2020 12:37 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Tue Dec 15, 2009 3:13 pm 
Offline

Joined: Tue Dec 15, 2009 2:59 pm
Posts: 1
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);
        }
    }


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


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