←Select platform

FormatSupportsMultipageSave Method

Summary
Gets a value that indicates whether the specified format supports multipage save operation.
Syntax
C#
C++/CLI
Python
public static bool FormatSupportsMultipageSave( 
   RasterImageFormat format 
) 
public: 
static bool FormatSupportsMultipageSave(  
   RasterImageFormat format 
)  
def FormatSupportsMultipageSave(self,format): 

Parameters

format
Format to check.

Return Value

true if the image file format specified by  format supports multipage save operations.

Example

This example will try to save a multipage file to disk, if the format supports multipage files, then it will create a single output file, otherwise, multiple files (for each page) will be created.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
 
 
private static void SaveMultiPageFile(RasterCodecs rasterCodecs, RasterImage multiPageImage, string outputFile, RasterImageFormat format) 
{ 
   // Check if the image has multi-ple pages and the format supports multi-page 
   if (multiPageImage.PageCount > 1 && RasterCodecs.FormatSupportsMultipageSave(format)) 
   { 
      // Yes, just save the file 
      rasterCodecs.Save(multiPageImage, outputFile, format, 0, 1, -1, 1, CodecsSavePageMode.Overwrite); 
   } 
   else 
   { 
      // No, we need to save each page in a separate file 
      int originalPageNumber = multiPageImage.Page; 
 
      for (int pageNumber = 1; pageNumber <= multiPageImage.PageCount; pageNumber++) 
      { 
         // Get the new file name 
         string name = Path.GetFileNameWithoutExtension(outputFile) + "_page" + pageNumber.ToString(); 
         name = Path.ChangeExtension(name, Path.GetExtension(outputFile)); 
         string pageFile = Path.Combine(Path.GetDirectoryName(outputFile), name); 
 
         // Save this page 
         multiPageImage.Page = pageNumber; 
         rasterCodecs.Save(multiPageImage, pageFile, format, 0); 
      } 
 
      multiPageImage.Page = originalPageNumber; 
   } 
} 
 
public void FormatSupportsMultipageSaveExample() 
{ 
   string inDir = LEAD_VARS.ImagesDir; 
   string outDir = Path.Combine(inDir, @"FormatSupportsMultipageSave"); 
 
   if (!Directory.Exists(outDir)) 
      Directory.CreateDirectory(outDir); 
 
   using (RasterCodecs rasterCodecs = new RasterCodecs()) 
   { 
      // Create a multi-page image 
      using (RasterImage multiPageImage = GetMultipageImage(rasterCodecs, inDir)) 
      { 
         // Save the image as TIF, this should create a single file 
         SaveMultiPageFile(rasterCodecs, multiPageImage, Path.Combine(outDir, "out.tif"), RasterImageFormat.Tif); 
 
         // Save the image as PNG, this should create multiple files (one for each page) 
         SaveMultiPageFile(rasterCodecs, multiPageImage, Path.Combine(outDir, "out.png"), RasterImageFormat.Png); 
      } 
   } 
} 
 
private static RasterImage GetMultipageImage(RasterCodecs rasterCodecs, string inDir) 
{ 
   // Create a multi-page image from some known LEADTOOLS images 
   RasterImage multiPageImage = null; 
 
   for (int imageNumber = 1; imageNumber <= 4; imageNumber++) 
   { 
      string fileName = Path.Combine(inDir, "Ocr" + imageNumber.ToString() + ".tif"); 
 
      RasterImage pageImage = rasterCodecs.Load(fileName, 1); 
      if (multiPageImage == null) 
      { 
         multiPageImage = pageImage; 
      } 
      else 
      { 
         multiPageImage.AddPage(pageImage); 
         pageImage.Dispose(); 
      } 
   } 
 
   return multiPageImage; 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.*; 
import java.net.*; 
import java.nio.file.Paths; 
import java.util.*; 
import java.time.Instant; 
import java.time.Duration; 
 
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.codecs.RasterCodecs.FeedCallbackThunk; 
import leadtools.drawing.internal.*; 
import leadtools.imageprocessing.*; 
import leadtools.imageprocessing.color.ChangeIntensityCommand; 
import leadtools.svg.*; 
 
 
private void saveMultiPageFile(RasterCodecs rasterCodecs, RasterImage multiPageImage, String outputFile, 
      RasterImageFormat format) throws IOException { 
   // Check that the output file exists 
   File outDirFile = new File(outputFile); 
   if (!outDirFile.exists()) 
      outDirFile.createNewFile(); 
 
   // Check if the image has multi-ple pages and the format supports multi-page 
   if (multiPageImage.getPageCount() > 1 && RasterCodecs.formatSupportsMultipageSave(format)) { 
      // Yes, just save the file 
      rasterCodecs.save(multiPageImage, outputFile, format, 0, 1, -1, 1, CodecsSavePageMode.OVERWRITE); 
   } else { 
      // No, we need to save each page in a separate file 
      int originalPageNumber = multiPageImage.getPage(); 
 
      for (int pageNumber = 1; pageNumber <= multiPageImage.getPageCount(); pageNumber++) { 
         // Get the new file name 
         String fileWithoutExtension = outputFile.substring(0, outputFile.indexOf(".")); 
         String name = fileWithoutExtension + "_page" + pageNumber; 
         String outputFileExtension = outputFile.substring(outputFile.indexOf(".")); 
         name = name + outputFileExtension; 
 
         // Save this page 
         multiPageImage.setPage(pageNumber); 
         rasterCodecs.save(multiPageImage, name, format, 0); 
      } 
      multiPageImage.setPage(originalPageNumber); 
   } 
} 
 
public void formatSupportsMultipageSaveExample() throws IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   RasterCodecs rasterCodecs = new RasterCodecs(); 
 
   // Create a multi-page image 
   RasterImage multiPageImage = getMultipageImage(rasterCodecs, LEAD_VARS_IMAGES_DIR); 
 
   // Save the image as TIF, this should create a single file 
   saveMultiPageFile( 
         rasterCodecs, 
         multiPageImage, 
         combine(LEAD_VARS_IMAGES_DIR, "out.tif"), 
         RasterImageFormat.TIF); 
   assertTrue( 
         "file unsuccessfully saved", 
         (new File(combine(LEAD_VARS_IMAGES_DIR, "out.tif"))).exists()); 
 
   // Save the image as PNG, this should create multiple files (one for each page) 
   saveMultiPageFile( 
         rasterCodecs, 
         multiPageImage, 
         combine(LEAD_VARS_IMAGES_DIR, "out.png"), 
         RasterImageFormat.PNG); 
   assertTrue( 
         "file unsuccessfully saved", 
         (new File(combine(LEAD_VARS_IMAGES_DIR, "out.png"))).exists()); 
} 
 
private RasterImage getMultipageImage(RasterCodecs rasterCodecs, String inDir) { 
 
   // Create a multi-page image from some known LEADTOOLS images 
   RasterImage multiPageImage = null; 
 
   for (int imageNumber = 1; imageNumber <= 4; imageNumber++) { 
      String fileName = combine(inDir, "Ocr" + imageNumber + ".tif"); 
 
      RasterImage pageImage = rasterCodecs.load(fileName, 1); 
      if (multiPageImage == null) { 
         multiPageImage = pageImage; 
      } else { 
         multiPageImage.addPage(pageImage); 
         pageImage.dispose(); 
      } 
   } 
 
   return multiPageImage; 
 
} 
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.Codecs Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.