Updated Content
This task has been made much easier with the addition of the TiffDocument class. This class lets you perform page level operations on TIFFs such as reordering, adding or removing pages.
[C#]
string targetFile = @"c:\pathTo\TargetTiff.tif";
string additionalSourceFile = @"c:\pathTo\another.tif";
using (FileStream inStream = new FileStream(targetFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
TiffDocument tDoc = new TiffDocument(inStream);
// removing the pages we want gone
// NOTE This is example code.. it is making assumptions that the
// frames you want to remove are present.. in your real code, you will
// want to be sure the frame exists etc...
// NOE: removing an item from the collection changes the collection so
// work BACKWARD
// IN this example we want to remove pages 5 and 6
// TiffDOucment.Pages is zero indexed so this is frames 4 and 5
tDoc.Pages.RemoveAt(5);
tDoc.Pages.RemoveAt(4);
// Now, lets say we want to add the the firt page of additionalSourceFile at the end of targetFile
// note that this stream must be kept open until save is complete...
// because tDoc is referencing the page from tDoc2 while it's open and until that save
using (FileStream inStream2 = new FileStream(additionalSourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
TiffDocument tDoc2 = new TiffDocument(inStream2);
// we're just adding the firt page of tDoc2 to the end of this
// we could insert at any location etc..
tDoc.Pages.Add(tDoc2.Pages[0]);
// once we're done adding all the things, we save so we can close out
tdoc.Save(@"c:\pathTo\some_new_Combined_tiff.tif");
}
}
Original Content - OUTDATED
There may be times when a user wants a page to be inserted into an existing TIFF file. To do this, the file must be recreated. Below is example code for performing this action.
[C#]
private void InsertTiffPage(int index, AtalaImage image, string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// Validate the file.
TiffDecoder decoder = new TiffDecoder();
if (!decoder.IsValidFormat(fs))
throw new ArgumentException("The filename provided is not a valid TIFF file.");
fs.Seek(0, SeekOrigin.Begin);
// Validate and correct the index.
int count = decoder.GetFrameCount(fs);
if (index > count) index = count;
if (index < 0) index = 0;
// Save the new image to a temp file.
string newFile = System.IO.Path.GetTempFileName();
FileStream newFs = new FileStream(newFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
// Loop through the pages and insert the new page.
count++;
int readIndex = 0;
TiffEncoder encoder = new TiffEncoder();
for (int i = 0; i < count; i++)
{
AtalaImage img = null;
if (i == index)
img = image;
else
{
fs.Seek(0, SeekOrigin.Begin);
img = decoder.Read(fs, readIndex, null);
readIndex++;
}
// Set the compression.
if (img.PixelFormat == PixelFormat.Pixel1bppIndexed)
encoder.Compression = TiffCompression.Group4FaxEncoding;
else
encoder.Compression = TiffCompression.Lzw;
newFs.Seek(0, SeekOrigin.Begin);
encoder.Save(newFs, img, null);
// Done with the image.
if (img != image) img.Dispose();
// All pages except the first must be appended.
encoder.Append = true;
}
fs.Close();
newFs.Close();
// Copy the temporary image to replace the old one.
File.Copy(newFile, fileName, true);
// Delete the temp file.
File.Delete(newFile);
}
[Visual Basic]
Private Sub InsertTiffPage(ByVal index As Integer, ByVal image As AtalaImage, ByVal fileName As String)
Dim fs As FileStream = New FileStream(fileName,FileMode.Open,FileAccess.Read,FileShare.Read)
' Validate the file.
Dim decoder As TiffDecoder = New TiffDecoder()
If Not decoder.IsValidFormat(fs) Then
Throw New ArgumentException("The filename provided is not a valid TIFF file.")
End If
fs.Seek(0, SeekOrigin.Begin)
' Validate and correct the index.
Dim count As Integer = decoder.GetFrameCount(fs)
If index > count Then index = count
If index < 0 Then index = 0
' Save the new image to a temp file.
Dim NewFile As String = System.IO.Path.GetTempFileName()
Dim NewFs As FileStream = New FileStream(NewFile,FileMode.Open,FileAccess.ReadWrite,FileShare.None)
' Loop through the pages and insert the new page.
count = count + 1
Dim readIndex As Integer = 0
Dim encoder As TiffEncoder = New TiffEncoder()
Dim i As Integer
For i = 0 To count- 1 Step i + 1
Dim img As AtalaImage = Nothing
If i = index Then
img = image
Else
fs.Seek(0, SeekOrigin.Begin)
img = decoder.Read(fs, readIndex, Nothing)
readIndex = readIndex + 1
End If
' Set the compression.
If img.PixelFormat = PixelFormat.Pixel1bppIndexed Then
encoder.Compression = TiffCompression.Group4FaxEncoding
Else
encoder.Compression = TiffCompression.Lzw
End If
NewFs.Seek(0, SeekOrigin.Begin)
encoder.Save(NewFs, img, Nothing)
' Done with the image.
If img <> image Then
img.Dispose()
End If
' All pages except the first must be appended.
encoder.Append = True
Next
fs.Close()
NewFs.Close()
' Copy the temporary image to replace the old one.
File.Copy(NewFile, fileName, True)
' Delete the temp file.
File.Delete(NewFile)
End Sub
Original Article:
Q10093 - HOWTO: Insert a page into an existing TIFF