←Select platform

LoadFromFile Method

Summary
Creates an SvgDocument object from an SVG file on disk.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public static SvgDocument LoadFromFile( 
   string fileName, 
   SvgLoadOptions 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 
)  
def LoadFromFile(self,fileName,options): 

Parameters

fileName
Path to the SVG file on disk

options
Options to use during load. If this parameter is null, then a default SvgLoadOptions object will be used.

Return Value

The SvgDocument object this method creates.

Remarks

To get and set information about the document's bounds and resolution, refer to SVG Size, Bounds and Flat.

Example

This example will use Leadtools.Document.Writer.DocumentWriter to create a PDF file from SVG files.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
using Leadtools.Forms.DocumentWriters; 
using Leadtools.Svg; 
 
using Leadtools.Document.Writer; 
 
public void SvgLoadFromFileExample() 
{ 
   // Create the SVG pages we will be using 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.doc"); 
   string outDir = Path.Combine(LEAD_VARS.ImagesDir, "TempSvgPages"); 
   string dstFileName = Path.Combine(outDir, "Example.pdf"); 
 
   if (!Directory.Exists(outDir)) 
      Directory.CreateDirectory(outDir); 
 
   int pageCount = CreateSvgPages(srcFileName, outDir); 
 
   // Create a PDF document using Document Writer 
   var documentWriter = new Leadtools.Document.Writer.DocumentWriter(); 
   documentWriter.BeginDocument(dstFileName, Leadtools.Document.Writer.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 ..."); 
         DocumentWriterSvgPage svgPage = new DocumentWriterSvgPage(); 
         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; 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.PrintStream; 
import java.nio.file.FileVisitOption; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.stream.Stream; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.document.writer.*; 
import leadtools.svg.*; 
 
 
public void svgDocumentLoadFromFile() { 
   String LEAD_VARS_ImagesDir = "C:\\LEADTOOLS23\\Resources\\Images"; 
   // Create the SVG pages we will be using 
   String srcFileName = combine(LEAD_VARS_ImagesDir, "Leadtools.pdf"); 
   String outDir = combine(LEAD_VARS_ImagesDir, "TempSvgPages"); 
   String dstFileName = combine(outDir, "Example.pdf"); 
 
   if (!new File(outDir).exists()) 
      new File(outDir).mkdirs(); 
 
   int pageCount = createSvgPages(srcFileName, outDir); 
 
   // Crreate a PDF document using Document Writer 
   DocumentWriter documentWriter = new DocumentWriter(); 
   documentWriter.beginDocument(dstFileName, DocumentFormat.PDF); 
 
   String svgPageTemplateName = combine(outDir, "Page%d.svg"); 
   for (int i = 1; i <= pageCount; i++) { 
      // Load this SVG 
      String pageFileName = String.format(svgPageTemplateName, i); 
      System.out.printf("Loading %s%n", pageFileName); 
      SvgDocument svgDocument = SvgDocument.loadFromFile(pageFileName, null); 
      // Check if we need to flat it 
      if (!svgDocument.isFlat()) 
         svgDocument.flat(null); 
      if (!svgDocument.getBounds().isValid()) 
         svgDocument.calculateBounds(false); 
 
      // Add it to the document writer 
      System.out.println("Adding....."); 
      DocumentWriterSvgPage svgPage = new DocumentWriterSvgPage(); 
      svgPage.setSvgDocument(svgDocument); 
      documentWriter.addPage(svgPage); 
   } 
 
   assertTrue(new File(dstFileName).exists()); 
   // Finish up 
   System.out.println("Finishing...."); 
   documentWriter.endDocument(); 
} 
 
public int createSvgPages(String srcFileName, String outDir) { 
   // Extract all the pages from the source file as SVG 
   RasterCodecs codecs = new RasterCodecs(); 
   // Set 300 as the default value for loading document files 
   codecs.getOptions().getRasterizeDocument().getLoad().setResolution(300); 
   // Get all the files from input directory 
   int pageCount = codecs.getTotalPages(srcFileName); 
 
   for (int i = 1; i <= pageCount; i++) { 
      SvgDocument svgDocument = (SvgDocument) codecs.loadSvg(srcFileName, i, null); 
      String dstFileName = combine(outDir, String.format("Page%d.svg", i)); 
      System.out.printf("Saving to %s%n", dstFileName); 
      // Save it to disk 
      svgDocument.saveToFile(dstFileName, null); 
   } 
   return pageCount; 
 
} 
Requirements

Target Platforms

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

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