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 browser.
Steps to download multiple PDF files in 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 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.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 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 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 zip format in 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(); //Imports 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); } //Zips 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() 'Imports 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 'Zips 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 rich set of features in Syncfusion Essential® PDF.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from freetrial 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.