←Select platform

SaveToFile Method

Summary

Exports this document to the specified file using the specified format.

Syntax
C#
C++/CLI
Java
Python
public void SaveToFile( 
   string fileName, 
   SaveDocumentOptions options 
) 
public:  
   void SaveToFile( 
      String^ fileName, 
      SaveDocumentOptions^ options 
   ) 
public void saveToFile(String fileName, SaveDocumentOptions options) 
def SaveToFile(self,fileName,options): 

Parameters

fileName

Target file name. This cannot be null.

options

Options to use when saving the document. If this is null, then the document will be saved

using default options.

Remarks

Use SaveToFile or SaveToUri to export this document to an external file or remote URL.

These methods support saving the document to a raster image format, not a document. In most cases, converting a document should be performed with more options and control using the DocumentConverter class.

The document will be saved with the latest changed if it has been modified, for example, if the original file has 4 pages and the user deleted page number 1, then the saved file will contain 3 pages. Similarly, if the user called SetImage to replace the raster image for any of the pages, then the saved file will contain the new version for this page.

SaveDocumentOptions is used as follows:

Member Description

Format

Any of the LEADTOOLS supported RasterImageFormat values can be used.

If this value is the default (RasterImageFormat.Unknown), then this document will be saved using the format of the original document.

BitsPerPixel

Bits per pixel value to use. Use 0 for the default value supported by Format.

AnnotationsUri

If not null, then it should contain the path to where the annotations file will be saved.

WebClient

The .NET System.Net.WebClient object to use when uploading the data to the remote URL.

Only used with SaveToUri.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Annotations.Engine; 
using Leadtools.Ocr; 
using Leadtools.Barcode; 
using Leadtools.Document.Converter; 
 
public void DocumentSaveToFileExample() 
{ 
   var options = new LoadDocumentOptions(); 
   using (var document = DocumentFactory.LoadFromFile(Path.Combine(LEAD_VARS.ImagesDir, "Protected.tif"), options)) 
   { 
      // The document is read-only by default. Ensure that we can modify it 
      document.IsReadOnly = false; 
      var page = document.Pages[1]; 
      var container = page.GetAnnotations(true); 
      var obj = new AnnEllipseObject(); 
      obj.Rect = LeadRectD.Create(0, 0, 1 * 720, 1 * 720); 
      obj.Stroke = AnnStroke.Create(AnnSolidColorBrush.Create("red"), LeadLengthD.Create(1)); 
      container.Children.Add(obj); 
      page.SetAnnotations(container); 
 
      var outName = Path.GetFileName(document.Uri.AbsolutePath).Replace(".", "_") + ".tif"; 
 
      var outFileName = Path.Combine(LEAD_VARS.ImagesDir, outName); 
      if (File.Exists(outFileName)) 
         File.Delete(outFileName); 
 
      var saveOptions = new SaveDocumentOptions(); 
      saveOptions.Format = RasterImageFormat.CcittGroup4; 
      saveOptions.BitsPerPixel = 1; 
      saveOptions.WebClient = null; 
      var annFileName = Path.ChangeExtension(outFileName, ".xml"); 
      if (File.Exists(annFileName)) File.Delete(annFileName); 
      saveOptions.AnnotationsUri = new Uri(annFileName); 
      document.SaveToFile(outFileName, saveOptions); 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.net.URL; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 
import java.util.regex.Pattern; 
 
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.annotations.engine.*; 
import leadtools.barcode.*; 
import leadtools.caching.*; 
import leadtools.codecs.*; 
import leadtools.document.*; 
import leadtools.document.DocumentMimeTypes.UserGetDocumentStatusHandler; 
import leadtools.document.converter.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
 
 
public void documentSaveToFileExample() throws URISyntaxException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "leadtools.pdf"); 
   LoadDocumentOptions options = new LoadDocumentOptions(); 
   LEADDocument document = DocumentFactory.loadFromFile(srcFileName, options); 
   // The document is read-only by default. Ensure that we can modify it 
   document.setReadOnly(false); 
   DocumentPage page = document.getPages().get(0); 
   AnnContainer container = page.getAnnotations(true); 
   AnnEllipseObject obj = new AnnEllipseObject(); 
   obj.setRect(LeadRectD.create(0, 0, 1 * 720, 1 * 720)); 
   obj.setStroke(AnnStroke.create(AnnSolidColorBrush.create("red"), LeadLengthD.create(1))); 
   container.getChildren().add(obj); 
   page.setAnnotations(container); 
 
   String outputFileName = srcFileName.replace(".", "_") + ".tif"; 
 
   File outputFile = new File(outputFileName); 
   if (outputFile.exists()) 
      outputFile.delete(); 
 
   SaveDocumentOptions saveOptions = new SaveDocumentOptions(); 
   saveOptions.setFormat(RasterImageFormat.CCITT_GROUP4); 
   saveOptions.setBitsPerPixel(1); 
   String annFileName = outputFileName.replace(".tif", ".xml"); 
   File annFile = new File(annFileName); 
   if (annFile.exists()) 
      annFile.delete(); 
   saveOptions.setAnnotationsUri(Paths.get(annFileName).toUri()); 
   document.saveToFile(outputFileName, saveOptions); 
   assertTrue(new File(outputFileName).exists()); 
} 
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 Assembly

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