19.
2-Creating Currency Converter
Program
using System;
using System.IO;
using OfficeOpenXml; // Make sure to install EPPlus via NuGet
using Aspose.Words; // Make sure to install Aspose.Words via NuGet
namespace DocumentConverter
class Program
static void Main(string[] args)
// Convert Excel to PDF
ConvertExcelToPDF("input.xlsx", "output.pdf");
// Convert Word to PDF
ConvertWordToPDF("input.docx", "output.pdf");
static void ConvertExcelToPDF(string inputPath, string outputPath)
FileInfo inputFile = new FileInfo(inputPath);
FileInfo outputFile = new FileInfo(outputPath);
using (ExcelPackage package = new ExcelPackage(inputFile))
{
using (FileStream stream = new FileStream(outputFile.FullName, FileMode.Create))
package.SaveAs(stream);
Console.WriteLine("Excel converted to PDF successfully!");
static void ConvertWordToPDF(string inputPath, string outputPath)
Document doc = new Document(inputPath);
doc.Save(outputPath, SaveFormat.Pdf);
Console.WriteLine("Word document converted to PDF successfully!");
Exercise
Write a C# program that that converts Excel (.xlsx) and Word (.docx) documents to PDF format. You
will use two popular libraries: EPPlus for handling Excel documents and Aspose.Words for handling
Word documents.
Implement a method named ConvertExcelToPDF that takes two parameters: inputPath (string) and
outputPath (string).
Use EPPlus library to load the Excel file specified by inputPath.
Save the Excel document as a PDF file at the location specified by outputPath.
Display a success message upon successful conversion.
Implement a method named ConvertWordToPDF that takes two parameters: inputPath (string) and
outputPath (string).
Use Aspose.Words library to load the Word file specified by inputPath.
Save the Word document as a PDF file at the location specified by outputPath.
Display a success message upon successful conversion.
In the Main method, demonstrate the usage of both conversion methods by converting sample Excel
and Word documents to PDF. You can provide sample input files such as "input.xlsx" and
"input.docx".
Hint
Ensure to include error handling mechanisms, such as try-catch blocks, to handle potential
exceptions during file handling and conversion processes.
Check the compatibility and version requirements of EPPlus and Aspose.Words libraries to ensure
they are compatible with your .NET environment and the targeted document formats.
Consider providing user-friendly feedback or progress updates during the conversion process, such
as displaying a progress bar or status messages, to enhance the user experience.
Explanation
This program is a document converter application that converts both Excel (.xlsx) and Word (.docx)
documents to PDF format. It utilizes two popular libraries, EPPlus for Excel handling and
Aspose.Words for Word document manipulation, both of which need to be installed via NuGet.
In the Main method, two conversion tasks are initiated sequentially. First, the ConvertExcelToPDF
method is called with the input Excel file path ("input.xlsx") and the desired output PDF file path
("output.pdf"). Similarly, the ConvertWordToPDF method is called with the input Word file path
("input.docx") and the same output PDF file path ("output.pdf").
The ConvertExcelToPDF method takes an input file path and an output file path as parameters. It
loads the Excel file using EPPlus, then creates a FileStream to write the converted PDF file. After
saving the Excel package as a PDF document, it displays a success message indicating the
completion of the conversion.
Similarly, the ConvertWordToPDF method takes an input file path and an output file path as
parameters. It loads the Word document using Aspose.Words, then saves it as a PDF document at
the specified output path. Like the Excel conversion method, it also displays a success message
upon completion.