Opening Pdf in Client Browser in .net core

Converting a webforms site to .net core. The code below that works in old site doesn’t work in new because HttpContext.Current.Response is not valid in .net core.
Can someone advise how i would modify the Save method to open in browser? If i change the Save signature to just a filename, i see it does save correctly to the filesystem.

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.SaveFormat = SaveFormat.Pdf;

var response = HttpContext.Current.Response;

MemoryStream memoryStream = new MemoryStream(byteArray);

Document doc = new Document(memoryStream);

doc.Save(response, "AnnualStatement.pdf", ContentDisposition.Inline, saveOptions);

@jasoncurley You can save the document to stream, and then sent it to the client browser.
For example if it is MVC application you should use MVC controller to sent file to the client’s browser. In your Setup.Configure method add the following line

app.UseMvcWithDefaultRoute();

Create controller that generates file and returns FileContentResult. See the example below:

using Aspose.Words;
using Microsoft.AspNetCore.Mvc;
using System.IO;

namespace TestAspNetCoreApp.Controllers
{
    public class AWController : Controller
    {
        public FileResult SaveFile()
        {

            // Create a simple document using DocumentBuilder.
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // Write some text in the document.
            builder.Writeln("Hello Aspose.Words!");

            // Now save the created document to PDF and return as a FileContentResult.
            using (MemoryStream ms = new MemoryStream())
            {
                doc.Save(ms, SaveFormat.Pdf);
                return File(ms.ToArray(), "application/pdf", "out.pdf");
            }
        }
    }
}

On your page create a link or button, for example:

@Html.ActionLink("Generate Document using Aspose.Words", "SaveFile", "AW")

The implementation might differ depending on your application type, but the main idea is the same - save the document to stream and send the stream to the client’s browser.

Thanks, Alexey.
I’ve tried the following, but receive the error:

System.NotSupportedException: 'Memory stream is not expandable.'

Have you seen this before by chance, any ideas?

 using (MemoryStream ms = new MemoryStream(byteArray))
 {
     Document doc = new Document(ms);
     doc.Save(ms, SaveFormat.Pdf);
     return File(ms.ToArray(), "application/pdf", "out.pdf");
 }

@jasoncurley You are crating MemoryStream from byte array, so the stream size is fixed. Please create MemoryStream with empty parameters.

using (MemoryStream ms = new MemoryStream())
{
    //....................
}

Alexey,
I’ve changed the code to below, and now a pdf gets created, but when i try to open it, i get the error:
“Could not open out.pdf because it is either not a supported file type or because the file has been damaged”.
Please let me know if you have any ideas, it seems that it’s not accepting the byte array im passing as legitimate.

using (MemoryStream ms = new MemoryStream())
{
     ms.Write(byteArray);
    Document doc = new Document(ms);
    doc.Save(ms, saveOptions);
    return File(ms.ToArray(), "application/pdf", "out.pdf");
}

@jasoncurley You are using the same stream as input and as output stream. Use different streams:

using (MemoryStream ms = new MemoryStream(byteArray))
{
    Document doc = new Document(ms);
    using (MemoryStream outStream = new MemoryStream())
    {
        doc.Save(outStream, saveOptions);
        return File(outStream.ToArray(), "application/pdf", "out.pdf");
    }
}

This worked for me, thanks for all your assistance, Alexey.

1 Like

This works in that it creates and downloads the pdf. In my old site, i used the line below that output the pdf directly in the browser window. Is this possible in the current aspose.words version?

doc.Save(response, "AnnualStatement.pdf", ContentDisposition.Inline, saveOptions);

@jasoncurley You should specify content disposition to inline in the response. The following answer in stack overflow describes how to achieve this:
https://stackoverflow.com/questions/38897764/asp-net-core-content-disposition-attachment-inline