PurplePen.MapUtil.GetDefaultPageSize C# (CSharp) Method

GetDefaultPageSize() public static method

public static GetDefaultPageSize ( RectangleF printAreaRectangle, float printScaleRatio, int &pageWidth, int &pageHeight, int &pageMargin, bool &landscape ) : void
printAreaRectangle System.Drawing.RectangleF
printScaleRatio float
pageWidth int
pageHeight int
pageMargin int
landscape bool
return void
        public static void GetDefaultPageSize(RectangleF printAreaRectangle, float printScaleRatio, out int pageWidth, out int pageHeight, out int pageMargin, out bool landscape)
        {
            bool metric = RegionInfo.CurrentRegion.IsMetric;

            if (printAreaRectangle.IsEmpty) {
                PaperSize paperSize = StandardPaperSizes[metric ? DefaultMetricPaperSizeindex : DefaultEnglighPaperSizeIndex];
                pageWidth = paperSize.Width;
                pageHeight = paperSize.Height;
                pageMargin = 0;
                landscape = false;
            }
            else {
                landscape = printAreaRectangle.Width > printAreaRectangle.Height;
                // Get needed page width and height in 1/100 of inch.
                float printAreaWidth = (landscape ? printAreaRectangle.Height : printAreaRectangle.Width) / printScaleRatio * 100 / 25.4F;
                float printAreaHeight = (landscape ? printAreaRectangle.Width : printAreaRectangle.Height) / printScaleRatio * 100 / 25.4F;

                int firstIndex = metric ? 0 : FirstEnglishPaperSizeIndex;
                int endIndex = metric ? FirstEnglishPaperSizeIndex : StandardPaperSizes.Length;
                int bestIndex = -1;

                // Scan through all paper indexes to find the smallest paper that fits the area.
                for (int i = firstIndex; i < endIndex; ++i) {
                    if (StandardPaperSizes[i].Width > printAreaWidth && StandardPaperSizes[i].Height > printAreaHeight &&
                        (bestIndex == -1 || StandardPaperSizes[i].Width < StandardPaperSizes[bestIndex].Width))
                        bestIndex = i;
                }
                if (bestIndex < 0)
                    bestIndex = metric ? DefaultMetricPaperSizeindex : DefaultEnglighPaperSizeIndex;

                pageWidth = StandardPaperSizes[bestIndex].Width;
                pageHeight = StandardPaperSizes[bestIndex].Height;

                // Use the default margin if it can fit, otherwise 0 margin.
                int defaultMargin = metric ? DefaultMetricMargin : DefaultEnglishMargin;
                if (pageWidth - printAreaWidth > defaultMargin * 2 &&
                    pageHeight - printAreaHeight > defaultMargin * 2) {
                    pageMargin = defaultMargin;
                }
                else {
                    pageMargin = 0;
                }
            }
        }

Usage Example

Example #1
0
        void NewEventPaperSize_Load(object sender, EventArgs e)
        {
            containingWizard = (NewEventWizard)Parent;

            RectangleF printArea       = containingWizard.mapBounds;
            float      printScaleRatio = containingWizard.DefaultPrintScale / containingWizard.MapScale;

            // Set the default page setting from the map size and print scale.
            int  pageWidth, pageHeight, pageMargin;
            bool landscape;

            MapUtil.GetDefaultPageSize(printArea, printScaleRatio, out pageWidth, out pageHeight, out pageMargin, out landscape);
            paperSizeControl.PaperSize  = new System.Drawing.Printing.PaperSize("", pageWidth, pageHeight);
            paperSizeControl.MarginSize = pageMargin;
            paperSizeControl.Landscape  = landscape;
        }