I am looking to convert TIFF files into PDF files. A single TIFF file can have multiple pages and I would want the resulting PDF to be a single file with multiple pages just like the TIFF. I have looked on the forum and found some articles but nothing that is exactly what I want.
I tested the conversion with this code and the resulting PDF was orders of magnitude larger in file size than the original TIFF. For example a 20MB TIFF came out to be a 700MB PDF!!
// The path to the documents directory.
string dataDir = @"C:\Users\10055\Desktop\ConsoleApp1";
// Get a list of tiff image files
string[] files = System.IO.Directory.GetFiles(dataDir, "*.tif");
Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
pdfLicense.SetLicense(@"C:\Users\10055\Desktop\ConsoleApp1\Aspose.Pdf.lic");
// Instantiate a Document object
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
// 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 Page in the Pdf document
Aspose.Pdf.Page currpage = doc.Pages.Add();
// Set margins so image will fit, etc.
currpage.PageInfo.Margin.Top = 5;
currpage.PageInfo.Margin.Bottom = 5;
currpage.PageInfo.Margin.Left = 5;
currpage.PageInfo.Margin.Right = 5;
currpage.PageInfo.Width = (b.Width / b.HorizontalResolution) * 72;
currpage.PageInfo.Height = (b.Height / b.VerticalResolution) * 72;
// Create an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
// Add the image into paragraphs collection of the page
currpage.Paragraphs.Add(image1);
// Set IsBlackWhite property to true for performance improvement
//image1.IsBlackWhite = true;
// Set the ImageStream to a MemoryStream object
image1.ImageStream = mystream;
// Set desired image scale
image1.ImageScale = 0.95F;
// Save the Pdf
doc.Save(dataDir + $"\\{System.IO.Path.GetFileNameWithoutExtension(myFile)}_PerformaceImprovement_out.pdf");
}
I have both the Aspose PDF and Aspose Words products and open to a solution in either.