Problem with Saving docs file to Html

Hi all,
I had this code:
[HttpGet(“getDocToHTML”)]
public string GetDocToHTMLApsos(string articleId, string file)
{
byte[] data = GetWordContent(articleId, file);
Stream stream = new MemoryStream(data);
Aspose.Words.Document document = new Aspose.Words.Document(stream);
string html= ConvertDocumentToHtml(document);
return html;
}

public string ConvertDocumentToHtml(Document doc)
{

        string html = string.Empty;

        // Save docuemnt to MemoryStream in Hml format

        using (MemoryStream htmlStream = new MemoryStream())

        {

            doc.Save(htmlStream, SaveFormat.Html);

            // Get Html string

            html = Encoding.UTF8.GetString(htmlStream.GetBuffer(), 0, (int)htmlStream.Length);

        }
        // There could be BOM at the beggining of the string.
        // We should remove it from the string.
        while (html[0] != '<')
            html = html.Substring(1);
        return html;
    }

GetWordContent: return the content of the doc with the id: ArticleId, and fileName: File in sharepoint.

The code works normaly in my local machine, it takes the word docs and return the Html but when i deploy to azure i had this error:

InvalidOperationException: Image file cannot be written to disk. When saving the document to a stream either ImagesFolder should be specified or custom streams should be provided via ImageSavingCallback. Please see documentation for details.

Thank you.

@jugurtha,

You can try saving images to HTML in Base64 format (see HtmlSaveOptions.ExportImagesAsBase64 Property).

Example C# code:

Document doc = new Document("C:\\Temp\\input.docx");
            
Aspose.Words.Saving.HtmlSaveOptions opts = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Html);
opts.ExportImagesAsBase64 = true;

MemoryStream stream = new MemoryStream();
doc.Save(stream, opts);
stream.Position = 0;

@awais.hafeez
Thank you for your response, it works but the images don’t work normaly i think it’s about the licence:
i add this code to my Startup.cs in my .net application:
Aspose.Words.License license = new Aspose.Words.License();

        try
        {
            license.SetLicense("Aspose.Words.NET.lic");
            Console.WriteLine("License set successfully.");
        }
        catch (Exception e)
        {
            // We do not ship any license with this example, visit the Aspose site to obtain either a temporary or permanent license. 
            Console.WriteLine("\nThere was an error setting the license: " + e.Message);
        }

it works in my local machin but it’s not working when i deploy to azure, when i run the project it convert the docs to html but i see this text in the html file:
image.png (3.2 KB)

@jugurtha,

Please check the following points:

  • Please check if your application has permissions to access/read directory/file
  • Make sure your call to SetLicense gets always executed. Debug your program and step through in the debugger to identify the problematic line of code.
  • Make sure your code does not silently catch an exception thrown by Aspose.Words licensing code. For example, Aspose.Words will throw if it cannot find the license.
  • Make sure SetLicense is executed before you instantiate any Document object.

Also, you can declare a global variable to determine whether a valid license has been applied before starting document processing:

using Aspose.Words;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            License license = new License();
            var isLicensed = false;
            try
            {
                license.SetLicense(@"C:\Temp\20201020-Aspose.Words.NET.lic"); 
                isLicensed = true;
            }
            catch (InvalidOperationException)
            {
                // the License.SetLicense() method will throw InvalidOperationException
                // for an invalid/wrong/expired license etc.
                isLicensed = false;
            }

            if (isLicensed)
            {
                Console.WriteLine("License is fine");

                Document doc = new Document();
                DocumentBuilder builder = new DocumentBuilder(doc);

                builder.Writeln("Hello World!!!");

                doc.Save(@"C:\Temp\output.pdf");
            }
            else
            {
                Console.WriteLine("License not applied");
            }

            Console.WriteLine("End of process... press any key");
            Console.Read();
        }
    }
}

You can also use the following method to determine whether the license was set or not.

private static bool IsLicensed()
{
    const string testString = "test";
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.Write(testString);

    using (MemoryStream ms = new MemoryStream())
    {
        doc.Save(ms, SaveFormat.Docx);
        ms.Position = 0;
        doc = new Document(ms);
    }

    string test = doc.ToString(SaveFormat.Text).Trim();

    return doc.ToString(SaveFormat.Text).Trim() == testString;
}