We’re selecting an image in an Excel spreadsheet and the Aspose picture for this image is then getting encoded in Base64 and saved to the database.
We then have Word documents, created programmatically, which are to use this image. To do this, the Aspose picture is retrieved from the database and decoded. It is then inserted into the document via DocumentBuilder.
Issue
We’ve noticed an issue where sometimes certain shapes within the image are missing, or at least don’t get converted correctly. One such example is the 2D arrow shapes.
When the image is viewed in the word document, these shapes are replaced with a no-entry sign.
The image itself was a screen grab from another excel spreadsheet which contained the shapes. The excel spreadsheet containing the image has been attached. The Workbook which was passed to the code below was created from the uploaded Excel spreadsheet.UAH.zip (1.3 MB)
UAH.zip (1.3 MB)
Does Aspose have an issue converting some of these shapes from an image to an Aspose picture, or could you please advise if there is a better approach for us to use?
image.png (863 Bytes)
image.png (10.9 KB)
Product Used
Aspose.Words 21.8.0
Aspose.Cells 21.10.0
Code Used
- When saving content to DB from Excel:
//Workbook is firstly loaded from MemoryStream from the physical file then passed in to this method
private void FindMatchingContent(Workbook workbook, string excelLink)
{
var pictures = new List
foreach (var sheet in workbook.Worksheets)
{
var worksheetCharts = sheet.Pictures;
pictures.AddRange(worksheetCharts);
}
var matchingPicture = pictures.FirstOrDefault(picture => $"{picture.Worksheet.Name}_{picture.Name}" == excelLink);
if (matchingPicture != null)
{
imageContentForDB = ConvertToBase64EncodedString(matchingPicture);
//Save to DB
}
}
public static string ConvertToBase64EncodedString(Picture picture)
{
using var stream = new MemoryStream();
picture.ToImage(stream, GetImageExportOptions());
var imageContent = Convert.ToBase64String(stream.ToArray());
var content = string.Concat(EncodedImagePrefix, imageContent);
return content;
}
private static ImageOrPrintOptions GetImageExportOptions()
{
const int resolution = 300;
return new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.Png,
SmoothingMode = SmoothingMode.AntiAlias,
HorizontalResolution = resolution,
VerticalResolution = resolution
};
}
- When loading content into Word document:
private static void SetContent(DocumentBuilder documentBuilder, string content, Paragraph paragraph)
{
documentBuilder.MoveTo(paragraph);
var pageInfo = PageSizeLayoutHelper.GetPageInfo(documentBuilder);
var bytes = Convert.FromBase64String(
content.Replace(“data:image/png;base64,”, string.Empty));
var shape = documentBuilder.InsertImage(bytes);
//code to resize image if necessary
}