Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Image not loading issue while Metafile image parsed. #6

Merged
merged 5 commits into from
Mar 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using WDocument = Syncfusion.DocIO.DLS.WordDocument;
using WFormatType = Syncfusion.DocIO.FormatType;
using Syncfusion.EJ2.SpellChecker;
using EJ2DocumentEditorServer;

namespace EJ2DocumentEditorServer.Controllers
{
Expand Down Expand Up @@ -44,12 +43,47 @@ public string Import(IFormCollection data)
file.CopyTo(stream);
stream.Position = 0;

//Hooks MetafileImageParsed event.
WordDocument.MetafileImageParsed += OnMetafileImageParsed;
WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower()));
//Unhooks MetafileImageParsed event.
WordDocument.MetafileImageParsed -= OnMetafileImageParsed;

string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
document.Dispose();
return json;
}

//Converts Metafile to raster image.
private static void OnMetafileImageParsed(object sender, MetafileImageParsedEventArgs args)
{
//You can write your own method definition for converting metafile to raster image using any third-party image converter.
args.ImageStream = ConvertMetafileToRasterImage(args.MetafileStream);
}

private static Stream ConvertMetafileToRasterImage(Stream ImageStream)
{
//Here we are loading a default raster image as fallback.
Stream imgStream = GetManifestResourceStream("ImageNotFound.jpg");
return imgStream;
//To do : Write your own logic for converting metafile to raster image using any third-party image converter(Syncfusion doesn't provide any image converter).
}

private static Stream GetManifestResourceStream(string fileName)
{
System.Reflection.Assembly execAssembly = typeof(WDocument).Assembly;
string[] resourceNames = execAssembly.GetManifestResourceNames();
foreach (string resourceName in resourceNames)
{
if (resourceName.EndsWith("." + fileName))
{
fileName = resourceName;
break;
}
}
return execAssembly.GetManifestResourceStream(fileName);
}

[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
Expand Down Expand Up @@ -118,7 +152,11 @@ public string SystemClipboard([FromBody]CustomParameter param)
{
try
{
//Hooks MetafileImageParsed event.
WordDocument.MetafileImageParsed += OnMetafileImageParsed;
WordDocument document = WordDocument.LoadString(param.content, GetFormatType(param.type.ToLower()));
//Unhooks MetafileImageParsed event.
WordDocument.MetafileImageParsed -= OnMetafileImageParsed;
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
document.Dispose();
return json;
Expand Down