Tiff to pdf conversion using aspose

While converting a tiff file into pdf the file size is getting decreased or increased much in c#.
The below is the code change we did, please help me to maintain the same file while converting from tiff to pdf.

else if (fileExtn.Contains(“tif”))
{
// Convert TIFF to PDF
PDF.Document pdfDoc = new PDF.Document();
Bitmap bitmap = new Bitmap(new MemoryStream(documentData));
FrameDimension dimension = new FrameDimension(bitmap.FrameDimensionsList[0]);
int frameCount = bitmap.GetFrameCount(dimension);
for (int frameIdx = 0; frameIdx < frameCount; frameIdx++)
{
PDF.Page page = pdfDoc.Pages.Add();
bitmap.SelectActiveFrame(dimension, frameIdx);
var currentImage = new MemoryStream();
bitmap.Save(currentImage, ImageFormat.Tiff); // Save as TIFF to preserve quality
page.AddImage(currentImage, page.GetPageRect(false));
}
PDF.Optimization.OptimizationOptions optimizeOptions = new PDF.Optimization.OptimizationOptions();
// Set CompressImages option
optimizeOptions.ImageCompressionOptions.CompressImages = false;
optimizeOptions.LinkDuplcateStreams = false;
optimizeOptions.RemoveUnusedObjects = true;
optimizeOptions.RemoveUnusedStreams = true;
// Set ImageQuality option
optimizeOptions.ImageCompressionOptions.ImageQuality = 100;
// Optimize PDF document using OptimizationOptions
pdfDoc.OptimizeResources(optimizeOptions);
pageCount = pdfDoc.Pages.Count;
_logger.LogTrace($“pageCount {pageCount}”);
return pdfDoc;
}

@KaviVarsha

To maintain the same file size when converting a TIFF file to PDF using Aspose.PDF in C#, you can follow these guidelines:

  1. Image Compression: Ensure that you are not compressing the images during the conversion process. In your code, you have already set optimizeOptions.ImageCompressionOptions.CompressImages to false, which is correct. This prevents the images from being compressed, helping to maintain the original quality and size.

  2. Image Quality: You have set the ImageQuality option to 100, which is also appropriate as it ensures that the images retain their original quality during the conversion.

  3. MemoryStream Usage: When saving the bitmap to a MemoryStream, ensure that you are using the correct format. You are currently saving it as TIFF, which is fine, but you might want to consider saving it as a PDF directly if the library supports it, to avoid any unnecessary conversions.

  4. Optimization Options: You are already using optimization options to remove unused objects and streams, which is good practice. However, if the file size is still not as expected, you might want to experiment with these settings to see if they affect the output size.

Here’s a refined version of your code snippet that incorporates these suggestions:

else if (fileExtn.Contains("tif"))
{
    // Convert TIFF to PDF
    PDF.Document pdfDoc = new PDF.Document();
    Bitmap bitmap = new Bitmap(new MemoryStream(documentData));
    FrameDimension dimension = new FrameDimension(bitmap.FrameDimensionsList[0]);
    int frameCount = bitmap.GetFrameCount(dimension);
    
    for (int frameIdx = 0; frameIdx < frameCount; frameIdx++)
    {
        PDF.Page page = pdfDoc.Pages.Add();
        bitmap.SelectActiveFrame(dimension, frameIdx);
        using (var currentImage = new MemoryStream())
        {
            bitmap.Save(currentImage, ImageFormat.Tiff); // Save as TIFF to preserve quality
            currentImage.Position = 0; // Reset stream position
            page.AddImage(currentImage, page.GetPageRect(false));
        }
    }

    PDF.Optimization.OptimizationOptions optimizeOptions = new PDF.Optimization.OptimizationOptions
    {
        ImageCompressionOptions = { CompressImages = false, ImageQuality = 100 },
        LinkDuplcateStreams = false,
        RemoveUnusedObjects = true,
        RemoveUnusedStreams = true
    };

    // Optimize PDF document using OptimizationOptions
    pdfDoc.OptimizeResources(optimizeOptions);
    int pageCount = pdfDoc.Pages.Count;
    _logger.LogTrace($"pageCount  {pageCount}");
    return pdfDoc;
}

By following these steps and ensuring that your settings are correctly configured, you should be able to maintain the file size more effectively during the TIFF to PDF conversion process. If you continue to experience issues, consider checking the original TIFF file’s properties and dimensions, as these can also impact the final PDF size.

After this changes also Still the file size is getting increased or decreased

@KaviVarsha

Would you kindly explain why do you want to keep the file sizes same for both input and output formats? Because both formats (TIFF and PDF) are quite different than each other and have different structures.