How to open a PDF in a new tab or download a PDF document using AJAX Call?
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:
- Create a new ASP.NET MVC application project.
- Install the Syncfusion.Pdf.AspNet.Mvc5 NuGet package as a reference to your .NET Framework application from
- 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
- 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>
- 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.
Refer to this link to explore a rich set of Syncfusion Essential® PDF features.
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 ASP.NET MVC
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 ASP.NET MVC PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC PDF example example 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 comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!