The Tagged Image File Format (TIFF) format is used by many scanning applications, publishing and layout applications and scanners and fax machines. Many scanners create multipage documents automatically when scanning documents.

TX Text Control directly supports the insertion of TIFF images using the ImageCollection class. This sample shows how to split multipage TIFF images first in order to insert them into new pages. Each image is inserted on a new section page that matches the size of the image.

The following method splits a multipage TIFF into single TIFF images that are returned as a list of images:

/***************************************************************
* SplitMultipageTIFF method
* description: Splits a multipage TIFF into separate images
*
* param: image - the image path
* return val: A list of images
***************************************************************/
private List<Image> SplitMultipageTIFF(string image)
{
List<Image> listImages = new List<Image>();
Bitmap bmp = (Bitmap)Image.FromFile(image);
int iCount = bmp.GetFrameCount(FrameDimension.Page);
for (int i = 0; i < iCount; i++)
{
// store each frame
bmp.SelectActiveFrame(FrameDimension.Page, i);
MemoryStream byteStream = new MemoryStream();
bmp.Save(byteStream, ImageFormat.Tiff);
// assemble new image
listImages.Add(Image.FromStream(byteStream));
}
return listImages;
}

The next method iterates through the list of images and creates a new section page for each image. The page size will be adapted to the image size and the margins are being set to 0,0, so that the images fit perfectly into each page.

/***************************************************************
* InsertTIFF method
* description: Insert a new section for each page and inserts
* the images
*
* param: image - the image path
* startAtNewPage - specifies whether the first
* image is inserted on a new page or not
***************************************************************/
private void InsertTIFF(string image, bool startAtNewPage)
{
// make sure the page unit is twips
textControl1.PageUnit = TXTextControl.MeasuringUnit.Twips;
bool bFirstPage = false;
// retrieve the images
List<Image> images = SplitMultipageTIFF(image);
foreach (Image img in images)
{
// create a new TXTextControl.Image from each image
TXTextControl.Image TXImg = new TXTextControl.Image(img);
if (bFirstPage == false && startAtNewPage == true)
{
// insert a new section
textControl1.Sections.Add(
TXTextControl.SectionBreakKind.BeginAtNewPage);
bFirstPage = true;
}
// add the image and adjust the page size and margins according
// to the image
textControl1.Images.Add(TXImg,
textControl1.InputPosition.Page,
new Point(0, 0),
TXTextControl.ImageInsertionMode.FixedOnPage);
textControl1.Sections.GetItem().Format.PageMargins =
new TXTextControl.PageMargins(0, 0, 0, 0);
textControl1.Sections.GetItem().Format.PageSize =
new TXTextControl.PageSize(
TXImg.Size.Width, TXImg.Size.Height);
}
}
view raw InsertTIFF.cs hosted with ❤ by GitHub

This method makes it very easy to combine different documents into one single document including PDF files, multipage TIFF files and other supported documents.

Download the sample from GitHub and test it on your own.