/// <summary>
 /// Divides elements of reportContainer into pages and prints them
 /// NOTE: this overload uses default margin of 25
 /// </summary>
 /// <param name="reportContainer">StackPanel containing report elements</param>
 /// <param name="dataContext">Data Context used in the report</param>
 /// <param name="orientation">Landscape or Portrait orientation</param>
 /// <param name="resourceDictionary">Resources used in report</param>
 /// <param name="backgroundBrush">Brush that will be used as background for report page</param>
 /// <param name="reportHeaderDataTemplate">
 /// Optional header for each page
 /// Note: You can use DynamicResource PageNumber in this template to display page number
 /// </param>
 /// <param name="headerOnlyOnTheFirstPage">Use header only on the first page (default is false)</param>
 /// <param name="reportFooterDataTemplate">
 /// Optional footer for each page
 /// Note: You can use DynamicResource PageNumber in this template to display page number
 /// </param>
 /// <param name="footerStartsFromTheSecondPage">Do not use footer on the first page (default is false)</param>
 /// <param name="direction">Flow Direction</param>
 public static void PrintReport(
     StackPanel reportContainer,
     object dataContext,
     ReportOrientation orientation,
     ResourceDictionary resourceDictionary = null,
     Brush backgroundBrush = null,
     DataTemplate reportHeaderDataTemplate = null,
     bool headerOnlyOnTheFirstPage         = false,
     DataTemplate reportFooterDataTemplate = null,
     bool footerStartsFromTheSecondPage    = false,
     FlowDirection direction = FlowDirection.LeftToRight)
 {
     PrintReport(
         reportContainer,
         dataContext,
         defaultMargin,
         orientation,
         resourceDictionary,
         backgroundBrush,
         reportHeaderDataTemplate,
         headerOnlyOnTheFirstPage,
         reportFooterDataTemplate,
         footerStartsFromTheSecondPage,
         direction);
 }
