private void _printDocument_PrintPage(object sender, PrintPageEventArgs e) { // Get the print document object PrintDocument document = sender as PrintDocument; SvgDocument svgDoc = _viewer.DocumentList[_currentPrintPageNumber - 1].Document; var svgResolution = svgDoc.Bounds.Resolution; var svgBounds = svgDoc.Bounds.Bounds; // Get page size in pixels var pixelSize = LeadSizeD.Create(svgBounds.Width, svgBounds.Height); using (Bitmap bitmap = new Bitmap((int)pixelSize.Width, (int)pixelSize.Height, PixelFormat.Format32bppPArgb)) { // Convert to DPI var size = LeadSizeD.Create(pixelSize.Width * bitmap.HorizontalResolution / svgResolution.Width, pixelSize.Height * bitmap.VerticalResolution / svgResolution.Height).ToLeadSize(); // Fit in the margin bounds var destRect = LeadRect.Create(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, e.MarginBounds.Height); destRect = RasterImage.CalculatePaintModeRectangle( size.Width, size.Height, destRect, RasterPaintSizeMode.Fit, RasterPaintAlignMode.Center, RasterPaintAlignMode.Center); using (Graphics g = Graphics.FromImage(bitmap)) { using (var engine = RenderingEngineFactory.Create(g)) { var options = new SvgRenderOptions(); options.Bounds = svgBounds; svgDoc.Render(engine, options); } } e.Graphics.DrawImage(bitmap, destRect.X, destRect.Y, destRect.Width, destRect.Height); } // Go to the next page _currentPrintPageNumber++; // Inform the printer whether we have more pages to print if (_currentPrintPageNumber <= document.PrinterSettings.ToPage) { e.HasMorePages = true; } else { e.HasMorePages = false; } }
private void DrawImage(PaintEventArgs e, Rectangle clipRect) { if (_documents.Count == 0) { return; } if (_documents[_currentPageIndex].Document == null || !this.CanUpdate) { return; } var graphics = e.Graphics; var options = new SvgRenderOptions(); options.Transform = _transform; options.Bounds = _documents[_currentPageIndex].Document.Bounds.Bounds; options.UseBackgroundColor = true; options.ClipBounds = LeadRectD.Create(clipRect.X, clipRect.Y, clipRect.Width, clipRect.Height); options.BackgroundColor = RasterColor.FromKnownColor(RasterKnownColor.White); try { using (var engine = RenderingEngineFactory.Create(graphics)) _documents[_currentPageIndex].Document.Render(engine, options); } catch { Console.WriteLine(); } DrawBounds(graphics, Pens.Black, null, null, null, null, options.Bounds, options.Transform); if (_documents[_currentPageIndex].DocumentText != null && _documents[_currentPageIndex].ShowText) { LeadRectD docBounds = _documents[_currentPageIndex].Document.Bounds.Bounds; // Could be rotated, so LeadPointD topLeft = docBounds.TopLeft; LeadPointD bottomRight = docBounds.BottomRight; LeadPointD[] corners = new LeadPointD[4]; corners[0].X = topLeft.X; corners[0].Y = topLeft.Y; corners[1].X = bottomRight.X; corners[1].Y = topLeft.Y; corners[2].X = bottomRight.X; corners[2].Y = bottomRight.Y; corners[3].X = topLeft.X; corners[3].Y = bottomRight.Y; options.Transform.TransformPoints(corners); GraphicsPath path = new GraphicsPath(); PointF[] pts = new PointF[4]; for (int i = 0; i < corners.Length; i++) { pts[i].X = (float)corners[i].X; pts[i].Y = (float)corners[i].Y; } path.AddPolygon(pts); graphics.SetClip(path, System.Drawing.Drawing2D.CombineMode.Intersect); using (var brush = new SolidBrush(Color.FromArgb(64, Color.Black))) { foreach (var character in _documents[_currentPageIndex].DocumentText.Characters) { var bounds = character.Bounds; var text = new string(new char[] { character.Code }); DrawBounds(graphics, Pens.Yellow, brush, Brushes.Yellow, Font, text, character.Bounds, options.Transform); } } } base.OnPaint(e); }