1

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

Luciano
  • 2,695
  • 6
  • 38
  • 53
  • Could you please show us some code that you are using for conversion from XPS to PDF? – Sevenate Jul 31 '13 at 13:14
  • Also what do you mean by *WPF report* exactly? Is this is come kind of [custom](http://wpfreports.codeplex.com) component or you just use [some approach](http://blogs.vertigo.com/personal/Paul/Blog/Lists/Posts/Post.aspx?ID=2) to print arbitrary WPF control or something? – Sevenate Jul 31 '13 at 13:29
  • XPS to PDF isn't the problem, PDFSharper works. My problem is when WPF is printed as XPS. Even using XPS viewer the page is 'corrupted', so the problem isn't the PDFSharper. But I'll try to leave here a sample project to download. [Anyway you should see: http://msdn.microsoft.com/en-us/library/ms742418.aspx] – Luciano Jul 31 '13 at 13:44
  • Luciano, thanks - sample project could be helpful. – Sevenate Jul 31 '13 at 14:04
  • @Sevenate: Sample project is there, any help is appreciated. – Luciano Aug 02 '13 at 19:04
  • Luciano, sorry I dropped out for a while. Glad you have an answer already. – Sevenate Sep 20 '13 at 13:03

1 Answers1

3

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