←Select platform

SortElements Method

Summary

Enumerates the elements of this SvgDocument in a sorted manner.

Syntax

C#
VB
Java
Objective-C
C++
public void SortElements( 
   SvgSortOptions options, 
   SvgSortElementsCallback callback, 
   object userData 
) 
Public Sub SortElements( _ 
   ByVal options As Leadtools.Svg.SvgSortOptions, _ 
   ByVal callback As Leadtools.Svg.SvgSortElementsCallback, _ 
   ByVal userData As Object _ 
)  
- (BOOL)sortElementsWithOptions:(nullable LTSvgSortOptions *)options 
                       userData:(nullable id)userData 
                       callback:(BOOL (^)(LTSvgDocument *document, LTSvgElementInfo *info, id _Nullable userData))callback 
                          error:(NSError **)error 
             
public void sortElements(SvgSortOptions options, SvgSortElementsCallback callback, Object userData) 

Parameters

options
Options to use. Can be null.

callback
Callback to receive the sorted elements. Cannot be null.

userData
Optional user data that will be passed to the callback.

Remarks

Use this method to enumerate the elements of this SvgDocument in a controlled, sorted manner.

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
SvgSortOptions.SortFlags

SvgSortFlags.Default

SvgSortOptions.ExtractText

SvgExtractText.Character

The value of SvgSortOptions.ExtractText controls how the engine parses the text of the document, as follows:

Value Description
SvgExtractText.Character

Extract the text one character at a time: callback will be called for each character. SvgTextData.Text will contain a single character.

SvgExtractText.Word

Extract the text one word at a time: callback will be called for each word. SvgTextData.Text will contain a word of one or more characters.

SvgExtractText.Line

Extract the text one line at a time: callback will be called for each line. SvgTextData.Text will contain a line of one or more words (and one or more characters).

Example

This example will use SortElements to load a page from a PDF as SVG and then create a text file with the strings found.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
using Leadtools.Forms.DocumentWriters; 
using Leadtools.Svg; 
using LeadtoolsExamples.Common; 
 
public void SortElementsExample() 
{ 
   // The source PDF file 
   string srcFileName = Path.Combine(ImagesPath.Path, "Leadtools.pdf"); 
   string dstFileName = Path.Combine(ImagesPath.Path, "Example.txt"); 
 
   // SVG sort callback handler 
   SvgSortElementsCallback sortCallback = (callabackDocument, info, userData) => 
   { 
      StreamWriter writer = userData as StreamWriter; 
      // Is it text? 
      SvgTextData textData = info.TextData; 
      if (textData != null) 
      { 
         // Yes, print it to the console 
         writer.Write(textData.Text + " "); 
 
         // See if its end of line 
         var len = textData.Text.Length; 
         if ((textData.CharacterFlags[len - 1] & SvgTextCharacterFlags.EndOfLine) == SvgTextCharacterFlags.EndOfLine) 
            writer.WriteLine(); 
      } 
 
      return true; 
   }; 
 
   using (var codecs = new RasterCodecs()) 
   { 
      // Set 300 as the default value for loading document files 
      codecs.Options.RasterizeDocument.Load.Resolution = 300; 
 
      // get the number of pages 
      int pageCount = codecs.GetTotalPages(srcFileName); 
 
      // Create a writer for the output text file 
      using (StreamWriter writer = File.CreateText(dstFileName)) 
      { 
         for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) 
         { 
            // Load this page as SVG, we are interested in the text only so 
            // we will ask LEADTOOLS to skip other elements 
            CodecsLoadSvgOptions loadSvgOptions = new CodecsLoadSvgOptions(); 
            loadSvgOptions.DropShapes = false; 
            loadSvgOptions.DropImages = true; 
            loadSvgOptions.DropShapes = true; 
            using (SvgDocument svgDocument = codecs.LoadSvg(srcFileName, pageNumber, loadSvgOptions) as SvgDocument) 
            { 
               // Sort requires a flat document, so check for that 
               if (!svgDocument.IsFlat) 
                  svgDocument.Flat(null); 
 
               if (!svgDocument.Bounds.IsValid) 
                  svgDocument.CalculateBounds(false); 
 
               SvgSortOptions sortOptions = new SvgSortOptions(); 
               sortOptions.ExtractText = SvgExtractText.Word; 
               sortOptions.SortFlags = SvgSortFlags.Default; 
               Console.WriteLine("Text for page {0}", pageNumber); 
               svgDocument.SortElements(sortOptions, sortCallback, writer); 
            } 
         } 
 
         // Show the text file 
         System.Diagnostics.Process.Start(dstFileName); 
      } 
   } 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Drawing 
Imports Leadtools.Forms.DocumentWriters 
Imports Leadtools.Svg 
 
Public Shared Sub SortElementsExample() 
   ' The source PDF file 
   Dim srcFileName As String = Path.Combine(Common.ImagesPath.Path, "Leadtools.pdf") 
   Dim dstFileName As String = Path.Combine(Common.ImagesPath.Path, "Example.txt") 
 
   ' SVG sort callback handler 
   Dim sortCallback As SvgSortElementsCallback = 
      Function(document As SvgDocument, info As SvgElementInfo, userData As Object) As Boolean 
         Dim writer As StreamWriter = DirectCast(userData, StreamWriter) 
         ' Is it text? 
         Dim textData As SvgTextData = info.TextData 
         If Not IsNothing(textData) Then 
            ' Yes, print it to the console 
            writer.Write(textData.Text + " ") 
 
            ' See if its end of line 
            Dim len As Integer = textData.Text.Length 
            If (textData.CharacterFlags(len - 1) And SvgTextCharacterFlags.EndOfLine) = SvgTextCharacterFlags.EndOfLine Then 
               writer.WriteLine() 
            End If 
         End If 
 
         Return True 
      End Function 
 
   Using codecs As New RasterCodecs() 
      ' Set 300 as the default value for loading document files 
      codecs.Options.RasterizeDocument.Load.Resolution = 300 
 
      ' get the number of pages 
      Dim pageCount As Integer = codecs.GetTotalPages(srcFileName) 
 
      ' Create a writer for the output text file 
      Using writer As StreamWriter = File.CreateText(dstFileName) 
         For pageNumber As Integer = 1 To pageCount 
            ' Load this page as SVG, we are interested in the text only so 
            ' we will ask LEADTOOLS to skip other elements 
            Dim loadSvgOptions As New CodecsLoadSvgOptions() 
            loadSvgOptions.DropText = False 
            loadSvgOptions.DropImages = True 
            loadSvgOptions.DropShapes = True 
            Using svgDocument As SvgDocument = DirectCast(codecs.LoadSvg(srcFileName, pageNumber, loadSvgOptions), SvgDocument) 
               ' Sort requires a flat document, so check for that 
               If Not svgDocument.IsFlat Then 
                  svgDocument.Flat(Nothing) 
               End If 
 
               If Not svgDocument.Bounds.IsValid Then 
                  svgDocument.CalculateBounds(False) 
               End If 
 
               Dim sortOptions As New SvgSortOptions() 
               sortOptions.ExtractText = SvgExtractText.Word 
               sortOptions.SortFlags = SvgSortFlags.Default 
               Console.WriteLine("Text for page {0}", pageNumber) 
               svgDocument.SortElements(sortOptions, sortCallback, writer) 
            End Using 
         Next 
 
         ' Show the text file 
         System.Diagnostics.Process.Start(dstFileName) 
      End Using 
   End Using 
End Sub 

Requirements

Target Platforms

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Svg Assembly