public void EnumerateElements(SvgEnumerateOptions options,SvgEnumerateElementsCallback callback,object userData)
Public Sub EnumerateElements( _ByVal options As SvgEnumerateOptions, _ByVal callback As SvgEnumerateElementsCallback, _ByVal userData As Object _)
- (BOOL)enumerateElementsWithOptions:(nullable LTSvgEnumerateOptions *)optionsuserData:(nullable id)userDatacallback:(BOOL (^)(LTSvgDocument *document, LTSvgNodeHandle *node, id _Nullable userData))callbackerror:(NSError **)error
public void enumerateElements(SvgEnumerateOptions options, SvgEnumerateElementsCallback callback, Object userData) public:void EnumerateElements(SvgEnumerateOptions^ options,SvgEnumerateElementsCallback^ callback,Object^ userData)
options
Options to use. Can be null.
callback
Callback to receive the elements (nodes). Cannot be null.
userData
Optional user data that will be passed to the callback.
Use this method to enumerate the elements of this SvgDocument using the specified options.
This method will throw an exception if this document is not flat (the value of IsFlat is false) or if it does not have valid physical (pixel) bounds (the value of Bounds.IsValid is false).
If the value of options is null, then the following default options will be used:
| Member | Value |
|---|---|
| SvgEnumerateOptions.EnumerateDirection |
This example will use EnumerateElements to extract the embedded images from an SVG file and save them to disk and replace each with a link to the file.
using Leadtools;using Leadtools.Codecs;using Leadtools.Drawing;using Leadtools.Forms.DocumentWriters;using Leadtools.Svg;using LeadtoolsExamples.Common;private static void SvgDocumentEnumerateElementsExample(){// The source PDF filevar sourceFile = @"C:\LEADTOOLS21\Resources\Images\Leadtools.pdf";var beforeSvgFile = @"C:\LEADTOOLS21\Resources\Images\Leadtools_before.svg";var afterSvgFile = @"C:\LEADTOOLS21\Resources\Images\Leadtools_after.svg";// Assume this is our Virtual Directoryvar virtualDirectory = "http://localhost/leadtools_images/svg/resources";// Assume this phrysical directory maps to the Virtual Directoryvar physicalDirectory = @"C:\LEADTOOLS21\Resources\Images\svg\resources";if (!Directory.Exists(physicalDirectory))Directory.CreateDirectory(physicalDirectory);// Our SVG element enumartion callbackSvgEnumerateElementsCallback callback = (SvgDocument document, SvgNodeHandle node, object userData) =>{if (node.ElementType == SvgElementType.Image){// Get the hrefvar href = node.GetAttributeValue("xlink:href");if (!string.IsNullOrEmpty(href)){// Parse it as data URIvar dataUri = SvgDataUri.Parse(href);// Check if it is a data URIif (dataUri.IsDataUri){// Yes, create a new file in a virtual directory// Show the dataURI propertiesConsole.WriteLine("Replacing data URI");Console.WriteLine("Format:" + dataUri.ImageFormat);if (dataUri.MediaLength > 0)Console.WriteLine("Media:" + dataUri.Href.Substring(dataUri.MediaOffset, dataUri.MediaLength));if (dataUri.CharSetOffset > 0)Console.WriteLine("CharSet:" + dataUri.Href.Substring(dataUri.CharSetOffset, dataUri.CharSetLength));elseConsole.WriteLine("CharSet: not set");Console.WriteLine("IsBase64:" + dataUri.IsBase64);Console.WriteLine("ImageFormat:" + dataUri.ImageFormat);var extension = dataUri.Extension;Console.WriteLine("Extension:" + dataUri.Extension);// Get a file namevar name = Guid.NewGuid().ToString().Replace("-", "") + "." + dataUri.Extension;// Save it// Full physical pathvar filePath = Path.Combine(physicalDirectory, name);dataUri.DecodeToFile(filePath);/* Alternatively you can save the data yourself using this codevar data = dataUri.Href.Substring(dataUri.ValueOffset, dataUri.ValueLength);// Save the datavar base64String = dataUri.Href.Substring(dataUri.ValueOffset, dataUri.ValueLength);byte[] rawData = Convert.FromBase64String(base64String);// Save it to diskFile.WriteAllBytes(filePath, rawData);*/// Finally replace the attribute in the image element with the URIvar virtualPath = virtualDirectory + "/" + name;node.SetAttributeValue("xlink:href", virtualPath);}else{Console.WriteLine("Does not contain a valid data URI.");}}}return true;};using (var rasterCodecs = new RasterCodecs()){// Use 300 DPI when loading document imagesrasterCodecs.Options.RasterizeDocument.Load.Resolution = 300;// Load the first page as SVGusing (var svg = rasterCodecs.LoadSvg(sourceFile, 1, null) as SvgDocument){if (!svg.IsFlat)svg.Flat(null);if (!svg.Bounds.IsValid)svg.CalculateBounds(false);// Save this SVG to disk, report the sizesvg.SaveToFile(beforeSvgFile, null);Console.WriteLine("Before unembedding the image, size is " + new FileInfo(beforeSvgFile).Length);// Now enumerate the elements to replace each embedded image with a URL// Since we are going to modify the SVG, call BeginUpdate/EndUpdate to speed up the processsvg.BeginUpdate();svg.EnumerateElements(new SvgEnumerateOptions { EnumerateDirection = SvgEnumerateDirection.TopToBottom }, callback, null);svg.EndUpdate();// Save this SVG to disk again, report the size, should be alot smaller since the image are unembedded and stored as external resourcessvg.SaveToFile(afterSvgFile, null);Console.WriteLine("Before unembedding the image, size is " + new FileInfo(afterSvgFile).Length);}}}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.DrawingImports Leadtools.Forms.DocumentWritersImports Leadtools.SvgPrivate Shared Sub SvgDocumentEnumerateElementsExample()' The source PDF fileDim sourceFile As String = "C:\LEADTOOLS21\Resources\Images\Leadtools.pdf"Dim beforeSvgFile As String = "C:\LEADTOOLS21\Resources\Images\Leadtools_before.svg"Dim afterSvgFile As String = "C:\LEADTOOLS21\Resources\Images\Leadtools_after.svg"' Assume this is our Virtual DirectoryDim virtualDirectory As String = "http://localhost/leadtools_images/svg/resources"' Assume this phrysical directory maps to the Virtual DirectoryDim physicalDirectory As String = "C:\LEADTOOLS21\Resources\Images\svg\resources"If Not Directory.Exists(physicalDirectory) Then Directory.CreateDirectory(physicalDirectory)' Our SVG element enumartion callbackDim callback As SvgEnumerateElementsCallback =Function(document As SvgDocument, node As SvgNodeHandle, userData As Object) As BooleanIf node.ElementType = SvgElementType.Image Then' Get the hrefDim href As String = node.GetAttributeValue("xlink:href")If Not String.IsNullOrEmpty(href) Then' Parse it as data URIDim dataUri As SvgDataUri = SvgDataUri.Parse(href)' Check if it is a data URIIf dataUri.IsDataUri Then' Yes, create a new file in a virtual directory' Show the dataURI propertiesConsole.WriteLine("Replacing data URI")Console.WriteLine("Format:" + dataUri.ImageFormat.ToString())If dataUri.MediaLength > 0 Then Console.WriteLine("Media:" + dataUri.Href.Substring(dataUri.MediaOffset, dataUri.MediaLength))If dataUri.CharSetOffset > 0 ThenConsole.WriteLine("CharSet:" + dataUri.Href.Substring(dataUri.CharSetOffset, dataUri.CharSetLength))ElseConsole.WriteLine("CharSet: not set")End IfConsole.WriteLine("IsBase64:" + dataUri.IsBase64.ToString())Console.WriteLine("ImageFormat:" + dataUri.ImageFormat.ToString())Dim extension As String = dataUri.ExtensionConsole.WriteLine("Extension:" + dataUri.Extension)' Get a file nameDim name As String = Guid.NewGuid().ToString().Replace("-", "") + "." + dataUri.Extension' Save it' Full physical pathDim filePath As String = Path.Combine(physicalDirectory, name)dataUri.DecodeToFile(filePath)' Alternatively you can save the data yourself using this code' Dim data As String = dataUri.Href.Substring(dataUri.ValueOffset, dataUri.ValueLength)' ' Save the data' Dim base64String As String = dataUri.Href.Substring(dataUri.ValueOffset, dataUri.ValueLength)' Dim rowData() As Byte = Convert.FromBase64String(base64String)'' ' Save it to disk' File.WriteAllBytes(filePath, rawData)' Finally replace the attribute in the image element with the URIDim virtualPath As String = virtualDirectory + "/" + namenode.SetAttributeValue("xlink:href", virtualPath)ElseConsole.WriteLine("Does not contain a valid data URI.")End IfEnd IfEnd IfReturn TrueEnd FunctionUsing rasterCodecs As New RasterCodecs()' Use 300 DPI when loading document imagesrasterCodecs.Options.RasterizeDocument.Load.Resolution = 300' Load the first page as SVGUsing svg As SvgDocument = CType(rasterCodecs.LoadSvg(sourceFile, 1, Nothing), SvgDocument)If Not svg.IsFlat Then svg.Flat(Nothing)If Not svg.Bounds.IsValid Then svg.CalculateBounds(False)' Save this SVG to disk, report the sizesvg.SaveToFile(beforeSvgFile, Nothing)Console.WriteLine("Before unembedding the image, size is " + New FileInfo(beforeSvgFile).Length.ToString())' Now enumerate the elements to replace each embedded image with a URL' Since we are going to modify the SVG, call BeginUpdate/EndUpdate to speed up the processsvg.BeginUpdate()Dim enumerationOptions As New SvgEnumerateOptionsenumerationOptions.EnumerateDirection = SvgEnumerateDirection.TopToBottomsvg.EnumerateElements(enumerationOptions, callback, Nothing)svg.EndUpdate()' Save this SVG to disk again, report the size, should be alot smaller since the image are unembedded and stored as external resourcessvg.SaveToFile(afterSvgFile, Nothing)Console.WriteLine("Before unembedding the image, size is " + New FileInfo(afterSvgFile).Length.ToString())End UsingEnd UsingEnd Sub
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
