Products Technologies Demo Docs Blog Support Company

Inserting Multipage TIFF Images into TX Text Control

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…

Inserting Multipage TIFF Images into TX Text Control

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);
    }
}

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.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2012 or better
  • TX Text Control .NET for Windows Forms (trial sufficient)

Windows Forms

Text Control combines the power of a reporting tool and an easy-to-use WYSIWYG word processor - fully programmable and embeddable in your Windows Forms application. TX Text Control .NET for Windows Forms is a royalty-free, fully programmable rich edit control that offers developers a broad range of word processing features in a reusable component for Visual Studio.

See Windows Forms products

Related Posts

ASP.NETWindows FormsWPF

Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub

This article gives a quick overview of the new repositories, their structure and our plans for the future.


Windows FormsGitHub

Applying Paragraph Styles from a Master Template

Styles are typically used to change the formatting consistently across a complete document. Specifically when merging templates using the MailMerge class, it makes sense to keep all styles in a…


ReportingWindows FormsGitHub

Windows Forms: Printing Multiple Pages Per Sheet

This sample project implements the class MultipagePrintDocument that inherits from System.Drawing.Printing.PrintDocument to print multiple pages of a document per sheet. The constructor of…


ReportingWindows FormsGitHub

Inserting Watermark Images to All Pages Dynamically

This sample project shows how to create and insert a watermark image on all pages dynamically. Image objects have the Name property to store additional string information with an image. This…


ReportingWindows FormsGitHub

Reporting: Merging MS Word Documents with DocVariables

TX Text Control supports all MS Word fields including the typical merge fields, content control fields, legacy form fields or special fields such as DOCVARIABLE. All of those fields can be found…