←Select platform

CompactFile(string,string,int) Method

Summary
Compacts TIFF/BigTIFF files with default options.
Syntax
C#
VB
Objective-C
C++
public void CompactFile( 
   string srcFileName, 
   string destFileName, 
   int pages 
) 
Public Overloads Sub CompactFile( _ 
   ByVal srcFileName As String, _ 
   ByVal destFileName As String, _ 
   ByVal pages As Integer _ 
)  
- (BOOL)compactFile:(NSString *)sourceFile  
    destinationFile:(NSString *)destinationFile  
              pages:(NSUInteger)pages  
              error:(NSError **)error 
public: 
void CompactFile(  
   String^ srcFileName, 
   String^ destFileName, 
   int pages 
)  

Parameters

srcFileName
A String containing the name of the file being compacted. All the pages will be read from this file.

destFileName
A String containing the name of the file in which all the pages will be written.

pages
Value that represents the number of pages to copy. Use 0 to copy all the pages. If  pages is > 0, only  pages will be copied to  destFileName.

Remarks

This method can also be used to copy or extract one or more pages from a TIFF/BigTIFF file and copy them without recompression to another TIFF/BigTIFF file.

This method can also be used to convert a TIFF file to BigTIFF or vice versa. The destination file will be Tiff or BigTiff depending on BigTiff.

Example
C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
 
 
public void CompactFileExample() 
{ 
	RasterCodecs codecs = new RasterCodecs(); 
 
	string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "CompactFile1_Src.tif"); 
	string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "CompactFile1_Dest.tif"); 
 
	// Create a RasterImage with 4 pages containing text showing the page number 
	RasterImage image = null; 
	const int pageCount = 4; 
 
	using (System.Drawing.Font f = new System.Drawing.Font("Arial", 36, System.Drawing.FontStyle.Bold)) 
	using (System.Drawing.Bitmap btmp = new System.Drawing.Bitmap(320, 200)) 
	using (System.Drawing.StringFormat sf = new System.Drawing.StringFormat()) 
	{ 
		System.Drawing.Rectangle rc = new System.Drawing.Rectangle(0, 0, btmp.Width, btmp.Height); 
		sf.Alignment = System.Drawing.StringAlignment.Center; 
		sf.LineAlignment = System.Drawing.StringAlignment.Center; 
 
		for (int i = 1; i <= pageCount; i++) 
		{ 
			// Create a GDI+ bitmap with the text 
			string text = "Page " + i; 
 
			using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(btmp)) 
			{ 
				g.FillRectangle(System.Drawing.Brushes.White, rc); 
				g.DrawString(text, f, System.Drawing.Brushes.Black, rc, sf); 
			} 
 
			RasterImage tempImage = Leadtools.Drawing.RasterImageConverter.ConvertFromImage(btmp, Leadtools.Drawing.ConvertFromImageOptions.None); 
 
			if (image == null) 
				image = tempImage; 
			else 
				image.AddPage(tempImage); 
		} 
	} 
 
	// Save all the pages to the file 
	// The file should have 4 pages now: 1, 2, 3, 4 
	codecs.Save(image, srcFileName, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite); 
	image.Dispose(); 
 
	// All the pages in the source TIFF file. This will create the destination file 
	codecs.CompactFile(srcFileName, destFileName, 0); 
 
	// Compact the source file again and append all the pages to the existing destination file 
	int pagesToAdd = 0;  // 0 means all pages 
	int srcStartPage = 1; 
	int destStartPage = 5;  // insert at the end 
 
	codecs.CompactFile( 
	   srcFileName, 
	   destFileName, 
	   pagesToAdd, 
	   srcStartPage, 
	   false, 
	   0, 
	   destStartPage, 
	   false, 
	   0, 
	   CodecsSavePageMode.Insert, 
	   false, 
	   false); 
 
	// Clean up 
	codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
Imports Leadtools.Drawing 
Imports Leadtools.Svg 
 
Public Sub CompactFileExample() 
   Dim codecs As RasterCodecs = New RasterCodecs() 
 
   Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "CompactFile1_Src.tif") 
   Dim destFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "CompactFile1_Dest.tif") 
 
   ' Create a RasterImage with 4 pages containing text showing the page number 
   Dim image As RasterImage = Nothing 
   Const pageCount As Integer = 4 
 
   Using f As New System.Drawing.Font("Arial", 36, System.Drawing.FontStyle.Bold) 
      Using btmp As New System.Drawing.Bitmap(320, 200) 
         Using sf As New System.Drawing.StringFormat() 
            Dim rc As New System.Drawing.Rectangle(0, 0, btmp.Width, btmp.Height) 
            sf.Alignment = System.Drawing.StringAlignment.Center 
            sf.LineAlignment = System.Drawing.StringAlignment.Center 
 
            For i As Integer = 1 To pageCount 
               ' Create a GDI+ bitmap with the text 
               Dim text As String = "Page " + i.ToString() 
 
               Using g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(btmp) 
                  g.FillRectangle(System.Drawing.Brushes.White, rc) 
                  g.DrawString(text, f, System.Drawing.Brushes.Black, rc, sf) 
               End Using 
 
               Dim tempImage As RasterImage = RasterImageConverter.ConvertFromImage(btmp, ConvertFromImageOptions.None) 
 
               If image Is Nothing Then 
                  image = tempImage 
               Else 
                  image.AddPage(tempImage) 
               End If 
            Next 
         End Using 
      End Using 
   End Using 
 
   ' Save all the pages to the file 
   ' The file should have 4 pages now: 1, 2, 3, 4 
   codecs.Save(image, srcFileName, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite) 
   image.Dispose() 
 
   ' All the pages in the source TIFF file. This will create the destination file 
   codecs.CompactFile(srcFileName, destFileName, 0) 
 
   ' Compact the source file again and append all the pages to the existing destination file 
   Dim pagesToAdd As Integer = 0 ' 0 means all pages 
   Dim srcStartPage As Integer = 1 
   Dim destStartPage As Integer = 5 ' insert at the end 
 
   codecs.CompactFile(srcFileName, destFileName, pagesToAdd, srcStartPage, False, 0, destStartPage, False, 0, CodecsSavePageMode.Insert, False, False) 
 
   ' Clean up 
   codecs.Dispose() 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\LEADTOOLS22\Resources\Images" 
End Class 
Requirements

Target Platforms

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

Leadtools.Codecs Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2022 LEAD Technologies, Inc. All Rights Reserved.