PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Jul 15, 2024 10:09 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Jun 08, 2012 6:21 am 
Offline

Joined: Wed Jun 06, 2012 5:48 pm
Posts: 7
Hi

I am talking about PdfSharp. Portrait orientation works well with margin or without margin. But In case of landscape orientation, page truncate in right side once I set any margin using TrimMargins. I have tried same thing on sample code of pdfSharp and having same problem!!

Look pdf rendered well for following code :)
Code:
page = document.AddPage();
page.Size = PdfSharp.PageSize.A4;
page.Orientation = PageOrientation.Landscape;
gfx = XGraphics.FromPdfPage(page);
gfx.DrawString("A4 (landscape)", font,XBrushes.DarkRed, new XRect(0, 0, page.Width, page.Height),XStringFormats.Center);


But for following code pdf is not rendered well, truncate in right side :(
Code:
page = document.AddPage();
page.TrimMargins.Top = 5;
page.TrimMargins.Right = 5;
page.TrimMargins.Bottom = 5;
page.TrimMargins.Left = 5;
page.Size = PdfSharp.PageSize.A4;
page.Orientation = PageOrientation.Landscape;
gfx = XGraphics.FromPdfPage(page);
gfx.DrawString("A4 (landscape)", font,XBrushes.DarkRed, new XRect(0, 0, page.Width, page.Height),XStringFormats.Center);


Have any idea :?

Advance thanks


Top
 Profile  
Reply with quote  
PostPosted: Sat Jun 09, 2012 4:32 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 343
Hi!

Could be a bug in PDFsharp.

As a workaround, do not set the orientation to Landscape, instead swap width and height when creating the page.
Code:
page = document.AddPage();
//page.Size = PdfSharp.PageSize.A4;
XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
page.MediaBox = new PdfRectangle(new XPoint(0, 0), new XPoint(size.Height, size.Width)); // Magic: swap width and height
//page.Orientation = PageOrientation.Landscape;

The default unit for margins is Points. To get e.g. millimetres instead, you can write:

Code:
page.TrimMargins.Top = XUnit.FromMillimeter(5);
page.TrimMargins.Right = XUnit.FromMillimeter(5);
page.TrimMargins.Bottom = XUnit.FromMillimeter(5);
page.TrimMargins.Left = XUnit.FromMillimeter(5);


See also:
http://stackoverflow.com/a/10962620/1015447

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 11, 2012 5:44 pm 
Offline

Joined: Wed Jun 06, 2012 5:48 pm
Posts: 7
Yes, this is a bug of PdfSharp

We can set the margins with orientation like bellow

Code:
page = document.AddPage();
//page.Size = PdfSharp.PageSize.A4;
XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
if(page.Orientation == PageOrientation.Landscape)
{
   page.Width  = size.Height;
   page.Height = size.Width;
}
else
{
   page.Width  = size.Width;
   page.Height = size.Height;
}

// default unit in points 1 inch = 72 points
page.TrimMargins.Top = 5;
page.TrimMargins.Right = 5;
page.TrimMargins.Bottom = 5;
page.TrimMargins.Left = 5;


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 53 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