Background
Aspose .Pdf allows you to add TIFF images to a PDF file. However, sometimes it takes too much time when we try to save the created PDF document. However, ImageInfo class provides a property named IsBlackWhite which can be set to true to force a TIFF image to be black and white. This will improve the performance between 70 to 80 %. Following are the code snippets which will help you understand how we can use this property while adding Tiff images to PDF.
C#
string baseDir = @"C:\TiffImages"; string outDir = @"C:\OutDir\"; //get a list of tiff image files string[] files = System.IO.Directory.GetFiles(baseDir); //Instantiate a Pdf object Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf(); //navigate through the files and them in the pdf file foreach (string myFile in files) { //Load all tiff files in byte array FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read); byte[] tmpBytes = new byte[fs.Length]; fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length)); MemoryStream mystream = new MemoryStream(tmpBytes); Bitmap b = new Bitmap(mystream); //Create a new section in the Pdf document Aspose.Pdf.Generator.Section sec1 = new Aspose.Pdf.Generator.Section(pdf1); // Set margins so image will fit, etc. sec1.PageInfo.Margin.Top = 5; sec1.PageInfo.Margin.Bottom = 5; sec1.PageInfo.Margin.Left = 5; sec1.PageInfo.Margin.Right = 5; sec1.PageInfo.PageWidth = (b.Width / b.HorizontalResolution) * 72; sec1.PageInfo.PageHeight = (b.Height / b.VerticalResolution) * 72; //Add the section in the sections collection of the Pdf document pdf1.Sections.Add(sec1); //Create an image object Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1); //Add the image into paragraphs collection of the section sec1.Paragraphs.Add(image1); image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Tiff; // set IsBlackWhite property to true for performance improvement image1.ImageInfo.IsBlackWhite = true; //Set the ImageStream to a MemoryStream object image1.ImageInfo.ImageStream = mystream; //Set desired image scale image1.ImageScale = 0.95F; } //Save the Pdf pdf1.Save(outDir + "OutputFile.pdf");
VB.NET
Dim baseDir As String = "C:\TiffImages" Dim outDir As String = "C:\OutDir\" 'get a list of tiff image files Dim files As String() = System.IO.Directory.GetFiles(baseDir) 'Instantiate a Pdf object Dim pdf1 As New Aspose.Pdf.Generator.Pdf() 'navigate through the files and them in the pdf file Dim myFile As String For Each myFile In files 'Load all tiff files in byte array Dim fs As New FileStream(myFile, FileMode.Open, FileAccess.Read) Dim tmpBytes As Byte() = New Byte(fs.Length - 1) {} fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length)) Dim mystream As New MemoryStream(tmpBytes) Dim b As New Bitmap(mystream) 'Create a new section in the Pdf document Dim sec1 As New Aspose.Pdf.Generator.Section(pdf1) 'Set margins so image will fit, etc. sec1.PageInfo.Margin.Top = 5 sec1.PageInfo.Margin.Bottom = 5 sec1.PageInfo.Margin.Left = 5 sec1.PageInfo.Margin.Right = 5 sec1.PageInfo.PageWidth = (b.Width / b.HorizontalResolution) * 72 sec1.PageInfo.PageHeight = (b.Height / b.VerticalResolution) * 72 'Add the section in the sections collection of the Pdf document pdf1.Sections.Add(sec1) 'Create an image object Dim image1 As New Aspose.Pdf.Generator.Image(sec1) 'Add the image into paragraphs collection of the section sec1.Paragraphs.Add(image1) image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Tiff 'set IsBlackWhite property to true for performance improvement image1.ImageInfo.IsBlackWhite = True 'Set the ImageStream to a MemoryStream object image1.ImageInfo.ImageStream = mystream 'Set desired image scale image1.ImageScale = 0.95F Next 'Save the Pdf pdf1.Save(outDir + "OutputFile.pdf")
