Articles in this section
Category / Section

How to open a PDF in a new tab or download a PDF document using AJAX Call?

7 mins read

The Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can open a PDF in a new tab or download a PDF document using the AJAX Call.

Steps to open a PDF in a new tab or download PDF using the AJAX call programmatically:

  1. Create a new ASP.NET MVC application project. Create a ASP.NET MVC application project.

Select a MVC platform

  1. Install the Syncfusion.Pdf.AspNet.Mvc5  NuGet package as a reference to your .NET Framework application from 
  2. A default controller with the name HomeController.cs gets added to the creation of the 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. Add a following code in the index.cshtml for open a PDF in new tab or download the PDF using the Ajax Call.
    <div class="jumbotron">
        <div style="font-size:17px; margin-bottom:10px">
            Open new browser window
            <input type="checkbox" value="newWindow" name="browser" />
        </div>
        <input type="submit" value="Generate PDF" class="Button" id="btn1" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
     
        <script type="text/javascript">
            $('#btn1').click(function () {
     
                var httpreadtype = new Array();
                $('input[name="browser"]:checked').each(function () {
                    httpreadtype.push(this.value);
                });
     
                $.ajax({
                    url: '/Home/GeneratePDF',
                    type: "POST",
                    responseType: 'arraybuffer',
                    success: function (data) {
                        var atobData = atob(data);
                        var num = new Array(atobData.length);
                        for (var i = 0; i < atobData.length; i++) {
                            num[i] = atobData.charCodeAt(i);
                        }
                        var pdfData = new Uint8Array(num);
     
                        //var blob = new Blob([pdfData], { type: 'text/plain' });
                        blob = new Blob([pdfData], { type: 'application/pdf;base64' });
                        var url = URL.createObjectURL(blob);
     
                        //Open a new tab.
                        if (httpreadtype.length > 0)
                            window.open(url);
                        else {
                            //Download the file.
                            var a = document.createElement('a');
                            a.href = url;
                            a.download = 'File.pdf';
                            a.click();
                        }
                    }
                });
            });
    </script>
    </div>
    

 

  1. Add a new action method named GeneratePDF in HomeController.cs and include the following code sample to create a PDF using C#.

C#

public ActionResult GeneratePDF()
{
    //Create a new PdfDocument
    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;
 
    //Create a solid brush
    PdfBrush brush = new PdfSolidBrush(Color.Black);
 
    //Set the font
    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20f);
 
    //Draw the text
    graphics.DrawString("Hello world!", font, brush, new PointF(20, 20));
 
    MemoryStream stream = new MemoryStream();
    document.Save(stream);
 
    Response.Clear();
    Response.ClearContent();
    Response.Write(Convert.ToBase64String(stream.ToArray()));
    Response.Flush();
    Response.End();
    return View();
}

 

VB.NET

Public Function GeneratePDF() As ActionResult
'Create a new PdfDocument
Dim document As PdfDocument = 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
 
'Create a solid brush
Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
 
'Set the font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20F)
 
'Draw the text
graphics.DrawString("Hello world!", font, brush, new PointF(20, 20))
 
Dim stream As MemoryStream = New MemoryStream()
document.Save(stream)
Response.Clear()
Response.ClearContent()
Response.Write(Convert.ToBase64String(stream.ToArray()))
Response.Flush()
Response.End()
return View()
End Function

 

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

By executing the program, you will get the PDF document as follows.

Output image screenshot

Refer to this link to explore a rich set of Syncfusion Essential PDF features.

Note:

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

 

See Also:

Create a PDF file in Xamarin

Create a PDF file in ASP.NET MVC

Create a PDF file in WPF

Create a PDF file in Windows Forms

Create a PDF file in ASP.NET Core

Download a PDF file using Ajax Call

 

Conclusion

I hope you enjoyed learning about how to open a PDF in a new tab or download a PDF document using AJAX Call.

You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create 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 controls.

If you have any queries or require clarifications, please let us know in the comments section 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 sign in to leave a comment
Access denied
Access denied