LoadFromFile Method (original) (raw)
Summary
Creates an SvgDocument object from an SVG file on disk.
Syntax
C#
Objective-C
C++/CLI
Java
Python
- (nullable instancetype)initWithFile:(NSString *)file
options:(nullable LTSvgLoadOptions *)_options_
error:(NSError **)error
public static SvgDocument loadFromFile(String fileName, SvgLoadOptions 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.
Example
This example will use Leadtools.Document.Writer.DocumentWriter to create a PDF file from SVG files.
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:\LEADTOOLS22\Resources\Images";
}