Skip to content

Commit

Permalink
Merge pull request #6 from SyncfusionExamples/EJ2-70010-Metafile-image
Browse files Browse the repository at this point in the history
Resolve Image not loading issue while Metafile image parsed.
  • Loading branch information
gunasekarant committed Mar 14, 2023
2 parents d614715 + 7dc3350 commit 78cfb82
Showing 1 changed file with 39 additions and 1 deletion.
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

0 comments on commit 78cfb82

Please sign in to comment.