Ejemplo n.º 2
0
 public ReportTemplate(string pHeader, string pFooter, string pTitle, string pSubTitle, ReportOrientation pOrientation, string pDataSourceName)
 {
     Header.Text    = pHeader;
     Footer.Text    = pFooter;
     Title.Text     = pTitle;
     SubTitle.Text  = pSubTitle;
     Orientation    = pOrientation;
     DataSourceName = pDataSourceName;
     ID             = Guid.NewGuid().ToString();
 }
        /// <summary>
        /// Divides elements of reportContainer into pages and prints them
        /// </summary>
        /// <param name="reportContainer">StackPanel containing report elements</param>
        /// <param name="dataContext">Data Context used in the report</param>
        /// <param name="margin">Margin of a report page</param>
        /// <param name="orientation">Landscape or Portrait orientation</param>
        /// <param name="resourceDictionary">Resources used in report</param>
        /// <param name="backgroundBrush">Brush that will be used as background for report page</param>
        /// <param name="reportHeaderDataTemplate">
        /// Optional header for each page
        /// Note: You can use DynamicResource PageNumber in this template to display page number
        /// </param>
        /// <param name="headerOnlyOnTheFirstPage">Use header only on the first page (default is false)</param>
        /// <param name="reportFooterDataTemplate">
        /// Optional footer for each page
        /// Note: You can use DynamicResource PageNumber in this template to display page number
        /// </param>
        /// <param name="footerStartsFromTheSecondPage">Do not use footer on the first page (default is false)</param>
        /// <param name="direction">Flow Direction</param>
        public static void PrintReport(
            StackPanel reportContainer,
            object dataContext,
            Thickness margin,
            ReportOrientation orientation,
            ResourceDictionary resourceDictionary = null,
            Brush backgroundBrush = null,
            DataTemplate reportHeaderDataTemplate = null,
            bool headerOnlyOnTheFirstPage         = false,
            DataTemplate reportFooterDataTemplate = null,
            bool footerStartsFromTheSecondPage    = false,
            FlowDirection direction = FlowDirection.LeftToRight)
        {
            PrintDialog printDialog = new PrintDialog();

            bool?result = printDialog.ShowDialog();

            if (result != true)
            {
                return;
            }

            Size reportSize = GetReportSize(reportContainer, margin, orientation, printDialog);

            List <FrameworkElement> ReportElements = new List <FrameworkElement>(reportContainer.Children.Cast <FrameworkElement>());

            reportContainer.Children.Clear(); //to avoid exception "Specified element is already the logical child of another element."

            List <ReportPage> ReportPages =
                GetReportPages(
                    resourceDictionary,
                    backgroundBrush,
                    ReportElements,
                    dataContext,
                    margin,
                    reportSize,
                    reportHeaderDataTemplate,
                    headerOnlyOnTheFirstPage,
                    reportFooterDataTemplate,
                    footerStartsFromTheSecondPage,
                    direction);

            try
            {
                ReportPages.ForEach(reportPage => reportPage.Scale(reportSize, printDialog));
                ReportPages.ForEach((reportPage, index) => printDialog.PrintVisual(reportPage.LayoutRoot, $"Report {index + 1}"));
            }
            finally
            {
                ReportPages.ForEach(reportPage => reportPage.ClearChildren());
                ReportElements.ForEach(elm => reportContainer.Children.Add(elm));
                reportContainer.UpdateLayout();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Calculate report width
        /// </summary>
        /// <param name="paperKind"></param>
        /// <param name="orientation"></param>
        /// <returns></returns>
        public static int ReportWidth(ReportPaperKind paperKind, ReportOrientation orientation)
        {
            var width = 0;

            switch (paperKind)
            {
            case ReportPaperKind.A4:
                switch (orientation)
                {
                case ReportOrientation.Landscape:
                    width = Constant.ReportWidthLandscapeA4;
                    break;

                case ReportOrientation.Portrait:
                    width = Constant.ReportWidthPotraitA4;
                    break;
                }
                break;

            case ReportPaperKind.A3:
                switch (orientation)
                {
                case ReportOrientation.Landscape:
                    width = Constant.ReportWidthLandscapeA3;
                    break;

                case ReportOrientation.Portrait:
                    width = Constant.ReportWidthPotraitA3;
                    break;
                }
                break;

            case ReportPaperKind.A2:
                switch (orientation)
                {
                case ReportOrientation.Landscape:
                    width = Constant.ReportWidthLandscapeA2;
                    break;

                case ReportOrientation.Portrait:
                    width = Constant.ReportWidthPotraitA2;
                    break;
                }
                break;
            }
            return(width);
        }
Ejemplo n.º 5
0
 private HtmlToPdfDocument PdfDocument(string content, ReportOrientation reportOrientation = ReportOrientation.Portrait)
 {
     return(new HtmlToPdfDocument()
     {
         GlobalSettings = new GlobalSettings()
         {
             PaperSize = PaperKind.A4,
             Orientation = (Orientation)(int)reportOrientation
         },
         Objects =
         {
             new ObjectSettings()
             {
                 HtmlContent = content
             }
         }
     });
 }
 /// <summary>
 /// Divides elements of reportContainer into pages and exports them as PDF
 /// NOTE: this overload uses default margin of 25
 /// </summary>
 /// <param name="reportContainer">StackPanel containing report elements</param>
 /// <param name="dataContext">Data Context used in the report</param>
 /// <param name="orientation">Landscape or Portrait orientation</param>
 /// <param name="resourceDictionary">Resources used in report</param>
 /// <param name="backgroundBrush">Brush that will be used as background for report page</param>
 /// <param name="reportHeaderDataTemplate">
 /// Optional header for each page
 /// Note: You can use DynamicResource PageNumber in this template to display page number
 /// </param>
 /// <param name="headerOnlyOnTheFirstPage">Use header only on the first page (default is false)</param>
 /// <param name="reportFooterDataTemplate">
 /// Optional footer for each page
 /// Note: You can use DynamicResource PageNumber in this template to display page number
 /// </param>
 /// <param name="footerStartsFromTheSecondPage">Do not use footer on the first page (default is false)</param>
 public static void ExportReportAsPdf(
     StackPanel reportContainer,
     object dataContext,
     ReportOrientation orientation,
     ResourceDictionary resourceDictionary = null,
     Brush backgroundBrush = null,
     DataTemplate reportHeaderDataTemplate = null,
     bool headerOnlyOnTheFirstPage         = false,
     DataTemplate reportFooterDataTemplate = null,
     bool footerStartsFromTheSecondPage    = false)
 {
     ExportReportAsPdf(
         reportContainer,
         dataContext,
         defaultMargin,
         orientation,
         resourceDictionary,
         backgroundBrush,
         reportHeaderDataTemplate,
         headerOnlyOnTheFirstPage,
         reportFooterDataTemplate,
         footerStartsFromTheSecondPage);
 }
        private static Size GetReportSize(StackPanel reportContainer, Thickness margin, ReportOrientation orientation, PrintDialog printDialog = null)
        {
            if (printDialog == null)
            {
                printDialog = new PrintDialog();
            }

            double reportWidth = reportContainer.ActualWidth + margin.Left + margin.Right;

            if (orientation == ReportOrientation.Landscape)
            {
                printDialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
            }

            double reportHeight = (reportWidth / printDialog.PrintableAreaWidth) * printDialog.PrintableAreaHeight;

            return(new Size(reportWidth, reportHeight));
        }
        /// <summary>
        /// Divides elements of reportContainer into pages and exports them as PDF
        /// </summary>
        /// <param name="reportContainer">StackPanel containing report elements</param>
        /// <param name="dataContext">Data Context used in the report</param>
        /// <param name="margin">Margin of a report page</param>
        /// <param name="orientation">Landscape or Portrait orientation</param>
        /// <param name="resourceDictionary">Resources used in report</param>
        /// <param name="backgroundBrush">Brush that will be used as background for report page</param>
        /// <param name="reportHeaderDataTemplate">
        /// Optional header for each page
        /// Note: You can use DynamicResource PageNumber in this template to display page number
        /// </param>
        /// <param name="headerOnlyOnTheFirstPage">Use header only on the first page (default is false)</param>
        /// <param name="reportFooterDataTemplate">
        /// Optional footer for each page
        /// Note: You can use DynamicResource PageNumber in this template to display page number
        /// </param>
        /// <param name="footerStartsFromTheSecondPage">Do not use footer on the first page (default is false)</param>
        /// <param name="direction">Document Flow Direction</param>
        public static void ExportReportAsPdf(
            StackPanel reportContainer,
            object dataContext,
            Thickness margin,
            ReportOrientation orientation,
            ResourceDictionary resourceDictionary = null,
            Brush backgroundBrush = null,
            DataTemplate reportHeaderDataTemplate = null,
            bool headerOnlyOnTheFirstPage         = false,
            DataTemplate reportFooterDataTemplate = null,
            bool footerStartsFromTheSecondPage    = false,
            FlowDirection direction = FlowDirection.LeftToRight)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog
            {
                DefaultExt = ".pdf",
                Filter     = "PDF Documents (.pdf)|*.pdf"
            };

            bool?result = saveFileDialog.ShowDialog();

            if (result != true)
            {
                return;
            }

            Size reportSize = GetReportSize(reportContainer, margin, orientation);

            List <FrameworkElement> ReportElements = new List <FrameworkElement>(reportContainer.Children.Cast <FrameworkElement>());

            reportContainer.Children.Clear(); //to avoid exception "Specified element is already the logical child of another element."

            List <ReportPage> ReportPages =
                GetReportPages(
                    resourceDictionary,
                    backgroundBrush,
                    ReportElements,
                    dataContext,
                    margin,
                    reportSize,
                    reportHeaderDataTemplate,
                    headerOnlyOnTheFirstPage,
                    reportFooterDataTemplate,
                    footerStartsFromTheSecondPage);

            FixedDocument fixedDocument = new FixedDocument();

            try
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    System.IO.Packaging.Package package = System.IO.Packaging.Package.Open(memoryStream, FileMode.Create);
                    XpsDocument       xpsDocument       = new XpsDocument(package);
                    XpsDocumentWriter xpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);

                    foreach (Grid reportPage in ReportPages.Select(reportPage => reportPage.LayoutRoot))
                    {
                        reportPage.Width         = reportPage.ActualWidth;
                        reportPage.Height        = reportPage.ActualHeight;
                        reportPage.FlowDirection = direction;

                        FixedPage newFixedPage = new FixedPage();
                        newFixedPage.Children.Add(reportPage);
                        newFixedPage.Measure(reportSize);
                        newFixedPage.Arrange(new Rect(reportSize));
                        newFixedPage.Width      = newFixedPage.ActualWidth;
                        newFixedPage.Height     = newFixedPage.ActualHeight;
                        newFixedPage.Background = backgroundBrush;
                        newFixedPage.UpdateLayout();
                        newFixedPage.FlowDirection = direction;

                        PageContent pageContent = new PageContent();
                        pageContent.FlowDirection = direction;

                        ((IAddChild)pageContent).AddChild(newFixedPage);

                        fixedDocument.Pages.Add(pageContent);
                    }

                    xpsDocumentWriter.Write(fixedDocument);
                    xpsDocument.Close();

                    var packageUri = new Uri("memorystream://myXps.xps");
                    PackageStore.AddPackage(packageUri, package);
                    XpsDocument doc = new XpsDocument(package, CompressionOption.SuperFast, packageUri.AbsoluteUri);

                    XpsConverter.Convert(doc, saveFileDialog.FileName, 0);

                    package.Close();
                    //var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(memoryStream);
                    //XpsConverter.Convert(pdfXpsDoc, saveFileDialog.FileName, 0);
                }
            }
            finally
            {
                ReportPages.ForEach(reportPage => reportPage.ClearChildren());
                ReportElements.ForEach(elm => reportContainer.Children.Add(elm));
                reportContainer.UpdateLayout();
            }
        }
