Spire.PDF for .NET is a PDF component which contains an incredible wealth of features to create, read, edit and manipulate PDF documents on .NET, Silverlight and WPF Platform. As a professional .NET PDF component, it also includes many useful features, for example, functionalities of adding header and footer, drawing table, saving PDF document as tiff and Splitting tiff image and drawing to pdf document without installing Adobe Acrobat or any other third party libraries.
This article would introduce a detail method to split the tiff image and draw to pdf document. The below picture show the effect of splitting tiff image and drawing to pdf document:
The main steps of the method are:
One: Split multi-page TIFF file to multiple frames
- Load a multipage Tiff.
- Use Image.GetFrameCount method to get the number of frames of tiff Image.
- Initialize a new instance of Guid to save the dimension from tiff image.
- Using Guid to initialize a new instance of the System.Drawing.Imaging.FrameDimension class.
- Iterate over the Tiff Frame Collection and save them to Image array.
Two: Draw image to PDF
- Initialize a new instance of Spire.Pdf.PdfDocument.
- Iterate over the image array.
- Use Spire.Pdf.PdfPageBase.Canvas.DrawImage method to draw image into page with the specified coordinates and image size.
Download and install Spire.Pdf for .NET and use below code to experience this method to split tiff and convert it to pdf document.
The full code of method:
- C#
- VB.NET
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using Spire.Pdf; using Spire.Pdf.Graphics; namespace SplitTiff { class Program { static void Main(string[] args) { using (PdfDocument pdfDocument = new PdfDocument()) { Image tiffImage = Image.FromFile(@"..\..\demo.tiff"); Image[] images = SplitTIFFImage(tiffImage); for (int i = 0; i < images.Length; i++) { PdfImage pdfImg = PdfImage.FromImage(images[i]); PdfPageBase page = pdfDocument.Pages.Add(); float width = pdfImg.Width * 0.5f; float height = pdfImg.Height * 0.5f; float x = (page.Canvas.ClientSize.Width - width) / 2; //set the image of the page page.Canvas.DrawImage(pdfImg, x, 0, width, height); } pdfDocument.SaveToFile(@"..\..\result.pdf"); System.Diagnostics.Process.Start(@"..\..\result.pdf"); } } public static Image[] SplitTIFFImage(Image tiffImage) { int frameCount = tiffImage.GetFrameCount(FrameDimension.Page); Image[] images = new Image[frameCount]; Guid objGuid = tiffImage.FrameDimensionsList[0]; FrameDimension objDimension = new FrameDimension(objGuid); for (int i = 0; i < frameCount; i++) { tiffImage.SelectActiveFrame(objDimension, i); using (MemoryStream ms = new MemoryStream()) { tiffImage.Save(ms, ImageFormat.Tiff); images[i] = Image.FromStream(ms); } } return images; } } }
If you are interested in the method of converting pdf to tiff image you can refer the Save PDF Document as tiff image article in our website.