←Select platform

LoadSvg(Stream,int,CodecsLoadSvgOptions) Method

Summary

Loads a page from a stream containing image, document or vector file as SVG

Syntax

C#
VB
Java
Objective-C
WinRT C#
C++
Public Overloads Function LoadSvg( _ 
   ByVal stream As Stream, _ 
   ByVal pageNumber As Integer, _ 
   ByVal options As Leadtools.Codecs.CodecsLoadSvgOptions _ 
) As Leadtools.ISvgDocument 
- (nullable id<ISvgDocument>)loadSvgStream:(LTLeadStream *)stream  
                                      page:(NSInteger)pageNumber  
                                   options:(nullable LTCodecsLoadSvgOptions *)options  
                                     error:(NSError **)error 
             
public ISvgDocument loadSvg( 
   ILeadStream stream,  
   int pageNumber,  
   CodecsLoadSvgOptions options 
) 
             
 function Leadtools.Codecs.RasterCodecs.LoadSvg(Stream,Int32,CodecsLoadSvgOptions)(  
   stream , 
   pageNumber , 
   options  
) 

Parameters

stream
The input stream.

pageNumber
1-based page number to load.

options
The options used for loading SVG. This can be null.

Return Value

SVG document representation of the page.

Remarks

Use this method to load a page from any supported image, document or vector file as SVG (Scalable Vector Graphics). The following conditions must be met to load a page from a file as SVG:

Condition Description
The file format is SVG

SVG can be loaded as SVG

The file format is document

Any of the document file formats supported by LEADTOOLS such DOCX/DOC, PPTX/PPT, XLSX/XLS, RTF, TXT, AFP, ICA, etc. These formats will set the CodecsDocumentImageInfo.IsDocumentFile property to true when calling GetInformation

The file format is vector

Any of the vector file formats supported by LEADTOOLS such as DXF, DWG, etc. These formats will set the CodecsVectorImageInfo.IsVectorFile property to true when calling GetInformation

The file format is PDF

And the PDF file contains more than pure raster data (for example, not scanned PDF file).

To find out if an input file can be loaded as SVG, use the CanLoadSvg method.

In addition to the usual format filter assembly (Leadtools.Codecs.*), The following additional assemblies may be required to support loading as SVG

Assembly Description
Leadtools.Svg

SVG support. Always required

Leadtools.Vector

Required if the input document is a vector file

Leadtools.Pdf

Required if the input document is a PDF file

Usually, the returned ISvgDocument is to be casted to Leadtools.Svg.SvgDocument to continue working with the other SVG features, such as retrieving its data, rendering it to a target or saving it to a separate file.

You must check the result SVG document flatness and perform the necessary operation before continuing.

To determine whether a file or stream can be loaded as SVG, use CanLoadSvg(string fileName) or CanLoadSvg(Stream stream).

To load as SVG from a disk file, use LoadSvg(string fileName, int pageNumber, CodecsLoadSvgOptions options).

To load as SVG asynchronously, use LoadSvgAsync.

For more information, refer to Working With SVG.

Example

This example will check a folder of document and images files for SVG support then loads the first page of each supported file and saves the result SVG.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
using LeadtoolsExamples.Common; 
 
public static void LoadSvgExample() 
{ 
   // input directory 
   string inDir = ImagesPath.Path; 
   // output directory 
   string outDir = Path.Combine(ImagesPath.Path, "svgpages"); 
   if (!Directory.Exists(outDir)) 
      Directory.CreateDirectory(outDir); 
 
   using (var codecs = new RasterCodecs()) 
   { 
      // Set 300 as the default value for loading document files 
      codecs.Options.RasterizeDocument.Load.Resolution = 300; 
 
      codecs.ThrowExceptionsOnInvalidImages = false; 
 
      // Get all the files from input directory 
      foreach (var srcFileName in Directory.EnumerateFiles(inDir)) 
      { 
         Console.WriteLine("Checking {0}", srcFileName); 
         using (var info = codecs.GetInformation(srcFileName, false)) 
         { 
            // We can load as SVG if its document or vector (skipping SVG files themselves) 
            if (info.Format != RasterImageFormat.Unknown && // valid format 
               info.Format != RasterImageFormat.Svg && // not svg 
               (info.Document.IsDocumentFile || // a document 
               info.Vector.IsVectorFile)) // or vector 
            { 
               // try to load the first page as SVG 
               try 
               { 
                  using (SvgDocument svgDocument = codecs.LoadSvg(srcFileName, 1, null) as SvgDocument) 
                  { 
                     // Save it to disk 
                     string name = Path.GetFileName(srcFileName).Replace(".", "-"); 
                     name = Path.ChangeExtension(name, "svg"); 
                     string dstFileName = Path.Combine(outDir, name); 
                     Console.WriteLine("Saving to {0}", dstFileName); 
                     svgDocument.SaveToFile(dstFileName, null); 
                  } 
               } 
               catch (Exception ex) 
               { 
                  Console.WriteLine(ex.Message); 
               } 
            } 
         } 
      } 
   } 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
Imports Leadtools.Drawing 
Imports Leadtools.Svg 
 
Public Shared Sub LoadSvgExample() 
   ' input directory 
   Dim inDir As String = LEAD_VARS.ImagesDir 
   ' output directory 
   Dim outDir As String = Path.Combine(LEAD_VARS.ImagesDir, "svgpages") 
   If Not Directory.Exists(outDir) Then 
      Directory.CreateDirectory(outDir) 
   End If 
 
   Using codecs As New RasterCodecs() 
      ' Set 300 as the default value for loading document files 
      codecs.Options.RasterizeDocument.Load.Resolution = 300 
 
      codecs.ThrowExceptionsOnInvalidImages = False 
 
      ' Get all the files from input directory 
      For Each srcFileName As String In Directory.EnumerateFiles(inDir) 
         Console.WriteLine("Checking {0}", srcFileName) 
         Using info As CodecsImageInfo = codecs.GetInformation(srcFileName, False) 
            ' We can load as SVG if its document or vector (skipping SVG files themselves) 
            If info.Format <> RasterImageFormat.Unknown AndAlso 
               info.Format <> RasterImageFormat.Svg AndAlso 
               (info.Document.IsDocumentFile OrElse 
               info.Vector.IsVectorFile) Then 
               ' try to load the first page as SVG 
               Try 
                  Using svgDocument As SvgDocument = DirectCast(codecs.LoadSvg(srcFileName, 1, Nothing), SvgDocument) 
                     ' Save it to disk 
                     Dim name As String = Path.GetFileName(srcFileName).Replace(".", "-") 
                     name = Path.ChangeExtension(name, "svg") 
                     Dim dstFileName As String = Path.Combine(outDir, name) 
                     Console.WriteLine("Saving to {0}", dstFileName) 
                     svgDocument.SaveToFile(dstFileName, Nothing) 
                  End Using 
               Catch ex As Exception 
                  Console.WriteLine(ex.Message) 
               End Try 
            End If 
         End Using 
      Next 
   End Using 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 

Requirements

Target Platforms

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Codecs Assembly