Category / Section
How to download multiple PDF files in browser in .NET MVC ?
4 mins read
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can download multiple PDF files in the browser.
Steps to download multiple PDF files in the browser programmatically:
- Create a new ASP.NET MVC application project.
- Install the Syncfusion.Pdf.AspNet.Mvc NuGet package as a reference to your .NET Framework applications from NuGet.org.
- A default controller with the name HomeController.cs gets added upon the creation of the ASP.NET MVC project. Include the following namespaces in that HomeController.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Compression.Zip;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Compression.Zip
- 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.
- Add a new button in Index.cshtml as follows.
<h2>Click the button to Generate PDF</h2> @using (Html.BeginForm("Download", "Home", FormMethod.Post)) { <input type="submit" value="GeneratePDF" /> }
- Add a new action method named Download in HomeController.cs and include the following code snippet to download multiple PDF files as a zip format in the browser.
C#
// Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Server.MapPath("input.pdf"));
// Create zip file
ZipArchive zipArchive = new ZipArchive();
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Import the loaded document page to the new document
document.ImportPage(loadedDocument, i);
// Save the document
MemoryStream stream = new MemoryStream();
document.Save(stream);
// Add the files you want to zip
zipArchive.AddItem(i + ".pdf", stream, false, FileAttributes.Normal);
}
// Zip the filename
MemoryStream memoryStream = new MemoryStream();
zipArchive.Save(memoryStream, false);
return File(memoryStream.ToArray(), "application/zip", "Attachments.zip");
VB.NET
'Load the PDF document
Dim loadedDocument As New PdfLoadedDocument(Server.MapPath("input.pdf"))
'Create zip file
Dim zipArchive As New ZipArchive()
For i As Integer = 0 To loadedDocument.Pages.Count - 1
'Create a new PDF document
Dim document As New PdfDocument()
'Import the loaded document page to the new document
document.ImportPage(loadedDocument, i)
'Save the document
Dim stream As New MemoryStream()
document.Save(stream)
'Add the files you want to zip
zipArchive.AddItem(i + ".pdf", stream, False, FileAttributes.Normal)
Next
'Zip the filename
Dim memoryStream As New MemoryStream()
zipArchive.Save(memoryStream, False)
Return File(memoryStream.ToArray(), "application/zip", "Attachments.zip")
A complete working sample can be downloaded from PDFSample.zip.
Refer here to explore the rich set of features in Syncfusion Essential® PDF.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the free trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.