How to convert multiple Word documents to multiple PDFs and zip the PDFs in C#?
Syncfusion Essential DocIO is a .NET Core Word library used to create, read, edit and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can convert multiple Word documents to PDF in C#.
You can zip the converted PDF files using Syncfusion Compression library which is one of the dependent packages of DocIO.
Steps to convert multiple Word documents to PDFs and zip the PDFs in C#
- Create new C# .NET Core console application.
- Install the Syncfusion.DocIORenderer.NET.Core NuGet package as a reference to your .NET Core application from NuGet.org.
Note:
-
The Syncfusion Compression library is a dependent package of the DocIORenderer NuGet package. Once DocIORenderer is installed, the Compression library will automatically be installed as its dependent package.
-
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.
- Include the following namespace in the Program.cs file.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
- Use the following code example to convert multiple Word documents to PDFs and zip the PDFs.
//Create new zip archive
Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
//Use CompressionLevel to reduce the size of the file.
zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best;
//Get the input Word documents from the folder.
string folderName = @"../../../InputDocuments";
string[] inputFiles = Directory.GetFiles(folderName);
DirectoryInfo directoryInfo = new DirectoryInfo(folderName);
List<string> files = new List<string>();
FileInfo[] fileInfo = directoryInfo.GetFiles();
foreach (FileInfo fi in fileInfo)
{
string name = Path.GetFileNameWithoutExtension(fi.Name);
files.Add(name);
}
//Convert each Word document to PDF.
for (int i = 0; i < inputFiles.Length; i++)
{
//Output PDF file name.
string outputFileName = files[i] + ".pdf";
//Load an existing Word document.
using (FileStream inputStream = new FileStream(inputFiles[i], FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Convert Word document to PDF.
MemoryStream outputStream = ConvertWordToPDF(inputStream);
//Add the converted PDF file into zip archive.
zipArchive.AddItem(outputFileName, outputStream, true, Syncfusion.Compression.FileAttributes.Normal);
}
}
//Zip file name and location.
FileStream zipStream = new FileStream(@"../../../OutputPDFs.zip", FileMode.OpenOrCreate, FileAccess.ReadWrite);
zipArchive.Save(zipStream, true);
zipArchive.Close();
- Helper method to convert Word document to PDF.
/// <summary>
/// Convert Word document to PDF.
/// </summary>
static MemoryStream ConvertWordToPDF(FileStream inputStream)
{
MemoryStream outputStream = new MemoryStream();
//Open an existing Word document.
using (WordDocument document = new WordDocument(inputStream, FormatType.Automatic))
{
//Initialize DocIORenderer.
using (DocIORenderer renderer = new DocIORenderer())
{
//Convert Word document into PDF document
using (PdfDocument pdfDocument = renderer.ConvertToPDF(document))
{
//Save the PDF.
pdfDocument.Save(outputStream);
outputStream.Position = 0;
}
}
}
return outputStream;
}
A complete working example of how to convert multiple Word documents to PDFs and zip the PDFs can be downloaded from GitHub.
By executing the example program, you will get the zip folder containing the output PDF files as like below.
Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples
An online example to convert Word document to PDF in ASP.NET Core web application.
See Also
How to zip files using the Syncfusion.Compression.Zip namespace?
How to zip all the files in subfolders using Syncfusion’s Compression?