public ReportPaginator(DocumentPaginator paginator, Size pageSize, PageDefinition pd)
 {
     this.pageSize      = pageSize;
     this.paginator     = paginator;
     pageDef            = pd;
     paginator.PageSize = new Size(pageSize.Width - pd.Margin.Width * 2, pageSize.Height - 2 * minimalOffset - pd.HeaderHeight - pd.FooterHeight - pd.Margin.Height * 2);
 }
        /// <summary>
        /// Helper method to create page header o footer from flow document template
        /// </summary>
        /// <param name="fd"></param>
        /// <param name="pageDef"></param>
        /// <returns></returns>
        public static XpsDocument CreateXpsDocument(FlowDocument fd, PageDefinition pageDef)
        {
            const string pack = "pack://report.xps";

            //var ms = new MemoryStream();
            //Package pkg = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
            //PackageStore.RemovePackage(new Uri(pack));
            //PackageStore.AddPackage(new Uri(pack), pkg);
            //var doc = new XpsDocument(pkg, CompressionOption.SuperFast, pack);
            //var rsm = new XpsSerializationManager(new XpsPackagingPolicy(doc), false);
            //DocumentPaginator paginator = ((IDocumentPaginatorSource)fd).DocumentPaginator;

            //var rp = new ReportPaginator(paginator, new Size(96/2.54*21, 96/2.54*28.7), pageDef);//PrintHelper.GetPageSize()
            //rsm.SaveAsXaml(rp);

            //return doc;



            var ms      = new MemoryStream();
            var package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
            var doc     = new XpsDocument(package, CompressionOption.SuperFast, pack);

            PackageStore.AddPackage(new Uri(pack), package);

            DocumentPaginator paginator = ((IDocumentPaginatorSource)fd).DocumentPaginator;

            XpsDocument.CreateXpsDocumentWriter(doc).Write(paginator);
            //ReplacePngsWithJpegs(package);

            //var fixedDoc = doc.GetFixedDocumentSequence();
            //fixedDoc.DocumentPaginator.PageSize = GetPaperSize(reportPaperSize);

            return(doc);
        }