I’m having an issue converting multipage TIF images to multipage PDFs and the file size of the converted file becoming way too big. We have 2 ways we convert images, and the old way converted images fine, but couldn’t retain if the individual page was portrait or landscape, which is needed. So we updated our logic (using an Aspose suggested logic/solution) to do the conversion page by page, which did retain the page by page orientation. However, the downside to this is the file sizes are far too big to be usable. The conversion logic between our old way and new way is below, under the test results.
Another issue we ran into with other files (not in this set) couldn’t convert the TIF, so we had to upgrade to a new Aspose.PDF version, which did allow conversion; the Aspose.PDF version is noted in the tests below.
Below is some information about the total differences when converting based on converting 3942 TIF files.
TIF Source Info
Total Source TIF Files: 3942
Total Source TIF File Size: 2.20 GB
Old Conversion (v10)
Converted Total File Size: 2.75 GB
Total Difference from Source: 0.55 GB
Average File Size Increase: 24.94%
Max File Size Increase: 398.54%
New Conversion (v10)
Converted Total File Size: 12.450 GB
Total Difference from Source: 10.24 GB
Average File Size Increase: 464.21%
Max File Size Increase: 3878.59%
New Conversion (v18)
Converted Total File Size: 12.449 GB
Total Difference from Source: 10.24 GB
Average File Size Increase: 464.20%
Max File Size Increase: 3878.53%
--------------- Code ---------------
Old Logic
using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
byte[] tmpBytes = new byte[fs.Length];
fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));
using (MemoryStream memoryStream = new MemoryStream(tmpBytes))
{
Bitmap bitmap = new Bitmap(memoryStream);
// Instantiate a Pdf object
Aspose.Pdf.Generator.Pdf pdfGenerator = new Aspose.Pdf.Generator.Pdf();
// Create a new section in the Pdf document
Aspose.Pdf.Generator.Section pdfSection = new Aspose.Pdf.Generator.Section(pdfGenerator);
// Set margins so image will fit, etc.
pdfSection.PageInfo.Margin.Top = 5;
pdfSection.PageInfo.Margin.Bottom = 5;
pdfSection.PageInfo.Margin.Left = 5;
pdfSection.PageInfo.Margin.Right = 5;
pdfSection.PageInfo.PageWidth = (bitmap.Width / bitmap.HorizontalResolution) * 72;
pdfSection.PageInfo.PageHeight = (bitmap.Height / bitmap.VerticalResolution) * 72;
// Add the section in the sections collection of the Pdf document
pdfGenerator.Sections.Add(pdfSection);
// Create an image object
Aspose.Pdf.Generator.Image pdfImage = new Aspose.Pdf.Generator.Image(pdfSection);
// Add the image into paragraphs collection of the section
pdfSection.Paragraphs.Add(pdfImage);
pdfImage.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Tiff;
// Set the ImageStream to a MemoryStream object
pdfImage.ImageInfo.ImageStream = memoryStream;
// Save the converted file
pdfGenerator.Save(outputfileName);
}
}
New Logic
using (var pdfDocument = new Document())
{
using (var tifFileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
var bitmap = new Bitmap(tifFileStream);
// Get the number of pages in the TIF
var frameDimension = new FrameDimension(bitmap.FrameDimensionsList[0]);
int frameCount = bitmap.GetFrameCount(frameDimension);
// Loop through each page and add it to the new PDF
for (int pageNum = 0; pageNum < frameCount; pageNum++)
{
// Add a new page to the PDF document
Page pdfPage = pdfDocument.Pages.Add();
// Get the current TIF page
bitmap.SelectActiveFrame(frameDimension, pageNum);
using (var currentStream = new MemoryStream())
{
// Store the current page for use
bitmap.Save(currentStream, ImageFormat.Tiff);
// Set portrait or landscape
pdfPage.PageInfo.IsLandscape = bitmap.Width > bitmap.Height;
// Set the margins
pdfPage.PageInfo.Margin = new MarginInfo(20, 20, 20, 20);
// Set the size
pdfPage.PageInfo.Width = (bitmap.Width / bitmap.HorizontalResolution) * 72;
pdfPage.PageInfo.Height = (bitmap.Height / bitmap.VerticalResolution) * 72;
// Add the image as a paragraph in the new PDF page
var pdfImage = new Aspose.Pdf.Image
{
ImageStream = currentStream
};
pdfPage.Paragraphs.Add(pdfImage);
// Save the new page to the final PDF
pdfDocument.Save(outputfileName);
}
}
}
}
Please Note: I’ve tried like 50 different ways/combinations to get the code to post and format properly, but the markup tool doesn’t want to work properly, so I gave up.