←Select platform

Render Method

Summary
Renders this SvgDocument to an output engine.
Syntax
C#
VB
C++
Java
Public Sub Render( _ 
   ByVal engine As IRenderingEngine, _ 
   ByVal options As SvgRenderOptions _ 
)  
public void render(IRenderingEngine engine, SvgRenderOptions options) 

Parameters

engine
Rendering engine to use for the target device. Cannot be null.

options
Options to use during rendering. Can be null.

Remarks

For more information, refer to SVG Rendering.

Use Rasterize to render this SVG document into the surface of a LEADTOOLS Leadtools.RasterImage object.

This method will throw an exception if this document is not flat (the value of IsFlat is false) or if it does not have valid physical (pixel) bounds (the value of Bounds.IsValid is false).

If the value of options is null, then the following default options will be used:

Member Value
SvgRenderOptions.Bounds

The current physical bounds of document (Bounds.Bounds)

SvgRenderOptions.ClipBounds

Empty rectangle for rendering the document

SvgRenderOptions.Transform

Identity Leadtools.LeadMatrix

SvgRenderOptions.UseBackgroundColor

true

BackgroundColor

White color

Example

This example will convert a page from a PDF file to SVG format and render it on a form.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
using Leadtools.Forms.DocumentWriters; 
using Leadtools.Svg; 
using LeadtoolsExamples.Common; 
 
