We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Create PDF From Word Doc and View In PDF viewer without saving

I have successfully saved a PDF created from a word doc template utilizing mail merge events in an asp.net core razor application


I would prefer not to save the file locally and upon rendering the PDF from the word file, view it on my webpage to give the user the ability to view the details in the pdf and print if necessary.  I'm stuck on returning the pdf to the Viewer and loading it after creating it.  Worst case, i would like to save over previously created pdf's

Thanks

the method in my controller:Details.cshtml.cs

public async Task<IActionResult> OnPostGeneratePDFAsync()
{
    Box = await _context.Box
            .Include(b => b.Location)
            .Include(b => b.Part).FirstOrDefaultAsync(m => m.BoxId == 75);


    string dataPath = _hostingEnvironment.WebRootPath + @"/images/";
    FileStream docStream = new FileStream(dataPath + "TTTT.docx", FileMode.Open, FileAccess.Read);
    WordDocument document = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic);

    string[] fieldNames = new string[] { "loadNumber", "loadDate" };
    string[] fieldValues = new string[] { "test load", "test/date" };

    document.MailMerge.Execute(fieldNames, fieldValues);


    MemoryStream stream = new MemoryStream();
    document.Save(stream, FormatType.Docx);
    stream.Position = 0;

    // Creates a new instance of DocIORenderer class.
    DocIORenderer render = new DocIORenderer();
    // Converts Word document into PDF document.
    PdfDocument pdf = render.ConvertToPDF(document);
    MemoryStream memoryStream = new MemoryStream();
    // Save the PDF document.
    pdf.Save(memoryStream);
    render.Dispose();
    pdf.Close();
    document.Close();
    memoryStream.Position = 0;

    return File(memoryStream, "application/pdf", "WordToPDF.pdf");

}


Details.cshtml

@page "{id}"
@model Summit.Pages.BackOffice.Boxes.DetailsModel

@{
    ViewData["Title"] = "Details";
}
<div class="container">
    <form id="pdfCreate" method="post" asp-page-handler="GeneratePDF" style="display:inline"><h2 style="display:inline">Box @Html.DisplayFor(model => model.Box.BoxId) Details:</h2><input type="submit" class="btn btn-primary btn-xs" value="Box Label" /></form>



@section ControlsSection{
    <div class="control-section">
        <ejs-pdfviewer id="pdfviewer" style="height:641px;"></ejs-pdfviewer>
    </div>
}

    <script type="text/javascript">
    window.onload = function () {
        var pdfViewer = document.getElementById('pdfviewer').ej2_instances[0];
        pdfViewer.serviceUrl = window.baseurl + 'api/PdfViewer';
        pdfViewer.load("PDF Succinctly.pdf", null);
    }
    </script>


3 Replies

RT Ramya Thirugnanam Syncfusion Team May 22, 2019 07:04 AM UTC

Hi Matthew, 
 
Thank you for contacting Syncfusion support. 
 
We can load the PDF document as base64 string in the PDF Viewer control. So we can convert the PDF document(converted from Word document) into base64 string and load it in the PDF Viewer control using documentPath property or load() method. We have created a simple sample for converting the Word document to PDF document and loading the same to the PDF Viewer control as base64 string. It can be downloaded from the following link. 
 
 
In the above sample, we have provided a button “Load PDF document as Base64String”. When this button is clicked, a request will be sent to the web service with “GetDocument” action method. In this method, we will convert the Word document into PDF document and return the base64 string to the client. In the client, when the request completed, the base64 string in the response will be loaded in the PDF Viewer control with documentPath property. 
 
Please find the below documentation link for more details. 
 
 
Regards, 
Ramya T 



MS Matthew Souder May 22, 2019 01:25 PM UTC

Thanks Ramya-

I've got it up and running in my application.  The only issue i'm having is when i go to print the pdf from the viewer.  It spools like it is going to print but nother ever happens.  All other PDF Viewer functionality works.

thanks



RT Ramya Thirugnanam Syncfusion Team May 23, 2019 06:57 AM UTC

Hi Matthew, 
 
Sorry for the inconvenience caused, 
 
We have missed to include the PrintImages action method in the PDF viewer controller class and it causes the issue. However, now we have included the PrintImages action method in the controller and share the sample in the following link. 
 
 
  
Regards, 
Ramya T 


Loader.
Live Chat Icon For mobile
Up arrow icon