Esempio n. 1
0
        // --------------------- AdjustFlowDocumentToPage ---------------------
        /// <summary>
        ///   Fits a given flow document to a specified media size.</summary>
        /// <param name="ipd">
        ///   The document paginator containing the flow document.</param>
        /// <param name="pq">
        ///   The print queue the document will be output to.
        public Visual AdjustFlowDocumentToPage(
            DocumentPaginator idp, PrintQueue pq)
        {
            const double inch = 96;

            PrintTicket pt = pq.UserPrintTicket;

            // Get the media size.
            double width = pt.PageMediaSize.Width.Value;
            double height = pt.PageMediaSize.Height.Value;

            // Set the margins.
            double leftmargin = 1.25 * inch;
            double rightmargin = 1.25 * inch;
            double topmargin = 1 * inch;
            double bottommargin = 1 * inch;

            // Calculate the content size.
            double contentwidth = width - leftmargin - rightmargin;
            double contentheight = height - topmargin - bottommargin;
            idp.PageSize = new Size(contentwidth, contentheight);

            DocumentPage p = idp.GetPage(0);

            // Create a wrapper visual for transformation and add extras.
            ContainerVisual page = new ContainerVisual();

            page.Children.Add(p.Visual);

            DrawingVisual title = new DrawingVisual();

            using (DrawingContext ctx = title.RenderOpen())
            {
                Typeface typeface = new Typeface("Times New Roman");
                Brush pen = Brushes.Black;
                FormattedText text =
                    new FormattedText("Page 0",
                            System.Globalization.CultureInfo.CurrentCulture,
                            FlowDirection.LeftToRight, typeface, 14, pen);

                ctx.DrawText(text, new Point(inch / 4, -inch / 2));
            }

            page.Children.Add(title);
            page.Transform = new TranslateTransform(leftmargin, topmargin);

            return page;
        }
 private Border GetPageUiElement(int i, DocumentPaginator paginator, double scale)
 {
     var source = paginator.GetPage(i);
     var border = new Border() { Background = Brushes.White };
     border.Margin = new Thickness(10 * scale);
     border.BorderBrush = Brushes.DarkGray;
     border.BorderThickness = new Thickness(1);
     //var margin = PrintUtility.GetPageMargin(CurrentPrinterName);
     var margin = new Thickness();
     var rectangle = new Rectangle();
     rectangle.Width = ((source.Size.Width * 0.96 - (margin.Left + margin.Right)) * scale);
     rectangle.Height = ((source.Size.Height * 0.96 - (margin.Top + margin.Bottom)) * scale);
     rectangle.Margin = new Thickness(margin.Left * scale, margin.Top * scale, margin.Right * scale, margin.Bottom * scale);
     rectangle.Fill = Brushes.White;
     var vb = new VisualBrush(source.Visual);
     vb.Opacity = 1;
     vb.Stretch = Stretch.Uniform;
     rectangle.Fill = vb;
     border.Child = rectangle;
     return border;
 }
Esempio n. 3
0
        private RenderTargetBitmap GetPageBitmap(DocumentPaginator documentPaginator, int pageNumber, Parameters parameters)
        {
            const double dpiConst = 96.0;

            double dpi = parameters.Dpi;

            var size = parameters.RequiredSize ?? new Size();

            Func<int, bool> isSizeDefined = requiredSize => requiredSize > 0;
            Action<int, double> calcDpi = (requiredSize, pageSize) =>
            {
                if (isSizeDefined(requiredSize))
                {
                    dpi = (requiredSize / pageSize) * dpiConst;
                }
            };

            try
            {
                using (var page = documentPaginator.GetPage(pageNumber))
                {
                    if (!size.IsEmpty)
                    {
                        var portrait = page.Size.Height >= page.Size.Width;

                        if (portrait || !isSizeDefined(size.Width))
                        {
                            calcDpi(size.Height, page.Size.Height);
                        }

                        if (!portrait || !isSizeDefined(size.Height))
                        {
                            calcDpi(size.Width, page.Size.Width);
                        }
                    }

                    var ratio = dpi / dpiConst;

                    var bitmap = new RenderTargetBitmap((int)Math.Round(page.Size.Width * ratio),
                                                        (int)Math.Round(page.Size.Height * ratio), dpi, dpi, PixelFormats.Pbgra32);

                    bitmap.Render(page.Visual);

                    // Memory leak fix.
                    // http://social.msdn.microsoft.com/Forums/en/wpf/thread/c6511918-17f6-42be-ac4c-459eeac676fd
                    ((FixedPage)page.Visual).UpdateLayout();

                    return bitmap;

                }
            }
            catch (XamlParseException ex)
            {
                throw new ConversionException(ex.Message, pageNumber + 1, ex);
            }
        }