Articles in this section
Category / Section

How to display a generated PDF file in a new browser tab using ASP.NET MVC?

5 mins read

Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can display the generated PDF file in a new browser tab.

Steps to display generated PDF file in a new browser tab programmatically:

  1. Create a new ASP.NET MVC application project. Create a new ASP.NET MVC application project
  2. Install the Syncfusion.Pdf.AspNet.Mvc NuGet package as a reference to your .NET Framework applications from NuGet.org. NuGet package reference screenshot
  3. A default controller with name HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespaces in that HomeController.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing

 

  1. A default action method named Index will be present in HomeController.cs. Right-click this action method and select Go To View, where you will be directed to its associated view page Index.cshtml.
  2. Add the following code in index.cshtml file to display the generated PDF file in a new browser tab.
    @{
        ViewBag.Title = "Home Page";
    }
    Enter your Name 
        <input type="text" id="txtName" />
    <div>
        <input type="checkbox" id="check1" checked="checked" /> Open Document inside Browser
    </div>
    <input type="submit" value="Generate PDF" class="Button" id="btn1" />
     
    <script type="text/javascript" src="/Scripts/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">  
        $('#btn1').click(function () {
            var checkbox1 = document.getElementById('check1');       
            $.ajax({
                url: '/Home/GeneratePDF',
                type: "POST",
                data: {
                    name : $("#txtName").val()
                },
                datatype: "json",
                success: function (data) {
                    var fileName = JSON.parse(data);
                    var fileUrl = "/Output/" + fileName['file'];    
                    if (checkbox1.checked) {                   
                        window.open(fileUrl);
                    }
                    else {
                        var a = document.createElement('a');                    
                        a.href = fileUrl;
                        a.download = fileName['file'];
                        document.body.appendChild(a);
                        a.click();
                        window.URL.revokeObjectURL(url);
                        a.remove();
                    }
                }          
            });
        });    
    </script>
    

 

  1. Add a new action method named GeneratePDF in HomeController.cs and include the following code snippet.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Set the standard font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text
graphics.DrawString("Hello " + name, font, PdfBrushes.Black, new PointF(0, 0));
MemoryStream ms = new MemoryStream();
string path = Server.MapPath("/Output/");
string fileName = "Sample.pdf";
//Save the document
document.Save(path + fileName);          
//Close the document
document.Close(true);
//Return the file name
var result = new { file = fileName };
return JsonConvert.SerializeObject(result);            

 

VB.NET

'Create a new PDF document
Dim document As New PdfDocument()
'Add a page to the document
Dim page As PdfPage = document.Pages.Add()
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Set the standard font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20)
'Draw the text
graphics.DrawString("Hello " + Name, font, PdfBrushes.Black, New PointF(0, 0))
Dim ms As New MemoryStream()
Dim path As String = Server.MapPath("/Output/")
Dim fileName As String = "Sample.pdf"
'Save the document
document.Save(path + fileName)
'Close the document
document.Close(True)
'Return the file name
Dim result = New ()
Return JsonConvert.SerializeObject(result)

 

A complete working sample can be downloaded from PDFSample.zip.

Refer here to explore the rich set of Syncfusion Essential® PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.

 

Conclusion

I hope you enjoyed learning about how to display a generated PDF file in a new browser tab using ASP.NET MVC

You can refer to our .NET PDF library’s feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to present and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied