←Select platform

AppendLtd Method

Summary
Appends one LEADTOOLS Temporary Document (LTD) file to another.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void AppendLtd( 
   string sourceFileName, 
   string destFileName 
) 
- (BOOL)appendLtdFile:(NSString *)srcLtdFile  
   destinationLtdFile:(NSString *)dstLtdFile  
                error:(NSError **)error 
public void appendLtd(String sourceFileName, String destFileName) 
public: 
void AppendLtd(  
   String^ sourceFileName, 
   String^ destFileName 
)  
def AppendLtd(self,sourceFileName,destFileName): 

Parameters

sourceFileName
The source LTD file.

destFileName
The destination LTD file.

Remarks

You can use this method to append several LTD files together before calling Convert. The example below shows how this can be useful in a multi-threading OCR solution. sourceFileName must exist on disk before calling this method, otherwise, an exception will be thrown. destFileName may or may not exist prior to calling this method, if it does not exist, a new file is created and the data is copied as is from the source file.

When this method returns,  destFileName will contain all the pages from the source and destination file (if exists), for example if the source file has 10 pages and the destination file has 20 pages, the result will be an LTD with 30 pages in  destFileName with the 10 pages from the source file appended to the end of the destination file at page index 20, 21, 22, etc.

The source file name is never changed by this method.

Example

This example will show how to use multiple threads to speed up OCR recognition of a multipage image file. This example shows part of internal functionality already achieved with the IOcrAutoRecognizeManager class.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 
 
 
public void AppendLtdExample() 
{ 
   // Get a multi-page source file 
   var inputFileName = GetImageFileName(); 
   var outputFileName = Path.Combine(LEAD_VARS.ImagesDir, "AppendLtdExample.pdf"); 
 
   // This is the LTD file we will use to append all recognition data 
   string mainLtdFileName = Path.GetTempFileName(); 
 
   if (File.Exists(outputFileName)) 
      File.Delete(outputFileName); 
 
   if (File.Exists(mainLtdFileName)) 
      File.Delete(mainLtdFileName); 
 
   // Use OCR Advantage engine 
   using (var ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
 
      // We can re-use LTD files, so create one 
      var pageLtdFileName = Path.GetTempFileName(); 
 
      // Get number of pages 
      var pageCount = ocrEngine.RasterCodecsInstance.GetTotalPages(inputFileName); 
      for (var pageNumber = 1; pageNumber <= pageCount; pageNumber++) 
      { 
         // OCR this page and save it as LTD 
         Debug.WriteLine("Processing page {0} of {1}", pageNumber, pageCount); 
         RecognizeAndSaveLtd(ocrEngine, inputFileName, pageNumber, pageLtdFileName); 
 
         // Append this LTD to the main one 
         // Notice, first time, the main LTD does not exist, AppendLtd will 
         // just copy the data over from the source file 
         ocrEngine.DocumentWriterInstance.AppendLtd(pageLtdFileName, mainLtdFileName); 
      } 
 
      // No need for this anymore 
      File.Delete(pageLtdFileName); 
 
      // We are done, convert the LTD to final format, here, we will 
      // use PDF 
      Debug.WriteLine("Converting to final format"); 
      ocrEngine.DocumentWriterInstance.Convert(mainLtdFileName, outputFileName, DocumentFormat.Pdf); 
   } 
 
   Debug.WriteLine("Success, file {0} is created", outputFileName); 
} 
 
private static void RecognizeAndSaveLtd(IOcrEngine ocrEngine, string inputFileName, int pageNumber, string pageLtdFileName) 
{ 
   // Delete the LTD file if it exists so we can put fresh data in it 
   if (File.Exists(pageLtdFileName)) 
      File.Delete(pageLtdFileName); 
 
   // Create an OCR document 
   using (var ocrDocument = ocrEngine.DocumentManager.CreateDocument()) 
   { 
      // Load the page 
      Debug.WriteLine("  Loading the page"); 
      ocrDocument.Pages.AddPages(inputFileName, pageNumber, pageNumber, null); 
 
      var ocrPage = ocrDocument.Pages[0]; 
 
      // Auto-zone it 
      Debug.WriteLine("  Auto-zoning the page"); 
      ocrPage.AutoZone(null); 
 
      // Recognize it 
      Debug.WriteLine("  Recognizing the page"); 
      ocrPage.Recognize(null); 
 
      // Save it 
      Debug.WriteLine("  Saving the page"); 
      ocrDocument.Save(pageLtdFileName, DocumentFormat.Ltd, null); 
   } 
} 
 
private static string GetImageFileName() 
{ 
   var pageTileTemplate = Path.Combine(LEAD_VARS.ImagesDir, "Ocr{0}.tif"); 
   var multiPageImageFileName = Path.Combine(LEAD_VARS.ImagesDir, "AppendLtdExample.tif"); 
 
   if (File.Exists(multiPageImageFileName)) 
      File.Delete(multiPageImageFileName); 
 
   // Create a multi-page TIF file by stitching OCR1 to OCR4.tif shipped with LEADTOOLS 
   using (var codecs = new RasterCodecs()) 
   { 
      RasterImage finalImage = null; 
 
      for (int page = 1; page <= 4; page++) 
      { 
         var pageImage = codecs.Load(string.Format(pageTileTemplate, page)); 
         if (finalImage == null) 
         { 
            finalImage = pageImage; 
         } 
         else 
         { 
            finalImage.AddPage(pageImage); 
            pageImage.Dispose(); 
         } 
      } 
 
      // Save the final image 
      codecs.Save(finalImage, multiPageImageFileName, RasterImageFormat.CcittGroup4, 1); 
   } 
 
   return multiPageImageFileName; 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime"; 
} 
 
import static org.junit.Assert.assertTrue; 
 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.IOException; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
 
 
public void AppendLtdExample() throws IOException { 
   final String OCR_LEAD_RUNTIME_DIR = "C:\\LEADTOOLS23\\Bin\\Common\\OcrLEADRuntime"; 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
 
   // Get a multi-page source file 
   String inputFileName = GetImageFileName(); 
   String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "Ocr1.pdf"); 
 
   // This is the LTD file we will use to append all recognition data 
   File file = File.createTempFile(combine(LEAD_VARS_IMAGES_DIR, "main"), ".PDF"); 
   String mainLtdFileName = file.getName(); 
   System.out.println(mainLtdFileName); 
   File file1 = new File(outputFileName); 
   if (file1.exists()) 
      file1.delete(); 
 
   File file2 = new File(mainLtdFileName); 
   if (file2.exists()) 
      file2.delete(); 
 
   // Use OCR Advantage engine 
   OcrEngine ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
   ocrEngine.startup(null, null, null, OCR_LEAD_RUNTIME_DIR); 
 
   // We can re-use LTD files, so create one 
   String pageLtdFileName = file.getAbsolutePath(); 
   System.out.println(pageLtdFileName); 
 
   // Get number of pages 
   int pageCount = ocrEngine.getRasterCodecsInstance().getTotalPages(inputFileName); 
   for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) { 
      // OCR this page and save it as LTD 
      System.out.printf("Processing page %s of %s", pageNumber, pageCount); 
      RecognizeAndSaveLtd(ocrEngine, inputFileName, pageNumber, pageLtdFileName); 
 
      // Append this LTD to the main one 
      // Notice, first time, the main LTD does not exist, AppendLtd will 
      // just copy the data over from the source file 
      ocrEngine.getDocumentWriterInstance().appendLtd(pageLtdFileName, mainLtdFileName); 
   } 
 
   // No need for this anymore 
   file.delete(); 
 
   // We are done, convert the LTD to final format, here, we will 
   // use PDF 
   System.out.println("Converting to final format"); 
   ocrEngine.getDocumentWriterInstance().convert(mainLtdFileName, outputFileName, DocumentFormat.PDF); 
 
   System.out.printf("Success, file %s is created", outputFileName); 
 
} 
 
private static void RecognizeAndSaveLtd(OcrEngine ocrEngine, String inputFileName, int pageNumber, 
      String pageLtdFileName) { 
 
   // Delete the LTD file if it exists so we can put fresh data in it 
   File file = new File(pageLtdFileName); 
   if (file.exists()) 
      file.delete(); 
 
   // Create an OCR document 
   OcrDocument ocrDocument = ocrEngine.getDocumentManager().createDocument(); 
   ILeadStream leadStream = LeadStreamFactory.create(inputFileName); 
 
   // Load the page 
   System.out.println("  Loading the page"); 
   ocrDocument.getPages().addPages(leadStream, pageNumber, pageNumber, null); 
 
   OcrPage ocrPage = ocrDocument.getPages().get(0); 
 
   // Auto-zone it 
   System.out.println("  Auto-zoning the page"); 
   ocrPage.autoZone(null); 
 
   // Recognize it 
   System.out.println("  Recognizing the page"); 
   ocrPage.recognize(null); 
 
   // Save it 
   System.out.println("  Saving the page"); 
   ocrDocument.save(pageLtdFileName, DocumentFormat.LTD, null); 
 
} 
 
private static String GetImageFileName() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String pageTileTemplate = combine(LEAD_VARS_IMAGES_DIR, "ocr1.tif"); 
   String multiPageImageFileName = combine(LEAD_VARS_IMAGES_DIR, "exam.tif"); 
 
   File file = new File(multiPageImageFileName); 
   if (file.exists()) 
      file.delete(); 
 
   // Create a multi-page TIF file by stitching OCR1 to OCR4.tif shipped with 
   // LEADTOOLS 
   RasterCodecs codecs = new RasterCodecs(); 
   RasterImage finalImage = null; 
 
   for (int page = 1; page <= 4; page++) { 
      RasterImage pageImage = codecs.load(String.format(pageTileTemplate, page)); 
      if (finalImage == null) { 
         finalImage = pageImage; 
      } else { 
         finalImage.addPage(pageImage); 
         pageImage.dispose(); 
      } 
   } 
 
   // Save the final image 
   codecs.save(finalImage, multiPageImageFileName, RasterImageFormat.CCITT_GROUP4, 1); 
 
   return multiPageImageFileName; 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document.Writer Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.