Skip to content

Instantly share code, notes, and snippets.

@miguelerm
Created July 28, 2016 16:24
Show Gist options
  • Save miguelerm/2ee0a5833e19746da9a7263bece55dec to your computer and use it in GitHub Desktop.
Save miguelerm/2ee0a5833e19746da9a7263bece55dec to your computer and use it in GitHub Desktop.
Convert word documents .doc to .pdf in batch.
// Install Nuget Package "NetOffice.Word"
using NetOffice.WordApi;
using System;
using System.IO;
namespace word2pdf
{
internal class Program
{
private static void Main(string[] args)
{
if (args == null || args.Length == 0)
{
PrintUsage();
return;
}
var directoryPath = Path.GetFullPath(args[0]);
if (!Directory.Exists(directoryPath))
{
PrintUsage();
return;
}
Console.WriteLine("Converting word documents in {0}", directoryPath);
using (var word = new Application())
{
foreach (var documentPath in Directory.GetFiles(directoryPath, "*.doc"))
{
var fileName = Path.GetFileNameWithoutExtension(documentPath);
Console.WriteLine("Converting {0}", fileName);
var pdfPath = Path.Combine(directoryPath, fileName + ".pdf");
var document = word.Documents.Open(documentPath);
document.ExportAsFixedFormat(pdfPath, NetOffice.WordApi.Enums.WdExportFormat.wdExportFormatPDF);
document.Close(false);
Console.WriteLine("document {0}.pdf created", fileName);
}
word.Quit();
}
Console.WriteLine("Done");
}
private static void PrintUsage()
{
Console.WriteLine("Please specify a valid Path");
Console.WriteLine("Example:");
Console.WriteLine(" word2pdf C:\\docs");
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment