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
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
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?
Thanks
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
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:
page.TrimMargins.Top = XUnit.FromMillimeter(5); page.TrimMargins.Right = XUnit.FromMillimeter(5); page.TrimMargins.Bottom = XUnit.FromMillimeter(5); page.TrimMargins.Left = XUnit.FromMillimeter(5);
Method 2
Yes, this is a bug of PdfSharp
We can set the margins with orientation like bellow
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;
Method 3
I had this same problem — very frustrating. To anyone whom the top solution didn’t work: try this!
Since PDFSharp can only handle graphics and transformations properly on pages in portrait orientation, my work-around on my project was converting the landscape pages to portrait with .page.Rotate = 0 upon input. Remember that these files are now sideways so keep that in mind when applying graphics and transformations. Then before saving the file, I converted the page back to landscape with .page.Rotate = 90. Worked fine for me! Good luck y’all.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0