public override void FillPage(PrintPageEventArgs e, System.Collections.ArrayList items, System.Collections.ArrayList coords)
        {
            System.Diagnostics.Debug.Assert(items != null, "pItems should be allocated by caller");
            System.Diagnostics.Debug.Assert(coords != null, "pCoords should be allocated by caller");

            int printerResolutionX, printerResolutionY;

            GetValidatedResolution(e, out printerResolutionX, out printerResolutionY);

            ConvertSpacings(printerResolutionX, printerResolutionY);

            int curX       = 0,
                curY       = 0,
                leftMargin = PrintUtils.InchHundredthsToPixels(e.MarginBounds.X, printerResolutionX),
                topMargin  = PrintUtils.InchHundredthsToPixels(e.MarginBounds.Y, printerResolutionY),
                maxHeight  = -1,
                pageWidth  = PrintUtils.InchHundredthsToPixels(e.MarginBounds.Width, printerResolutionX),
                pageHeight = PrintUtils.InchHundredthsToPixels(e.MarginBounds.Height, printerResolutionY);

            Size pageSize = new Size(pageWidth, pageHeight);

            bool isPageFilled = false;

            while (!isPageFilled)
            {
                bool isRowFilled = false;
                maxHeight = -1;

                while (!isRowFilled)
                {
                    ImagePrintItem item = GetNextItem(e);
                    if (e.Cancel == true)
                    {
                        return;
                    }

                    if (item == null)
                    {
                        isPageFilled = true;
                        break;
                    }

                    item.FitSize(pageSize, _printOptions.PlaceholderAutoRotate);
                    Size itemSize    = item.GetSize();
                    Size rotatedSize = item.GetSizeOfRotated();

                    // Trying to insert (maybe with rotation, if it allowed)
                    int freeSpaceWidth  = pageWidth - curX,
                        freeSpaceHeight = pageHeight - curY;

                    if (itemSize.Width <= freeSpaceWidth && itemSize.Height <= freeSpaceHeight)
                    {
                        // Item placed successfully
                        items.Add(item);
                        coords.Add(new Point(curX + leftMargin, curY + topMargin));

                        curX += itemSize.Width;

                        if (itemSize.Height > maxHeight)
                        {
                            maxHeight = itemSize.Height;
                        }
                    }
                    else if (_printOptions.PlaceholderAutoRotate && (rotatedSize.Width <= freeSpaceWidth && rotatedSize.Height <= freeSpaceHeight))
                    {
                        // Item placed successfully after a 90-degree rotate.
                        items.Add(item);
                        coords.Add(new Point(curX + leftMargin, curY + topMargin));

                        item.SetSize(rotatedSize);

                        curX += rotatedSize.Width;

                        if (rotatedSize.Height > maxHeight)
                        {
                            maxHeight = rotatedSize.Height;
                        }
                    }
                    else
                    {
                        // Item cannot be inserted into current row - putting it into
                        // NonPlacedItems queue for trying to insert into next rows.
                        AddToNonPlacedItems(item);
                        isRowFilled = true;
                    }

                    curX += _horizontalSpacing;
                    if (curX >= pageWidth)
                    {
                        isRowFilled = true;
                    }
                }

                // Shifting to next row. If intMaxHeight == -1 - this means that
                // no one Item has been inserted on current step => current Item
                // of the queue can be placed only on next page => this page is filled.
                if (maxHeight == -1)
                {
                    isPageFilled = true;
                }
                else
                {
                    curX  = 0;
                    curY += maxHeight + _verticalSpacing;
                    if (curY > pageHeight)
                    {
                        isPageFilled = true;
                    }
                }
            }

            e.HasMorePages = HasMorePages();
        }
예제 #2
0
        protected virtual ImagePrintItem GetNextItem(PrintPageEventArgs e)
        {
            if (!_eventHasMoreImages)
            {
                return(null);
            }

            // Getting next image & info from IImageProvider
            PrintPlaceholder imagePlaceholder;

            if (_imageProvider != null && !_imageProvider.IsEmpty())
            {
                imagePlaceholder = _imageProvider.GetNext();
            }
            else
            {
                imagePlaceholder = new PrintPlaceholder();
            }

            // Firing PrintImage event in ImagePrintDocument
            QueryImageEventArgs queryImageEventArgs = new QueryImageEventArgs();

            queryImageEventArgs.PrintPlaceholder = imagePlaceholder;
            queryImageEventArgs.PrintOptions     = _printOptions;
            OnQueryImageEvent(queryImageEventArgs);

            if (queryImageEventArgs.Cancel == true || queryImageEventArgs.PrintPlaceholder.Image == null)
            {
                e.Cancel = true;
                return(null);
            }

            int printerResolutionX, printerResolutionY;

            GetValidatedResolution(e, out printerResolutionX, out printerResolutionY);

            // If we didn't got image from IImageProvider or from user's handler
            // of PrintImage event - just return null.
            if (imagePlaceholder.Image == null)
            {
                return(null);
            }

            // Filling result ImagePrintItem
            ImagePrintItem item = new ImagePrintItem(printerResolutionX, printerResolutionY);

            item.Image             = imagePlaceholder.Image;
            item.ImageFitMode      = _printOptions.ImageFitMode;
            item.ImageAutoRotate   = _printOptions.ImageAutoRotate;
            item.InterpolationMode = _printOptions.InterpolationMode;
            item.HeaderFont        = _printOptions.HeaderFont;
            item.FooterFont        = _printOptions.FooterFont;
            item.HeaderColor       = _printOptions.HeaderColor;
            item.FooterColor       = _printOptions.FooterColor;
            item.HeaderTrimmming   = _printOptions.HeaderTrimming;
            item.FooterTrimmming   = _printOptions.FooterTrimming;
            item.HeaderAlignment   = _printOptions.HeaderAlignment;
            item.FooterAlignment   = _printOptions.FooterAlignment;

            // Header & footer
            if (_printOptions.HeaderEnabled)
            {
                item.HeaderText = imagePlaceholder.Header;
            }
            if (_printOptions.FooterEnabled)
            {
                item.FooterText = imagePlaceholder.Footer;
            }

            if (_printOptions.BorderEnabled)
            {
                int pixelWidth = PrintUtils.InchHundredthsToPixels(_printOptions.BorderWidth, printerResolutionX);
                item.BorderPen = new System.Drawing.Pen(_printOptions.BorderColor, pixelWidth);
            }

            // Setting external size. Size can be redefined in QueryImageEventHandler
            if (imagePlaceholder.Size.Width != 0 && imagePlaceholder.Size.Height != 0)
            {
                Size resizeSize = new Size();
                resizeSize.Width  = PrintUtils.InchHundredthsToPixels(imagePlaceholder.Size.Width, printerResolutionX);
                resizeSize.Height = PrintUtils.InchHundredthsToPixels(imagePlaceholder.Size.Height, printerResolutionY);

                item.SetSize(resizeSize);
            }
            else if (_printOptions.PlaceholderSize.Width != 0 && _printOptions.PlaceholderSize.Height != 0)
            {
                Size resizeSize = new Size();
                resizeSize.Width  = PrintUtils.InchHundredthsToPixels(_printOptions.PlaceholderSize.Width, printerResolutionX);
                resizeSize.Height = PrintUtils.InchHundredthsToPixels(_printOptions.PlaceholderSize.Height, printerResolutionY);

                item.SetSize(resizeSize);
            }

            return(item);
        }