public static SvgDocument LoadFromFile(
string fileName,
SvgLoadOptions options
)
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.
The SvgDocument object this method creates.
To get and set information about the document's bounds and resolution, refer to SVG Size, Bounds and Flat.
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:\LEADTOOLS23\Resources\Images";
}