Skip to main content Skip to footer

How to Print a FlexReport to Multiple Printers

As the name says, FlexReport provides flexibility when it comes to meeting reporting needs. Its powerful engine provides control over the rendering and printing processes in various ways. Recently, we received a support question where the requirement was to switch printers between pages of a report. Our customer wanted even-numbered pages of their report to be printed from one printer, and the odd-numbered pages from another. Seems a bit tricky, but it's not.

In this blog post, we'll go over how to print pages of a FlexReport from multiple printers.

Get the sample

Let’s say you have a report called FlexReport1 that has 10 pages, and you want to print all the even-numbered pages using Printer X, and the odd-numbered pages using Printer Y. In this case, you'll need to create two instances of FlexReport, and add the even- and odd-numbered pages from FlexReport1 as images. Using FlexReport’s GetPageImage method, you can get a page’s metafile, which can further be added in the two instances of reports depending on the odd- and even-numbered pages.

You can use the following C# code to print your reports from different printers:

           double _preTopEven = 0.0;
            double _preTopOdd = 0.0;
            for (int i = 0; i < c1FlexReport1.PageCount; i++)
            {
                if (i % 2 == 0)
                {                    
                    ImageField im = new ImageField();
                    im.AutoHeight = AutoSizeBehavior.CanGrow;
                    im.AutoWidth = AutoSizeBehavior.CanGrow;
                    Metafile mf = c1FlexReport1.GetPageImage(i);
                    im.Picture = mf;
                    im.SplitVertBehavior = SplitBehavior.Never;
                    im.SplitHorzBehavior = SplitBehavior.Never;
                    im.Top = _preTopEven;
                    _preTopEven += _flexEven.Layout.PageSize.Height;
                    _flexEven.Sections.Detail.Fields.Add(im);  
                }
                else
                {
                    ImageField im = new ImageField();
                    im.AutoHeight = AutoSizeBehavior.CanGrow;
                    im.AutoWidth = AutoSizeBehavior.CanGrow;
                    Metafile mf = c1FlexReport1.GetPageImage(i);
                    im.Picture = mf;
                    im.SplitVertBehavior = SplitBehavior.Never;
                    im.SplitHorzBehavior = SplitBehavior.Never;
                    im.Top = _preTopOdd;
                    _preTopOdd += _flexOdd.Layout.PageSize.Height;                    
                    _flexOdd.Sections.Detail.Fields.Add(im);
                }
            }

            _flexEven.GenerateAsync().ContinueWith(t =>
            {
                PrinterSettings ps1 = new PrinterSettings();
                ps1.PrinterName = "Printer 1";
                ps1.PrintFileName = "file1";
                _flexEven.Print(ps1);
            });

            if (c1FlexReport1.PageCount>1)
            {
                _flexOdd.GenerateAsync().ContinueWith(t =>
                {
                    PrinterSettings ps2 = new PrinterSettings();
                    ps2.PrinterName = "Printer 2";
                    ps2.PrintFileName = "file2";
                    _flexOdd.Print(ps2);
                });
            }

You can modify the above code for your own printing needs. If there's any requirement you need us to explain, please mention in the comments.

Get the sample

Try FlexReport today

Sonu Singh

Software Engineer
comments powered by Disqus