public void SvgDocumentRenderExample() 
{ 
   // Create a form with a picture box 
   Form form = new Form(); 
   PictureBox pictureBox = new PictureBox(); 
   pictureBox.Dock = DockStyle.Fill; 
   form.Controls.Add(pictureBox); 
 
   SvgDocument document = null; 
 
   // Load a page from a document file as SVG 
   string srcFileName = Path.Combine(ImagesPath.Path, "Leadtools.pdf"); 
   using (var codecs = new RasterCodecs()) 
   { 
      // Set 300 as the default value for loading document files 
      codecs.Options.RasterizeDocument.Load.Resolution = 300; 
 
      document = codecs.LoadSvg(srcFileName, 1, null) as SvgDocument; 
   } 
 
   // Make sure the document is valid for rendering 
   if (!document.IsFlat) 
      document.Flat(null); 
   if (!document.Bounds.IsValid) 
      document.CalculateBounds(false); 
 
   // Optimize it for rendering to increase the speed 
   document.BeginRenderOptimize(); 
   Console.WriteLine("IsRenderOptimized is " + document.IsRenderOptimized); 
 
   // This is our paint code 
   pictureBox.Paint += (sender, e) => 
   { 
      Graphics graphics = e.Graphics; 
 
      // We will fit and center this SVG document in the client area of the picture box 
      Rectangle dstBounds = pictureBox.ClientRectangle; 
      if (dstBounds.Width < 1 || dstBounds.Height < 1) 
         return; 
 
      // Create the transformation matrix 
      LeadMatrix transform = LeadMatrix.Identity; 
      LeadRectD srcBounds = document.Bounds.Bounds; 
 
      // Calculate the zoom so we can fit 
      double zoom = 1.0; 
      if (dstBounds.Width > dstBounds.Height) 
      { 
         zoom = dstBounds.Width / srcBounds.Width; 
         if ((zoom * srcBounds.Height) > dstBounds.Height) 
            zoom = dstBounds.Height / srcBounds.Height; 
      } 
      else 
      { 
         zoom = dstBounds.Height / srcBounds.Height; 
         if ((zoom * srcBounds.Width) > dstBounds.Width) 
            zoom = dstBounds.Width / srcBounds.Width; 
      } 
 
      // We have the zoom factor, set it in the transform 
      transform.Scale(zoom, zoom); 
 
      // Center 
      double xOffset = (dstBounds.Width - (zoom * srcBounds.Width)) / 2.0; 
      double yOffset = (dstBounds.Height - (zoom * srcBounds.Height)) / 2.0; 
      transform.Translate(xOffset, yOffset); 
 
      // Now setup the rendering options 
      SvgRenderOptions options = new SvgRenderOptions(); 
      // Use our transform 
      options.Transform = transform; 
      // clipping (if any) 
      options.ClipBounds = LeadRectD.Create(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height); 
      // Fill the background with a white color 
      options.UseBackgroundColor = true; 
      options.BackgroundColor = RasterColor.FromKnownColor(RasterKnownColor.White); 
      options.Bounds = srcBounds; 
 
      // Create a rendering engine 
      using (var engine = RenderingEngineFactory.Create(graphics)) 
      { 
         // Render the document 
         document.Render(engine, options); 
      } 
   }; 
 
   form.SizeChanged += (sender, e) => pictureBox.Invalidate(); 
 
   form.ShowDialog(); 
 
   document.Dispose(); 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Drawing 
Imports Leadtools.Forms.DocumentWriters 
Imports Leadtools.Svg 
 
Public Shared Sub SvgDocumentRenderExample() 
   ' Create a form with a picture box 
   Dim form As New Form() 
   Dim pictureBox As New PictureBox() 
   pictureBox.Dock = DockStyle.Fill 
   form.Controls.Add(pictureBox) 
 
   Dim document As SvgDocument = Nothing 
 
   ' Load a page from a document file as SVG 
   Dim srcFileName As String = Path.Combine(Common.ImagesPath.Path, "Leadtools.pdf") 
   Using codecs As New RasterCodecs() 
      ' Set 300 as the default value for loading document files 
      codecs.Options.RasterizeDocument.Load.Resolution = 300 
 
      document = DirectCast(codecs.LoadSvg(srcFileName, 1, Nothing), SvgDocument) 
   End Using 
 
   ' Make sure the document is valid for rendering 
   If Not document.IsFlat Then 
      document.Flat(Nothing) 
   End If 
   If Not document.Bounds.IsValid Then 
      document.CalculateBounds(False) 
   End If 
 
   ' Optimize it for rendering to increase the speed 
   document.BeginRenderOptimize() 
   Console.WriteLine("IsRenderOptimized is " + document.IsRenderOptimized.ToString()) 
 
   ' This is our paint code 
   AddHandler pictureBox.Paint, 
      Sub(sender As Object, e As PaintEventArgs) 
         Dim graphics As Graphics = e.Graphics 
 
         ' We will fit and center this SVG document in the client area of the picture box 
         Dim dstBounds As Rectangle = pictureBox.ClientRectangle 
         If dstBounds.Width < 1 OrElse dstBounds.Height < 1 Then 
            Return 
         End If 
 
         ' Create the transformation matrix 
         Dim transform As LeadMatrix = LeadMatrix.Identity 
         Dim srcBounds As LeadRectD = document.Bounds.Bounds 
 
         ' Calculate the zoom so we can fit 
         Dim zoom As Double = 1.0 
         If dstBounds.Width > dstBounds.Height Then 
            zoom = dstBounds.Width / srcBounds.Width 
            If (zoom * srcBounds.Height) > dstBounds.Height Then 
               zoom = dstBounds.Height / srcBounds.Height 
            End If 
         Else 
            zoom = dstBounds.Height / srcBounds.Height 
            If (zoom * srcBounds.Width) > dstBounds.Width Then 
               zoom = dstBounds.Width / srcBounds.Width 
            End If 
         End If 
 
         ' We have the zoom factor, set it in the transform 
         transform.Scale(zoom, zoom) 
 
         ' Center 
         Dim xOffset As Double = (dstBounds.Width - (zoom * srcBounds.Width)) / 2.0 
         Dim yOffset As Double = (dstBounds.Height - (zoom * srcBounds.Height)) / 2.0 
         transform.Translate(xOffset, yOffset) 
 
         ' Now setup the rendering options 
         Dim options As New SvgRenderOptions() 
         ' All of the document 
         options.Bounds = srcBounds 
         ' Use our transform 
         options.Transform = transform 
         ' clipping (if any) 
         options.ClipBounds = LeadRectD.Create(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height) 
         ' Fill the background with a white color 
         options.UseBackgroundColor = True 
         options.BackgroundColor = RasterColor.FromKnownColor(RasterKnownColor.White) 
 
         ' Create a rendering engine 
         Using engine As IRenderingEngine = RenderingEngineFactory.Create(graphics) 
            ' Render the document 
            document.Render(engine, options) 
         End Using 
      End Sub 
 
   AddHandler form.SizeChanged, 
      Sub(sender As Object, e As EventArgs) 
         pictureBox.Invalidate() 
      End Sub 
 
   form.ShowDialog() 
 
   document.Dispose() 
End Sub 
Requirements

Target Platforms

Help Version 21.0.2021.6.30
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Svg Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.