internal void Render(UIElement element, PdfRenderContext context) { if (element == null || element.Visibility != Visibility.Visible || element.Opacity == 0) { return; } var compositeSave = new CompositeDisposableObject(); compositeSave.Add(UIElementRendererBase.SaveMatrixPosition(context.drawingSurface, element as FrameworkElement)); compositeSave.Add(UIElementRendererBase.SaveClip(context.drawingSurface, element)); compositeSave.Add(UIElementRendererBase.SaveOpacity(context, context.opacity * element.Opacity)); using (compositeSave) { foreach (UIElementRendererBase renderer in this.renderers) { bool success = renderer.Render(element, context); if (success) { return; } } } System.Diagnostics.Debug.WriteLine("" + element + " could not be exported correctly."); }
internal override bool Render(UIElement element, PdfRenderContext context) { Shape shape = element as Shape; if (shape == null) { return(false); } Type type = shape.GetType(); PropertyInfo geometryProperty = type.GetProperty("DefiningGeometry", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); Geometry geometry = (Geometry)geometryProperty.GetValue(shape, null); var pdfGeometry = PdfGeometryHelper.ConvertGeometry(geometry); if (pdfGeometry == null) { return(false); } using (context.drawingSurface.SaveGraphicProperties()) { SetFill(context, shape.Fill, shape.ActualWidth, shape.ActualHeight); SetStroke(context, shape.StrokeThickness, shape.Stroke, shape.ActualWidth, shape.ActualHeight, shape.StrokeDashArray); if (context.drawingSurface.GraphicProperties.IsFilled || context.drawingSurface.GraphicProperties.IsStroked) { context.drawingSurface.DrawPath(pdfGeometry); } return(true); } }
public static void DrawTextBlock(string text, PdfRenderContext context, Brush foreground, double width, double height, FontFamily fontFamily, double fontSize, System.Windows.FontWeight fontWeight) { if (!string.IsNullOrEmpty(text)) { using (context.drawingSurface.SaveProperties()) { UIElementRendererBase.SetFill(context, foreground, width, height); SetFontFamily(context.drawingSurface, fontFamily, fontWeight); context.drawingSurface.TextProperties.FontSize = fontSize; Block block = new Block(); block.TextProperties.CopyFrom(context.drawingSurface.TextProperties); block.GraphicProperties.CopyFrom(context.drawingSurface.GraphicProperties); string[] textLines = text.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); foreach (string textLine in textLines) { block.InsertText(textLine); block.InsertLineBreak(); } context.drawingSurface.DrawBlock(block); } } }
private void DrawBorderStroke(PdfRenderContext context, Thickness thickness, Brush stroke, double actualWidth, double actualHeight) { if (stroke == null) { return; } if (thickness.Left != 0) { LineRenderer.DrawLine(context, thickness.Left / 2, 0, thickness.Left / 2, actualHeight, thickness.Left, stroke, null); } if (thickness.Top != 0) { LineRenderer.DrawLine(context, 0, thickness.Top / 2, actualWidth, thickness.Top / 2, thickness.Top, stroke, null); } if (thickness.Right != 0) { double x = actualWidth - (thickness.Right / 2); LineRenderer.DrawLine(context, x, 0, x, actualHeight, thickness.Right, stroke, null); } if (thickness.Bottom != 0) { double y = actualHeight - (thickness.Bottom / 2); LineRenderer.DrawLine(context, 0, y, actualWidth, y, thickness.Bottom, stroke, null); } }
internal override bool Render(UIElement element, PdfRenderContext context) { var border = element as System.Windows.Controls.Border; if (border == null) { return(false); } if (IsSimpleStrokeThickness(border.BorderThickness)) { RectangleRenderer.DrawRectangle(context, border.Background, border.BorderBrush, border.BorderThickness.Left, border.ActualWidth, border.ActualHeight, null); } else { DrawBackground(context, border.Background, border.BorderThickness, border.ActualWidth, border.ActualHeight); DrawBorderStroke(context, border.BorderThickness, border.BorderBrush, border.ActualWidth, border.ActualHeight); } UIElement firstChild = border.Child; context.facade.Render(firstChild, context); return(true); }
internal static void SetFill(PdfRenderContext context, Brush brush, double width, double height) { var fill = PdfColorHelper.ConvertBrush(brush, context.opacity, context.drawingSurface.Position, width, height); context.drawingSurface.GraphicProperties.IsFilled = fill != null; context.drawingSurface.GraphicProperties.FillColor = fill; }
internal override bool Render(UIElement element, PdfRenderContext context) { Path path = element as Path; if (path == null) { return(false); } var pdfGeometry = PdfGeometryHelper.ConvertGeometry(path.Data); if (pdfGeometry == null) { return(false); } using (context.drawingSurface.SaveGraphicProperties()) { SetFill(context, path.Fill, path.ActualWidth, path.ActualHeight); SetStroke(context, path.StrokeThickness, path.Stroke, path.ActualWidth, path.ActualHeight, path.StrokeDashArray); if (context.drawingSurface.GraphicProperties.IsFilled || context.drawingSurface.GraphicProperties.IsStroked) { context.drawingSurface.DrawPath(pdfGeometry); } return(true); } }
public void Dispose() { if (this.context != null) { this.context.opacity = this.opacity; this.context = null; } }
internal override bool Render(UIElement element, PdfRenderContext context) { TextBlock textBlock = element as TextBlock; if (textBlock == null) { return(false); } string text = textBlock.Text; Brush foreground = textBlock.Foreground; double width = textBlock.ActualWidth; double height = textBlock.ActualHeight; var fontFamily = textBlock.FontFamily; double fontSize = textBlock.FontSize; var fontWeight = textBlock.FontWeight; var groupData = textBlock.DataContext as GroupData; if (groupData != null) { var groupNode = groupData.Data as IGroup; switch (groupNode.Type) { case GroupType.BottomLevel: fontWeight = FontWeights.Normal; break; case GroupType.GrandTotal: case GroupType.Subheading: case GroupType.Subtotal: fontWeight = FontWeights.Bold; break; default: break; } } else { var data = textBlock.DataContext as CellData; var groupNode = data.RowItem as IGroup; var shouldBeBold = (groupNode.Type == GroupType.GrandTotal || groupNode.Type == GroupType.Subtotal); groupNode = data.ColumnItem as IGroup; shouldBeBold = shouldBeBold || (groupNode.Type == GroupType.GrandTotal || groupNode.Type == GroupType.Subtotal); // Bold the TextBlocks that display SubTotal and GrandTotal if (shouldBeBold) { fontWeight = FontWeights.Bold; } } TextRenderer.DrawTextBlock(text, context, foreground, width, height, fontFamily, fontSize, fontWeight); return(true); }
private void DrawBackground(PdfRenderContext context, Brush fill, Thickness thickness, double actualWidth, double actualHeight) { using (context.drawingSurface.SaveGraphicProperties()) { context.drawingSurface.GraphicProperties.IsStroked = false; SetFill(context, fill, actualWidth, actualHeight); Rect innerRect = new Rect(thickness.Left, thickness.Top, actualWidth - thickness.Left - thickness.Right, actualHeight - thickness.Top - thickness.Bottom); context.drawingSurface.DrawRectangle(innerRect); } }
internal override bool Render(UIElement element, PdfRenderContext context) { Line line = element as Line; if (line == null) { return(false); } DrawLine(context, line.X1, line.Y1, line.X2, line.Y2, line.StrokeThickness, line.Stroke, line.StrokeDashArray); return(true); }
internal override bool Render(UIElement element, PdfRenderContext context) { Panel panel = element as Panel; if (panel == null) { return(false); } RectangleRenderer.DrawRectangle(context, panel.Background, null, 0, panel.ActualWidth, panel.ActualHeight, null); return(base.Render(panel, context)); }
internal static IDisposable SaveOpacity(PdfRenderContext context, double newOpacity) { if (context.opacity != newOpacity) { DisposableOpacity disposableOpacity = new DisposableOpacity(context); context.opacity = newOpacity; return(disposableOpacity); } else { return(null); } }
internal static void DrawRectangle(PdfRenderContext context, Brush fill, Brush stroke, double strokeThickness, double actualWidth, double actualHeight, DoubleCollection dashArray) { using (context.drawingSurface.SaveGraphicProperties()) { SetFill(context, fill, actualWidth, actualHeight); SetStroke(context, strokeThickness, stroke, actualWidth, actualHeight, dashArray); if (context.drawingSurface.GraphicProperties.IsFilled || context.drawingSurface.GraphicProperties.IsStroked) { // account for the difference in the notion of stroke in wpf's Rectangle/Border and pdf's Rectangle double thickness = context.drawingSurface.GraphicProperties.IsStroked ? strokeThickness : 0; context.drawingSurface.DrawRectangle(new Rect(thickness / 2, thickness / 2, actualWidth - thickness, actualHeight - thickness)); } } }
internal static void SetStroke(PdfRenderContext context, double thickness, Brush brush, double width, double height, DoubleCollection dashArray) { var stroke = PdfColorHelper.ConvertBrush(brush, context.opacity, context.drawingSurface.Position, width, height); context.drawingSurface.GraphicProperties.IsStroked = thickness != 0 && stroke != null; if (context.drawingSurface.GraphicProperties.IsStroked) { context.drawingSurface.GraphicProperties.StrokeThickness = thickness; context.drawingSurface.GraphicProperties.StrokeColor = stroke; if (dashArray != null) { context.drawingSurface.GraphicProperties.StrokeDashArray = dashArray; } } }
internal override bool Render(UIElement element, PdfRenderContext context) { FrameworkElement frameworkElement = element as FrameworkElement; if (frameworkElement == null) { return(false); } bool hasMatchingType = false; Type elementType = frameworkElement.GetType(); foreach (Type type in this.types) { if (elementType == type || elementType.IsSubclassOf(type)) { hasMatchingType = true; break; } } if (!hasMatchingType) { return(false); } List <UIElement> uiElements = new List <UIElement>(); for (int i = 0; i < VisualTreeHelper.GetChildrenCount(frameworkElement); i++) { UIElement child = VisualTreeHelper.GetChild(frameworkElement, i) as UIElement; uiElements.Add(child); } uiElements = uiElements.OrderBy(el => Canvas.GetZIndex(el)).ToList(); foreach (UIElement child in uiElements) { context.facade.Render(child, context); } return(true); }
internal override bool Render(UIElement element, PdfRenderContext context) { Rectangle rectangle = element as Rectangle; if (rectangle == null) { return(false); } bool hasStroke = rectangle.Stroke != null && rectangle.StrokeThickness != 0; bool isLine = rectangle.ActualWidth == 1 || rectangle.ActualHeight == 1; if (isLine && !hasStroke) { // export the thin Rectangle as a Line to work-around a visual bug in Adobe Reader // account for the different rendering in wpf's Rectangle and pdf's line Point startPoint; Point endPoint; if (rectangle.ActualWidth == 1) { startPoint = new Point(0.5, 0); endPoint = new Point(0.5, rectangle.ActualHeight); } else { startPoint = new Point(0.5, 0.5); endPoint = new Point(0.5 + rectangle.ActualWidth, 0.5); } LineRenderer.DrawLine(context, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y, 1, rectangle.Fill, null); } else { DrawRectangle(context, rectangle.Fill, rectangle.Stroke, rectangle.StrokeThickness, rectangle.ActualWidth, rectangle.ActualHeight, rectangle.StrokeDashArray); } return(true); }
internal static void DrawLine(PdfRenderContext context, double x1, double y1, double x2, double y2, double strokeThickness, Brush stroke, DoubleCollection dashArray) { using (context.drawingSurface.SaveGraphicProperties()) { if (x1 == x2 && System.Math.Abs(y1 - y2) > 10) { if (y2 < y1) { double max = y1; y1 = y2; y2 = max; } // offset the start position of the line to resolve a visual bug in Adobe Reader, where the axis line seems to continue after the last tick y1 += 0.5; } SetStroke(context, strokeThickness, stroke, Math.Abs(x2 - x1), Math.Abs(y2 - y1), dashArray); if (context.drawingSurface.GraphicProperties.IsStroked) { context.drawingSurface.DrawLine(new Point(x1, y1), new Point(x2, y2)); } } }
internal DisposableOpacity(PdfRenderContext context) { this.context = context; this.opacity = context.opacity; }
internal abstract bool Render(UIElement element, PdfRenderContext context);
internal void Render(UIElement element, FixedContentEditor drawingSurface) { PdfRenderContext context = new PdfRenderContext(drawingSurface, this); this.Render(element, context); }