native files.zip (335.1 KB)
i used groupdocs.conversion 25.2.1,
public async Task ConvertAsync(Document document)
{
_logger.LogInformation($“{nameof(ConvertAsync)} started for document {{DocumentName}}.”, document.Name);
TargetConversion? conversion = document.GetConversionToPdf();
MagickFormatInfo? imageFormat = document.GetImageFormat();
(int? Width, int? Height) size = document.GetImageSize();
ImageResizer resizer = new ImageResizerBuilder()
.WithConversion(conversion)
.WithDocument(document)
.WithImageFormatInfo(imageFormat)
.WithImageSize(size.Width, size.Height)
.Build();
if ((conversion == null) && (imageFormat == null))
{
throw new IncompatibleForPdfConversionException(document.Name);
}
PdfConvertOptions? pdfConvertOptions = null;
AdjustedDocument? adjustedDocument = await resizer.GetAdjustedDocument();
pdfConvertOptions = adjustedDocument.GetPdfConvertOptions();
adjustedDocument.Content.Seek(0, SeekOrigin.Begin);
using var outputPdfStream = new MemoryStream();
using var converter = new Converter(() => adjustedDocument.Content);
_logger.LogInformation($"Conversion for document {{DocumentName}} started.", document.Name);
converter.Convert(() => outputPdfStream, pdfConvertOptions);
_logger.LogInformation($"Conversion for document {{DocumentName}} finished.", document.Name);
var outputStream = new MemoryStream(outputPdfStream.ToArray());
outputStream.Seek(0, SeekOrigin.Begin);
_logger.LogInformation($"{nameof(ConvertAsync)} finished for document {{DocumentName}}.", document.Name);
return outputStream;
}
public static class DocumentConverterExtensions
{
public static PdfConvertOptions? GetPdfConvertOptions(this Document document)
{
TargetConversion? conversion = GetConversionToPdf(document);
var conversionConvertOptions = conversion?.ConvertOptions as PdfConvertOptions;
if ((conversionConvertOptions != null) && document is AdjustedDocument adjustedDocument)
{
conversionConvertOptions.Dpi = 300;
conversionConvertOptions.PageWidth = adjustedDocument.Width ?? conversionConvertOptions.PageWidth;
conversionConvertOptions.PageHeight = adjustedDocument.Height ?? conversionConvertOptions.PageHeight;
}
return conversionConvertOptions;
}
public static TargetConversion? GetConversionToPdf(this Document document)
{
PossibleConversions? conversions = GroupDocs.Conversion.Converter.GetPossibleConversions(document.Extension);
return conversions[FileType.FromExtension(Constants.PdfExtension)];
}
public static MagickFormatInfo? GetImageFormat(this Document document)
{
return MagickNET.SupportedFormats
.FirstOrDefault(x =>
x.IsReadable && ("." + x.Format).Equals(document.Extension, StringComparison.OrdinalIgnoreCase));
}
public static (int? Width, int? Height) GetImageSize(this Document document)
{
try
{
// should handle sgi files
(int? Width, int? Height) rgbDimensions = GetRgbDimensions(document);
// if not sgi try to use MagickImageInfo
if (rgbDimensions.Height is null && rgbDimensions.Width is null)
{
var magickImageInfo = new MagickImageInfo(document.Content);
return (magickImageInfo.Width, magickImageInfo.Height);
}
return (rgbDimensions.Width, rgbDimensions.Height);
}
catch (MagickException e)
{
// size cannot be detected
return (null, null);
}
}
/// <summary>
/// MagickImage requires width and height to open MagickFormat.Rgb images.
/// The method reads ResizedWidth and ResizedHeight for Sgi/Rgb files from its header.
/// https://en.wikipedia.org/wiki/Silicon_Graphics_Image
/// Field number Hex offset Length Field name Description
/// 1 0x00 2 byte File signature/magic number For show document is SGI document. Should equal 0x01 0xda for SGI
/// document.
/// 2 0x02 1 byte Compression 0 = uncompressed, 1 = RLE compressed
/// 3 0x03 1 byte Bytes per pixel 1 = 8 bit, 2 = 16 bit
/// 4 0x04 2 bytes Dimension Image dimension, equal 3 for RGBA image
/// 5 0x06 2 bytes X size Image width
/// 6 0x08 2 bytes Y size Image height
/// 7 0x0A 2 bytes Number of channels Number channel in image, equal 4 for RGBA image
/// 8 0x0C 4 bytes Minimum pixel value Smallest pixel value in image
/// 9 0x10 4 bytes Maximum pixel value Largest pixel value in image
/// 10 0x14 4 bytes Reserved No use, ignore
/// 11 0x18 80 bytes Image name C string name have last byte equal 0x00
/// 12 0x68 4 bytes Color map ID Only for color map image
/// 13 0x6C 404 bytes Dummy For make head 512 bytes long. Ignore
/// </summary>
/// <param name="stream">File stream to read.</param>
/// <returns>Returns ResizedWidth and ResizedHeight as a tuple. Returns (null, null) for non sgi files.</returns>
private static (int? Width, int? Height) GetRgbDimensions(Document document)
{
if (document.Content.Length < 10)
{
return (null, null);
}
var buffer = new byte[10];
document.Content.Read(buffer, 0, 10);
document.Content.Position = 0;
bool isSgi = (buffer[0] == 0x01) && (buffer[1] == 0xda);
return (
isSgi ? buffer[6] * 256 + buffer[7] : null,
isSgi ? buffer[8] * 256 + buffer[9] : null);
}
}
this is my code used for all type of files docx and ppt file is convert successfully but excel files have a issue
input.docx (108.7 KB)