I tried the FreeSpire.XLS component for converting an excel file (xlsx format) into tiff file. This works pretty easy with FreeSpire.XLS component. But i noticed differences on each system, I tried to convert the sample file into the output format tiff.
On dev environment (see attachment output_dev.tif), there is a lots of space, but I´m using worksheet.LastRow / worksheet.LastColumn properties to identify the end of sheet. Also when I´m entering manual the last row / last column, the space is generated in the output file.
On test environment, the problem with the space does not occure, but as you can see on files output_test_new.tif and output_test_old.tif there are differences in the output file and also differences at the resolution:
output_prod_new.tif has 4404x3402 pixels while
output_prod_old.tif has 5871 x 4542 pixels
Is there any way to define one standard, the files has to be generated? (e.g. setting a fixed font or system properties)
Code for converting the files:
- Code: Select all
public class ExcelToTiffTransformator
{
#region Fields & Properties
/// <summary>
/// Mime type for find of image codec.
/// </summary>
private const string MIME_TYPE = "image/tiff";
/// <summary>
/// Output resolution for Tiff file.
/// </summary>
private const int tiffOutputDPI = 400;
/// <summary>
/// Tiff compression type.
/// </summary>
private EncoderValue TiffCompression
{
get
{
return EncoderValue.CompressionCCITT4;
}
}
#endregion
#region Public methods
/// <summary>
/// Converts the specified excel file to tiff output.
/// </summary>
/// <param name="excelFileToConvert">Excel to convert first worksheet.</param>
/// <param name="tiffOutputPath">Path of result tiff file.</param>
public void ConvertToTiff(string excelFileToConvert, string tiffOutputPath, int lastRow = -1, int lastColumn = -1)
{
Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
workbook.LoadFromFile(excelFileToConvert);
Spire.Xls.Worksheet worksheet = workbook.Worksheets[0];
using (MemoryStream stream = new MemoryStream())
{
int paramFirstRow = 0;
int paramFirstColumn = 0;
int paramLastRow = lastRow != -1 ? lastRow : worksheet.LastRow;
int paramLastColumn = lastColumn != -1 ? lastColumn : worksheet.LastColumn;
// Get image and vector informations from worksheet for worksheet content.
worksheet.ToEMFStream(stream, paramFirstRow, paramFirstColumn, paramLastRow, paramLastColumn);
Image imageFile = Image.FromStream(stream);
// Resize image to the desired quality.
Bitmap images = ResetResolution(imageFile as Metafile, tiffOutputDPI);
// Encoder parameters for CCITT 6
EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)TiffCompression);
ImageCodecInfo tiffEncoder = GetImageCodec();
if (tiffEncoder == null)
{
throw new NotSupportedException("No image codec found!");
}
// Save image to specified location with specific codec settings.
images.Save(tiffOutputPath, tiffEncoder, encoderParameters);
}
}
#endregion
/// <summary>
/// Find image codec for save as tiff file.
/// </summary>
/// <returns>
/// <see cref="ImageCodecInfo"/> for tiff file.
/// </returns>
private ImageCodecInfo GetImageCodec()
{
if (ImageCodecInfo.GetImageEncoders() == null) return null;
if (!ImageCodecInfo.GetImageEncoders().ToList().Any(i => i.MimeType == MIME_TYPE)) return null;
return ImageCodecInfo.GetImageEncoders().ToList().Find(i => i.MimeType == MIME_TYPE);
}
/// <summary>
///
/// </summary>
/// <param name="metaFile">Meta file, containing vector informations for resing.</param>
/// <param name="resolution">Resolution to resize the graphic to</param>
/// <returns>
/// Image, recalculated by metafile for desired resultion.
/// </returns>
private Bitmap ResetResolution(Metafile metaFile, float resolution)
{
// Calulate new width and height according to resolution and existing resolution.
int width = (int)(metaFile.Width * resolution / metaFile.HorizontalResolution);
int height = (int)(metaFile.Height * resolution / metaFile.VerticalResolution);
// Create new bitmap with new dimensions.
Bitmap bitmap = new Bitmap(width, height);
bitmap.SetResolution(resolution, resolution);
// Draw the new bitmap in calculated dimensions.
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(metaFile, 0, 0);
}
return bitmap;
}
}
Thanks in advance for your help
Best regards
Alexander Kommer