public Size GetSizeOfRotated() { Size size; if (_sizeSpecified) { size = CalculateElementSize(PrintItemElement.WholeItem); int interim = size.Width; size.Width = size.Height; size.Height = interim; size.Width = PrintUtils.RecalculateResolution(size.Width, _printerResolutionY, _printerResolutionX); size.Height = PrintUtils.RecalculateResolution(size.Height, _printerResolutionX, _printerResolutionY); } else { Size headerSize = CalculateElementSize(PrintItemElement.Header), footerSize = CalculateElementSize(PrintItemElement.Footer); size = new Size(); size.Width = PrintUtils.RecalculateResolution(_actualImageSize.Height, _printerResolutionY, _printerResolutionX); size.Height = PrintUtils.RecalculateResolution(_actualImageSize.Width, _printerResolutionX, _printerResolutionY); size.Height += headerSize.Height + footerSize.Height; size.Height += this.BorderWidth * 2; size.Width += this.BorderWidth * 2; } return(size); }
public void FitSize(Size bounds, bool placeholderAutoRotate) { Size itemSize = CalculateElementSize(PrintItemElement.WholeItem); if (bounds.Width >= itemSize.Width && bounds.Height >= itemSize.Height) { return; } if (_imageFitMode == ImageFitMode.CropToFit) { SetSize(bounds); return; } if (!_sizeSpecified) { FitSize(bounds); } else { if (!placeholderAutoRotate) { SetSize(PrintUtils.FitSizeToBounds(itemSize, bounds)); } else { SetSize(CalculateOptimalItemSize(bounds)); } } }
public override void FillPage(PrintPageEventArgs e, System.Collections.ArrayList items, System.Collections.ArrayList coords) { while (true) { ImagePrintItem item = GetNextItem(e); if (e.Cancel == true || item == null) { break; } if (_beginNewPage) { AddToNonPlacedItems(item); _beginNewPage = false; break; } int printerResolutionX, printerResolutionY; GetValidatedResolution(e, out printerResolutionX, out printerResolutionY); Point location = new Point(PrintUtils.InchHundredthsToPixels(_placeholderLocation.X, printerResolutionX), PrintUtils.InchHundredthsToPixels(_placeholderLocation.Y, printerResolutionY)); items.Add(item); coords.Add(location); } e.HasMorePages = HasMorePages(); }
protected void InitializePage(PrintPageEventArgs e) { int printerResolutionX, printerResolutionY; GetValidatedResolution(e, out printerResolutionX, out printerResolutionY); ConvertSpacings(printerResolutionX, printerResolutionY); _pageWidth = PrintUtils.InchHundredthsToPixels(e.MarginBounds.Width, printerResolutionX); _pageHeight = PrintUtils.InchHundredthsToPixels(e.MarginBounds.Height, printerResolutionY); _pageLeft = PrintUtils.InchHundredthsToPixels(e.MarginBounds.X, printerResolutionX); _pageTop = PrintUtils.InchHundredthsToPixels(e.MarginBounds.Y, printerResolutionY); // Taking into account shared touch-borders _pageLeft -= 1; _pageTop -= 1; _pageWidth += 1; _pageHeight += 1; _pageRight = _pageLeft + _pageWidth - 1; _pageBottom = _pageTop + _pageHeight - 1; }
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"); ImagePrintItem item = GetNextItem(e); if (e.Cancel == true || item == null) { e.HasMorePages = HasMorePages(); return; } int printerResolutionX, printerResolutionY; GetValidatedResolution(e, out printerResolutionX, out printerResolutionY); int pageWidth = PrintUtils.InchHundredthsToPixels(e.MarginBounds.Width, printerResolutionX), pageHeight = PrintUtils.InchHundredthsToPixels(e.MarginBounds.Height, printerResolutionY), marginX = PrintUtils.InchHundredthsToPixels(e.MarginBounds.X, printerResolutionX), marginY = PrintUtils.InchHundredthsToPixels(e.MarginBounds.Y, printerResolutionY); Size pageSize = new Size(pageWidth, pageHeight); item.FitSize(pageSize, _printOptions.PlaceholderAutoRotate); Size itemSize = item.GetSize(); Point coord = new Point(); coord.X = marginX + (pageWidth - itemSize.Width) / 2; coord.Y = marginY + (pageHeight - itemSize.Height) / 2; items.Add(item); coords.Add(coord); e.HasMorePages = HasMorePages(); }
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(); }
protected void ConvertSpacings(int printerResolutionX, int printerResolutionY) { _horizontalSpacing = PrintUtils.InchHundredthsToPixels(_printOptions.HorizontalSpacing, printerResolutionX); _verticalSpacing = PrintUtils.InchHundredthsToPixels(_printOptions.VerticalSpacing, printerResolutionY); }
private void FitSize(Size bounds) { Debug.Assert(!_sizeSpecified); // If item less than bounds - just exit. Size itemSize = CalculateElementSize(PrintItemElement.WholeItem); if (itemSize.Width <= bounds.Width && itemSize.Height <= bounds.Height) { return; } // Calculating area of bounded rectangle, where item could be placed Size boundedAreaSize = bounds, headerSize = CalculateElementSize(PrintItemElement.Header), footerSize = CalculateElementSize(PrintItemElement.Footer); boundedAreaSize.Width -= this.BorderWidth * 2; boundedAreaSize.Height -= this.BorderWidth * 2 + headerSize.Height + footerSize.Height; if (boundedAreaSize.Width <= 0 || boundedAreaSize.Height <= 0) { SetSize(bounds); return; } // Fitting size (maybe with rotation) Size imageSize = CalculateElementSize(PrintItemElement.Image); if (_imageAutoRotate) { Size rotatedSize = imageSize; int tmp = rotatedSize.Width; rotatedSize.Width = rotatedSize.Height; rotatedSize.Height = tmp; if (rotatedSize.Width <= boundedAreaSize.Width && rotatedSize.Height <= boundedAreaSize.Height) { imageSize = rotatedSize; } else { float originalScale = (float)boundedAreaSize.Width / imageSize.Width; if (originalScale * imageSize.Height > boundedAreaSize.Height) { originalScale = (float)boundedAreaSize.Height / imageSize.Height; } float rotatedScale = (float)boundedAreaSize.Width / rotatedSize.Width; if (rotatedScale * rotatedSize.Height > boundedAreaSize.Height) { rotatedScale = (float)boundedAreaSize.Height / rotatedSize.Height; } if (Math.Abs(1 - originalScale) >= Math.Abs(1 - rotatedScale)) { imageSize = rotatedSize; } } } itemSize = PrintUtils.FitSizeToBounds(imageSize, boundedAreaSize); itemSize.Width += this.BorderWidth * 2; itemSize.Height += this.BorderWidth * 2 + headerSize.Height + footerSize.Height; SetSize(itemSize); }
private Size CalculateOptimalItemSize(Size bounds) { Size itemSize = CalculateElementSize(PrintItemElement.WholeItem); if (itemSize.Width < bounds.Width && itemSize.Height < bounds.Height) { throw new Aurigma.GraphicsMill.UnexpectedException(); } if (!_sizeSpecified) { throw new Aurigma.GraphicsMill.UnexpectedException(); } Size headerSize = CalculateElementSize(PrintItemElement.Header), footerSize = CalculateElementSize(PrintItemElement.Footer); float maxScale = float.MinValue; Size bestSize = Size.Empty; for (int i = 0; i < 2; i++) { Size fittedItemSize; if (i == 0) { fittedItemSize = PrintUtils.FitSizeToBounds(itemSize, bounds); } else { fittedItemSize = PrintUtils.FitSizeToBounds(GetSizeOfRotated(), bounds); } for (int j = 0; j < 2; j++) { Size fittedImageSize = fittedItemSize; fittedImageSize.Height -= headerSize.Height + footerSize.Height + 2 * this.BorderWidth; fittedImageSize.Width -= 2 * this.BorderWidth; Size imageSize; float scale; if (j == 0) { imageSize = PrintUtils.ResizeProportionally(_actualImageSize, fittedImageSize); scale = (float)imageSize.Width / _actualImageSize.Width; } else { imageSize = PrintUtils.ResizeProportionally(new Size(_actualImageSize.Height, _actualImageSize.Width), fittedImageSize); scale = (float)imageSize.Width / _actualImageSize.Height; } if (scale > maxScale) { maxScale = scale; bestSize = fittedItemSize; } if (!_imageAutoRotate) { break; } } } return(bestSize); }
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); }