Example #1
0
        private void send_metafile_to_printer(Metafile metafile, PrintPageEventArgs e)
        {

            // constants
            const float unit_factor = 100.0f;

            // prefs
            var desired_printing_dpi = new System.Drawing.SizeF(300.0f, 300.0f);

            // Retrieve metadfile information
            var metafile_header = metafile.GetMetafileHeader();
            var metafile_dpi = new System.Drawing.SizeF(metafile_header.DpiX, metafile_header.DpiY);

            // Based on the orientation command, adjust the printer orientation
            var orient_command = SSRSCommon.PageOrientationCommand.UseDefaultPrinterSetting;

            if (orient_command == SSRSCommon.PageOrientationCommand.ForceLandscape)
            {
                e.PageSettings.Landscape = true;
            }
            else if (orient_command == SSRSCommon.PageOrientationCommand.ForcePortrait)
            {
                e.PageSettings.Landscape = false;
            }
            else if (orient_command == SSRSCommon.PageOrientationCommand.UseDefaultPrinterSetting)
            {
                // do nothing
            }
            else if (orient_command == SSRSCommon.PageOrientationCommand.UseReportDesign)
            {
                bool design_landscape = metafile.Width > metafile.Height;
                e.PageSettings.Landscape = design_landscape;
            }

            // Retrieve information about the printer
            var papersize = new SD.SizeF(e.PageSettings.PaperSize.Width, e.PageSettings.PaperSize.Height);
            //var printable_size = new SD.SizeF(e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);

            // ------------------------------------------------------------------
            // find the metafile size in printer units
            var dpi_factor = metafile_dpi.DivideBy(desired_printing_dpi);
            e.Graphics.ScaleTransform(dpi_factor.Width, dpi_factor.Height, System.Drawing.Drawing2D.MatrixOrder.Prepend);
            var metafile_size_normalized = metafile.Size.ToSizeF().DivideBy(desired_printing_dpi).MultiplyBy(unit_factor);
            var metafile_size_oriented = e.PageSettings.Landscape ? metafile_size_normalized.FlipSides() : metafile_size_normalized;

            // ----------------------------------------------------
            // Calculate the scaling factor to use on the metafile - do we need to shrink it down to fit the page or not.
            
            float scale_to_fit_factor = 1.0f; // 1.0 = don't scale the metafile at all
            if (!metafile_size_normalized.FitsInside(papersize))
            {
                scale_to_fit_factor = papersize.DivideBy(metafile_size_oriented).GetSmallestSide();
            }

            // we now have all the information we need to scale
            e.Graphics.ScaleTransform(scale_to_fit_factor, scale_to_fit_factor, System.Drawing.Drawing2D.MatrixOrder.Append);

            // shift the metafile by hard margin size to aling to the top left side of the paper
            int hm_offset_x = (int) e.PageSettings.HardMarginX*-1;
            int hm_offset_y = (int) e.PageSettings.HardMarginY * -1;
            var hm_offset = new SD.Point(hm_offset_x, hm_offset_y);
            var points = new[] { hm_offset };
            var matrix = e.Graphics.Transform;
            matrix.Invert();
            matrix.TransformPoints(points);

            // --------------------------------------------------------------------
            // Draw the image
            
            e.Graphics.DrawImageUnscaled(metafile, points[0]);
        }
Example #2
0
        private void send_metafile_to_printer(Metafile metafile, PrintPageEventArgs e)
        {
            // constants
            const float unit_factor = 100.0f;

            // prefs
            var desired_printing_dpi = new System.Drawing.SizeF(300.0f, 300.0f);

            // Retrieve metadfile information
            var metafile_header = metafile.GetMetafileHeader();
            var metafile_dpi    = new System.Drawing.SizeF(metafile_header.DpiX, metafile_header.DpiY);

            // Based on the orientation command, adjust the printer orientation
            var orient_command = SSRSCommon.PageOrientationCommand.UseDefaultPrinterSetting;

            if (orient_command == SSRSCommon.PageOrientationCommand.ForceLandscape)
            {
                e.PageSettings.Landscape = true;
            }
            else if (orient_command == SSRSCommon.PageOrientationCommand.ForcePortrait)
            {
                e.PageSettings.Landscape = false;
            }
            else if (orient_command == SSRSCommon.PageOrientationCommand.UseDefaultPrinterSetting)
            {
                // do nothing
            }
            else if (orient_command == SSRSCommon.PageOrientationCommand.UseReportDesign)
            {
                bool design_landscape = metafile.Width > metafile.Height;
                e.PageSettings.Landscape = design_landscape;
            }

            // Retrieve information about the printer
            var papersize = new SD.SizeF(e.PageSettings.PaperSize.Width, e.PageSettings.PaperSize.Height);
            //var printable_size = new SD.SizeF(e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);

            // ------------------------------------------------------------------
            // find the metafile size in printer units
            var dpi_factor = metafile_dpi.DivideBy(desired_printing_dpi);

            e.Graphics.ScaleTransform(dpi_factor.Width, dpi_factor.Height, System.Drawing.Drawing2D.MatrixOrder.Prepend);
            var metafile_size_normalized = metafile.Size.ToSizeF().DivideBy(desired_printing_dpi).MultiplyBy(unit_factor);
            var metafile_size_oriented   = e.PageSettings.Landscape ? metafile_size_normalized.FlipSides() : metafile_size_normalized;

            // ----------------------------------------------------
            // Calculate the scaling factor to use on the metafile - do we need to shrink it down to fit the page or not.

            float scale_to_fit_factor = 1.0f; // 1.0 = don't scale the metafile at all

            if (!metafile_size_normalized.FitsInside(papersize))
            {
                scale_to_fit_factor = papersize.DivideBy(metafile_size_oriented).GetSmallestSide();
            }

            // we now have all the information we need to scale
            e.Graphics.ScaleTransform(scale_to_fit_factor, scale_to_fit_factor, System.Drawing.Drawing2D.MatrixOrder.Append);

            // shift the metafile by hard margin size to aling to the top left side of the paper
            int hm_offset_x = (int)e.PageSettings.HardMarginX * -1;
            int hm_offset_y = (int)e.PageSettings.HardMarginY * -1;
            var hm_offset   = new SD.Point(hm_offset_x, hm_offset_y);
            var points      = new[] { hm_offset };
            var matrix      = e.Graphics.Transform;

            matrix.Invert();
            matrix.TransformPoints(points);

            // --------------------------------------------------------------------
            // Draw the image

            e.Graphics.DrawImageUnscaled(metafile, points[0]);
        }