←Select platform

SortElements Method

Summary
Enumerates the elements of this SvgDocument in a sorted manner.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void SortElements( 
   SvgSortOptions options, 
   SvgSortElementsCallback callback, 
   object userData 
) 
- (BOOL)sortElementsWithOptions:(nullable LTSvgSortOptions *)options userData:(nullable id)userData callback:(BOOL (^)(LTSvgDocument *document, LTSvgElementInfo *info, id _Nullable userData))callback error:(NSError **)error NS_SWIFT_NAME(sortElements(options:userData:callback:)); 
public void sortElements(SvgSortOptions options, SvgSortElementsCallback callback, Object userData) 
public: 
void SortElements(  
   SvgSortOptions^ options, 
   SvgSortElementsCallback^ callback, 
   Object^ userData 
)  
def SortElements(self,callback,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#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
using Leadtools.Forms.DocumentWriters; 
using Leadtools.Svg; 
 
using Leadtools.Document.Writer; 
 
public void SortElementsExample() 
{ 
   // The source PDF file 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf"); 
   string dstFileName = Path.Combine(LEAD_VARS.ImagesDir, "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); 
      } 
   } 
} 
 
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 svgDocumentSortElementsExample() { 
   String LEAD_VARS_ImagesDir = "C:\\LEADTOOLS23\\Resources\\Images"; 
   // The source PDF file 
   String srcFilePath = combine(LEAD_VARS_ImagesDir, "Leadtools_pdf.svg"); 
   String dstFilePath = combine(LEAD_VARS_ImagesDir, "Example.txt"); 
 
   // SVG sort callback handler 
   SvgSortElementsCallback sortCallback = (callbackDocument, info, userData) -> { 
      try { 
         FileOutputStream writer = (FileOutputStream) userData; 
         PrintStream printWriter = new PrintStream(writer); 
         // Is it text? 
         SvgTextData textData = info.getTextData(); 
         if (textData.getText() != null) { 
            // Yes, print it to the console 
            printWriter.print(textData.getText() + " "); 
 
            // See if its end of line 
            int length = textData.getText().length(); 
            if ((textData.getCharacterFlags()[length - 1] 
                  & SvgTextCharacterFlags.END_OF_LINE.getValue()) == SvgTextCharacterFlags.END_OF_LINE.getValue()) 
               printWriter.println(); 
         } 
         writer.flush(); 
      } catch (Exception e) { 
         assertFalse(true); 
         e.printStackTrace(); 
      } 
      return true; 
   }; 
 
   RasterCodecs codecs = new RasterCodecs(); 
   // Set 300 as the default value for loading document files 
   codecs.getOptions().getRasterizeDocument().getLoad().setResolution(300); 
   // get the number of pages 
   int pageCount = codecs.getTotalPages(srcFilePath); 
 
   // Create a stream for the output text file 
   try (FileOutputStream fos = new FileOutputStream(dstFilePath)) { 
      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.setDropImages(true); 
         loadSvgOptions.setDropShapes(true); 
 
         SvgDocument svgDocument = (SvgDocument) codecs.loadSvg(srcFilePath, pageNumber, loadSvgOptions); 
         // Sort requires a flat document, so check for that 
         if (!svgDocument.isFlat()) 
            svgDocument.flat(null); 
         if (!svgDocument.getBounds().isValid()) 
            svgDocument.calculateBounds(false); 
 
         SvgSortOptions sortOptions = new SvgSortOptions(); 
         sortOptions.setExtractText(SvgExtractText.WORD.getValue()); 
         sortOptions.setSortFlags(SvgSortFlags.DEFAULT.getValue()); 
         System.out.println("Text for page " + pageNumber); 
         svgDocument.sortElements(sortOptions, sortCallback, fos); 
      } 
   } catch (Exception exception) { 
      assertFalse(true); 
      exception.printStackTrace(); 
   } 
 
   System.out.println(sortCallback); 
 
} 
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.