Skip to content

Commit

Permalink
Merge pull request #137 from AkashArul26/main
Browse files Browse the repository at this point in the history
ES-848213 Sample to convert multiple Word documents to PDFs and zip the PDFs in C#
  • Loading branch information
MohanaselvamJothi authored Oct 18, 2023
2 parents 71cdb7a + a88c39a commit 782f81d
Showing 1 changed file with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
using System.IO.Compression;
using Syncfusion.Compression.Zip;

//Creating new zip archive
//Creating new zip archive.
Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
//You can use CompressionLevel to reduce the size of the file.
zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best;

//Get the input files from the folder
//Get the input Word documents from the folder.
string folderName = @"../../../InputDocuments";
string[] inputFiles = Directory.GetFiles(folderName);
DirectoryInfo directoryInfo = new DirectoryInfo(folderName);
Expand All @@ -22,47 +20,48 @@
files.Add(name);
}

//Converts each Word documents to PDF documents
//Convert each Word documents to PDF.
for (int i = 0; i < inputFiles.Length; i++)
{
//PDF file name
//Output PDF file name.
string outputFileName = files[i] + ".pdf";
//Loads an existing Word document
//Load an existing Word document.
using (FileStream inputStream = new FileStream(inputFiles[i], FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Converts Word document to PDF document
//Convert Word document to PDF.
MemoryStream outputStream = ConvertWordToPDF(inputStream);
//Add the converted PDF file to zip
//Add the converted PDF file into zip archive.
zipArchive.AddItem(outputFileName, outputStream, true, Syncfusion.Compression.FileAttributes.Normal);
}
}

//Zip file name and location
//Zip file name and location.
FileStream zipStream = new FileStream(@"../../../OutputPDFs.zip", FileMode.OpenOrCreate, FileAccess.ReadWrite);
zipArchive.Save(zipStream, true);
zipArchive.Close();


/// <summary>
/// Convert Word document to PDF
/// Convert Word document to PDF.
/// </summary>
/// <param name="inputStream">Input Word document stream</param>
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())
{
//Converts Word document into PDF document
//Convert Word document into PDF document
using (PdfDocument pdfDocument = renderer.ConvertToPDF(document))
{
MemoryStream outputStream = new MemoryStream();
//Save the PDF.
pdfDocument.Save(outputStream);
outputStream.Position = 0;
//Closes the instance of PDF document object
pdfDocument.Close();
return outputStream;
}
}
}
return outputStream;
}

0 comments on commit 782f81d

Please sign in to comment.