wpfpdf-generationxpsviewerxps-generation

Custom page size output in XPS from WPF report


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


Solution

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