Evaluating Aspose.Words: Problem empty docx with saving memoryStream to Sharepoint

I tried to follow and old support thread but using a second Stream yields an empty Word docs in SharePoint. The file saved to my machine is fine…

using (MemoryStream memoryStream = new MemoryStream())
 using (MemoryStream outputStream = new MemoryStream())
 {
     Document doc = new Document(memoryStream);
     DocumentBuilder builder = new DocumentBuilder(doc);
     
     builder.Writeln("My New Word Doc");
     builder.Write("How do I do this?");

     doc.Save("c:\\xxx\\yyy\\test.docx", SaveFormat.Docx);
     doc.Save(outputStream, SaveFormat.Docx);

     var requestInformation = graphClient
      .Drives["longDriveID"]
      .Items["root"]
      .ItemWithPath("Aspose.docx")
      .Content
      .ToPutRequestInformation(outputStream);

     requestInformation.URI = new Uri(requestInformation.URI.OriginalString + "?@microsoft.graph.conflictBehavior=" + "replace");

     var result4 = await graphClient.RequestAdapter.SendAsync<DriveItem>(requestInformation, DriveItem.CreateFromDiscriminatorValue);

     outputStream.Close();
 }

@dmurphy6 Please check the following points:

  • Make sure your call to SetLicense gets executed. Step through in the debugger.
  • Make sure your code does not catch an exception thrown by Aspose.Words licensing code. For example, Aspose.Words will throw if it cannot find the license.
  • Make sure the input documents do not already have the evaluation message. Aspose.Words does not delete existing evaluation messages.
  • Make sure SetLicense is executed before you instantiate any Document object.

Alexey,

Thank you for your response.

Here’s the start of my code:

class Program
{
    public static async Task Main(string[] args)
    {
        License license = new License();

        // This line attempts to set a license from several locations relative to the executable and Aspose.Words.dll.
        // You can also use the additional overload to load a license from a stream, this is useful,
        // for instance, when the license is stored as an embedded resource.
        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);
        }

I always see “License set successfully” in the Debug Console.

My input doc is created from a new MemoryStream: using (MemoryStream memoryStream = new MemoryStream())

I believe that I have confirmed your four points but file in SharePoint is still blank.

Dave

@dmurphy6 Please try setting the license once application start or in the static constructor of the class that uses Aspose.Words. In this case the license will be set only once per application domain, just like it is intended.

Alexy,

Sorry for my lack of C# experience…

I tried:

class Program
{
    static Aspose.Words.License license;

    static Program()
    {
        license.SetLicense("Aspose.Words.NET.lic");
    }
public static async Task Main(string[] args)
{
......

but code yields a null reference exception on: license.SetLicense(“Aspose.Words.NET.lic”);

Aspose.Words.NET.lic file was copied to directory where Aspose.Words.dll is located…

@dmurphy6 Please use the following code:

class Program
{
    static Program()
    {
        Aspose.Words.License license= new Aspose.Words.License();
        license.SetLicense("Aspose.Words.NET.lic");
    }

......

Alexey,

I think I found the issue.

I had a method that was calling MS Graph to upload the file see below.

If I moved the memoryStream.Position to 0 outside of the method I got a 0 byte file. Moving it into the method seems to have helped. :smiley:

    public static async Task UploadFile(GraphServiceClient graphClient, string graphSiteID, string graphDriveID, string graphDriveitemID, string conflictBehavior, MemoryStream memoryStream)

    {
       memoryStream.Position = 0;

Thank you for your assistance Alexey. I gives me more confident in purchasing the product.

Dave

@dmurphy6 It is perfect that you managed to resolve the problem. Please feel free to ask in case of any further issues. We are always glad to help you.