Page truncate in right side for landscape orientation with trimmargins using PdfSharp

前端 未结 3 1735
忘掉有多难
忘掉有多难 2021-01-14 07:18

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

3条回答
  •  粉色の甜心
    2021-01-14 07:36

    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);
    

提交回复
热议问题