Tiff to PDF Transformation

Here is the code use for the transformation:

if (conversionSetting.Brightness <= 0)
    tiffSettings.Brightness = 0.33F;
else
    tiffSettings.Brightness = conversionSetting.Brightness;
//Load Byte Array to Stream<o:p></o:p>

using (Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(filePath))

{

    if (conversionSetting.IsDynamicPdf)

        pdfDoc.Form.Type = Aspose.Pdf.Forms.FormType.Standard;

    //create tiff device


    TiffDevice tiffPdfDevice = new TiffDevice(rsl, tiffSettings);

    //convert to TIFF image

    for (int i = 0; i < pdfDoc.Pages.Count; i++)

    {

        string fileDestinationPath = GetDestinationPath(filePath, destinationDirectory, i);

        try

        {

            using (FileStream outputStream = new FileStream(fileDestinationPath, FileMode.Create))

            {

                //Save as TIFF

                tiffPdfDevice.Process(pdfDoc, i + 1, i + 1, outputStream);

                images.Add(fileDestinationPath);

            }

        }

        catch (Exception)

        {

            pdfDoc.FreeMemory();


            foreach (var fileToDelete in images)

            {

                if (File.Exists(fileToDelete))

                    File.Delete(fileToDelete);

            }

            throw;

        }

    }


    pdfDoc.FreeMemory();

}

}

Hi Luis,

Thanks for contacting support and sharing code snippet.

I have tried to convert one of my sample PDF’s into TIFF using the code snippet which you have shared. Though there were some undefined objects (i.e conversionSettings, images) in the code but somehow I managed to make the code executable. The code generated the TIFF from PDF without creating any issue. I have modified the code to the following to make it executable.

TiffSettings tiffSettings = new TiffSettings();
tiffSettings.Brightness = 0.33f;
// Load Byte Array to Stream
using (Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(dataDir + "sample.pdf"))
{
    pdfDoc.Form.Type = Aspose.Pdf.Forms.FormType.Standard;
    // create tiff device
    TiffDevice tiffPdfDevice = new TiffDevice(new Resolution(300, 300), tiffSettings);
    // convert to TIFF image
    for (int i = 0; i < pdfDoc.Pages.Count; i++)
    {
        string fileDestinationPath = dataDir + "sampleTiff.tif";

        try
        {
            using (FileStream outputStream = new FileStream(fileDestinationPath, FileMode.Create))

            {

                // Save as TIFF

                tiffPdfDevice.Process(pdfDoc, i + 1, i + 1, outputStream);
            }
        }
        catch (Exception ex)
        {
            pdfDoc.FreeMemory();
        }
    }
    pdfDoc.FreeMemory();
}

Please note that sometimes issue may be related with specific input file(s). We will really appreciate if you please share more information about the issue you are facing along with the sample input document so that we can try to reproduce the issue in our environment and address it accordingly. We are sorry for this inconvenience.

Best Regards,

Thanks, the issue we are having is when converting from pdf to tiff the images sometimes don’t have the best quality and we are trying to find a threshold for the brightness setting: using these brightness settings:


TiffDevice tiffPdfDevice = new TiffDevice(rsl, tiffSettings);
//convert to TIFF image
for (int i = 0; i < pdfDoc.Pages.Count; i++)
{
switch (pdfDoc.Pages[i + 1].ColorType)
{
case ColorType.BlackAndWhite:
tiffSettings.Brightness = 0.8f;
break;
case ColorType.Grayscale:
tiffSettings.Brightness = 0.7f;
break;
case ColorType.Rgb:
tiffSettings.Brightness = 0.5f;
break;
case ColorType.Undefined:
tiffSettings.Brightness = 0.5f;
break;
}
}
but we have two issue right now for some pdfs when checking the ColorType it is throwing an exception that image type is not valid. And second we can’t find a good threshold for brightness for color images, some of them have brighter colors that are not readable using 0.5f and some of them have more colors and look to dark if we use 0.8f do you know if there is a better way to improve quality of conversion.

Thanks, the issue we are having is when converting from pdf to tiff the images sometimes don’t have the best quality and we are trying to find a threshold for the brightness setting: using these brightness settings:


TiffDevice tiffPdfDevice = new TiffDevice(rsl, tiffSettings);
//convert to TIFF image
for (int i = 0; i < pdfDoc.Pages.Count; i++)
{
switch (pdfDoc.Pages[i + 1].ColorType)
{
case ColorType.BlackAndWhite:
tiffSettings.Brightness = 0.8f;
break;
case ColorType.Grayscale:
tiffSettings.Brightness = 0.7f;
break;
case ColorType.Rgb:
tiffSettings.Brightness = 0.5f;
break;
case ColorType.Undefined:
tiffSettings.Brightness = 0.5f;
break;
}
}
but we have two issue right now for some pdfs when checking the ColorType it is throwing an exception that image type is not valid. And second we can’t find a good threshold for brightness for color images, some of them have brighter colors that are not readable using 0.5f do you know if there is a better way to improve quality of conversion.

Hi Luis,


Thanks for sharing more information.

usxer:
when checking the ColorType it is throwing an exception that image type is not valid

As shared earlier that I have tried to get ColorType from one of my sample PDF’s and I did not notice any issue. We will really appreciate if you please share a sample PDF document with which you are facing the issue so that we can try to replicate the scenario in our environment and share our findings with you.

usxer:
And second we can’t find a good threshold for brightness for color images, some of them have brighter colors that are not readable using 0.5f do you know if there is a better way to improve quality of conversion.

To increase the readability of the images you can Binarize them using BinarizeBradely() Method provide by Aspose.Pdf for .NET. In order to get threshold, you can specify the value of threshold by passing it to the Method. For more information you may check “Use Bradely Algorithm” article in the API documentation.

In case of any further assistance please feel free to contact us.


Best Regards,