Custom page size output in XPS from WPF report

荒凉一梦 提交于 2019-12-05 07:26:29

问题


I have a WPF report which draws the report content in a custom page size (we can consider it as a A4, the problem is the same), if I send the output to a printer (physical or virtual like PDFCreator) my custom page size are correctly preserved for each page.

But when I output it as XPS format the pages are adapted to Letter page size.

How to preserve my custom page size when outputting a WPF report to XPS ?

MY FINAL GOAL: Is to have a PDF from WPF, and my approach is to convert a XPS to PDF using PDFSharper. The conversion works well but the XPS output corrupts my custom page size. Others approaches are welcome but I would like to understand and control the XPS output page size, anyway.

SAMPLE PROJECT: Test_WpfToPDF_withXpsCorruptingPageSizeToLetter.zip


回答1:


It seems to work when you wrap the user control (the report) inside another element, providing also the real size and not forgetting to put a background. I don't know exactly why, but the background seems to do the trick, along with the size.

public override DocumentPage GetPage(int pageNumber) {
    var page = new MyUserControl();

    Border wrapper = new Border();
    wrapper.Width = this.PageSize.Width;
    wrapper.Height = this.PageSize.Height;
    wrapper.Background = Brushes.White;

    wrapper.Child = page;

    wrapper.Measure(this.PageSize);
    wrapper.Arrange(new Rect(new Point(0, 0), this.PageSize));
    wrapper.UpdateLayout();

    return new DocumentPage(wrapper);
}


来源:https://stackoverflow.com/questions/17889108/custom-page-size-output-in-xps-from-wpf-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!