Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction - All Topics | Help Version 19.0.6.22
|
Leadtools.Svg Assembly > Leadtools.Svg Namespace > SvgDocument Class : LoadFromFile Method |
public static SvgDocument LoadFromFile( string fileName, SvgLoadOptions options )
'Declaration
Public Shared Function LoadFromFile( _ ByVal fileName As String, _ ByVal options As SvgLoadOptions _ ) As SvgDocument
'Usage
Dim fileName As String Dim options As SvgLoadOptions Dim value As SvgDocument value = SvgDocument.LoadFromFile(fileName, options)
- (nullable instancetype)initWithFile:(NSString *)file options:(nullable LTSvgLoadOptions *)options error:(NSError **)error
public static SvgDocument loadFromFile(String fileName, SvgLoadOptions options)
public: static SvgDocument^ LoadFromFile( String^ fileName, SvgLoadOptions^ options )
To get and set information about the document's bounds and resolution, refer to SVG Size, Bounds and Flat.
This example will use Leadtools.Forms.DocumentWriters.DocumentWriter to create a PDF file from SVG files.
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Drawing Imports Leadtools.Forms.DocumentWriters Imports Leadtools.Svg Public Shared Sub SvgLoadFromFileExample() ' Create the SVG pages we will be using Dim srcFileName As String = Path.Combine(Common.ImagesPath.Path, "Leadtools.doc") Dim dstFileName As String = Path.Combine(Common.ImagesPath.Path, "Example.pdf") Dim outDir As String = Path.Combine(Common.ImagesPath.Path, "TempSvgPages") If Not Directory.Exists(outDir) Then Directory.CreateDirectory(outDir) End If Dim pageCount As Integer = CreateSvgPages(srcFileName, outDir) ' Create a PDF document using Document Writer Dim documentWriter As New DocumentWriter() DocumentWriter.BeginDocument(dstFileName, DocumentFormat.Pdf) Dim svgPageTemplateName As String = Path.Combine(outDir, "Page{0}.svg") For pageNumber As Integer = 1 To pageCount ' Load this SVG Dim pageFileName As String = String.Format(svgPageTemplateName, pageNumber) Console.WriteLine("Loading {0}", pageFileName) Using svgDocument As SvgDocument = svgDocument.LoadFromFile(pageFileName, Nothing) ' Check if we need to flat it If Not svgDocument.IsFlat Then svgDocument.Flat(Nothing) End If If Not svgDocument.Bounds.IsValid Then svgDocument.CalculateBounds(False) End If ' Add it to the document writer Console.WriteLine("Adding ...") Dim svgPage As New DocumentSvgPage() svgPage.SvgDocument = svgDocument documentWriter.AddPage(svgPage) End Using Next ' Finish up Console.WriteLine("Finishing ...") documentWriter.EndDocument() End Sub Private Shared Function CreateSvgPages(srcFileName As String, outDir As String) As Integer ' Extract all the pages from the source file as SVG Using codecs As New RasterCodecs() ' Set 300 as the default value for loading document files codecs.Options.RasterizeDocument.Load.Resolution = 300 ' Get all the files from input directory Dim pageCount As Integer = codecs.GetTotalPages(srcFileName) For pageNumber As Integer = 1 To pageCount Using svgDocument As SvgDocument = DirectCast(codecs.LoadSvg(srcFileName, pageNumber, Nothing), SvgDocument) ' Save it to disk Dim dstFileName As String = Path.Combine(outDir, Path.Combine(String.Format("Page{0}.svg", pageNumber))) Console.WriteLine("Saving to {0}", dstFileName) svgDocument.SaveToFile(dstFileName, Nothing) End Using Next Return pageCount End Using End Function
using Leadtools; using Leadtools.Codecs; using Leadtools.Drawing; using Leadtools.Forms.DocumentWriters; using Leadtools.Svg; public void SvgLoadFromFileExample() { // Create the SVG pages we will be using string srcFileName = Path.Combine(ImagesPath.Path, "Leadtools.doc"); string dstFileName = Path.Combine(ImagesPath.Path, "Example.pdf"); string outDir = Path.Combine(ImagesPath.Path, "TempSvgPages"); if (!Directory.Exists(outDir)) Directory.CreateDirectory(outDir); int pageCount = CreateSvgPages(srcFileName, outDir); // Create a PDF document using Document Writer var documentWriter = new DocumentWriter(); documentWriter.BeginDocument(dstFileName, DocumentFormat.Pdf); string svgPageTemplateName = Path.Combine(outDir, "Page{0}.svg"); for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) { // Load this SVG string pageFileName = string.Format(svgPageTemplateName, pageNumber); Console.WriteLine("Loading {0}", pageFileName); using (SvgDocument svgDocument = SvgDocument.LoadFromFile(pageFileName, null)) { // Check if we need to flat it if (!svgDocument.IsFlat) svgDocument.Flat(null); if (!svgDocument.Bounds.IsValid) svgDocument.CalculateBounds(false); // Add it to the document writer Console.WriteLine("Adding ..."); DocumentSvgPage svgPage = new DocumentSvgPage(); svgPage.SvgDocument = svgDocument; documentWriter.AddPage(svgPage); } } // Finish up Console.WriteLine("Finishing ..."); documentWriter.EndDocument(); } private static int CreateSvgPages(string srcFileName, string outDir) { // Extract all the pages from the source file as SVG using (var codecs = new RasterCodecs()) { // Set 300 as the default value for loading document files codecs.Options.RasterizeDocument.Load.Resolution = 300; // Get all the files from input directory int pageCount = codecs.GetTotalPages(srcFileName); for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) { using (SvgDocument svgDocument = codecs.LoadSvg(srcFileName, pageNumber, null) as SvgDocument) { // Save it to disk string dstFileName = Path.Combine(outDir, Path.Combine(string.Format("Page{0}.svg", pageNumber))); Console.WriteLine("Saving to {0}", dstFileName); svgDocument.SaveToFile(dstFileName, null); } } return pageCount; } }