Ejemplo n.º 9
0
        private static Size GetReportSize(StackPanel reportContainer, Thickness margin, ReportOrientation orientation, PrintDialog printDialog = null)
        {
            if (printDialog == null)
            {
                printDialog = new PrintDialog();
            }

            double reportWidth = reportContainer.ActualWidth + margin.Left + margin.Right;

            double reportHeight;

            if (orientation == ReportOrientation.Portrait)
            {
                reportHeight = (reportWidth / printDialog.PrintableAreaWidth) * printDialog.PrintableAreaHeight;
            }
            else
            {
                reportHeight = (reportWidth / printDialog.PrintableAreaHeight) * printDialog.PrintableAreaWidth;
            }

            return(new Size(reportWidth, reportHeight));
        }
Ejemplo n.º 10
0
 public PdfDocument(ReportOrientation orientation)
 {
     this.FontSize = 6;
     this.Rect.Inset(5, 5);
     this.Orientation = orientation;
 }
Ejemplo n.º 11
0
        private async Task <DownloadFileAsAttachmentResult> GeneratePDF <TModel>(string fileName, string reportPath, TModel data, ReportOrientation reportOrientation)
        {
            var view = await GetViewAsString(reportPath, data);

            var doc = PdfDocument(view, reportOrientation);

            var fileBytes = _converter.Convert(doc);

            return(new DownloadFileAsAttachmentResult(fileName + ".pdf", fileBytes, "application/pdf"));
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> GenerateReportAsPDFSingleDoc <TModel>(string fileName, string reportPath, string query, object param = null, CommandType type = CommandType.Query, ReportOrientation reportOrientation = ReportOrientation.Portrait)
        {
            var data = await RunSingleQuery <TModel>(query, param, type);

            return(await GeneratePDF(fileName, reportPath, data, reportOrientation));
        }
Ejemplo n.º 13
0
        public async Task <IActionResult> GenerateReportAsEXCEL <TModel>(string fileName, string query, object param = null, CommandType type = CommandType.Query, ReportOrientation reportOrientation = ReportOrientation.Portrait)
        {
            using (IDbConnection conn = _connection.Connection)
            {
                conn.Open();

                DynamicParameters dynamicParameters = new DynamicParameters();

                dynamicParameters.AddDynamicParams(param);

                var tes = (System.Data.CommandType)type;

                var data = await conn.QueryAsync <TModel>(query, dynamicParameters, commandType : tes);

                return(new DownloadFileAsAttachmentResult(fileName + ".xls", CreateTable(data), "application/vnd.ms-excel"));;
            }
        }