Skip to content

Instantly share code, notes, and snippets.

@ArthurEzenwanne
Last active March 20, 2019 10:50
Show Gist options
  • Save ArthurEzenwanne/fa5d4d2d651081b39071f1883fe6619c to your computer and use it in GitHub Desktop.
Save ArthurEzenwanne/fa5d4d2d651081b39071f1883fe6619c to your computer and use it in GitHub Desktop.
A console app with input argument to convert word to pdf
using System;
using System.Diagnostics;
namespace ConsoleApp_WordToPDF
{
class Program
{
static void Main(string[] args)
{
// The code provided will print ‘Hello World’ to the console.
// Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.
try
{
using (Process wordPDFConversionProcess = new Process())
{
string inputLocation = @"C:\Users\Arthur\WordToPDFFiles\TestMemoDoc-Copy-Copy-Copy.docx"; //file location of word doc
wordPDFConversionProcess.StartInfo.Arguments = inputLocation;
wordPDFConversionProcess.StartInfo.FileName = @"C:\Users\Arthur\source\repos\WordToPDFConsoleApp\WordToPDFConsoleApp\bin\Debug\WordToPDFConsoleApp.exe";
wordPDFConversionProcess.StartInfo.UseShellExecute = false;
wordPDFConversionProcess.StartInfo.CreateNoWindow = true;
wordPDFConversionProcess.Start();
wordPDFConversionProcess.WaitForExit();
wordPDFConversionProcess.Dispose();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
//To be removed on client env
Console.WriteLine("Document... Converted! Press any key to continue!");
Console.ReadKey(); //Press any key to continue!
}
}
}
using System;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
namespace WordToPDFConsoleApp
{
class Program
{
static void Main(string[] args)
{
/*
* Convert Input.docx into Output.pdf
* Please note: You must have the Microsoft Office 2007 Add-in: Microsoft Save as PDF or XPS installed
* http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en
* Solution source http://cathalscorner.blogspot.com/2009/10/converting-docx-into-doc-pdf-html.html
* argument --> inputLocation
*/
foreach (string arg in args)
{
Convert(arg);
}
}
// Convert method
private static void Convert(string input)
{
// Create an instance of Word.exe
_Application oWord = new Word.Application
{
// Make this instance of word invisible (Can still see it in the taskmgr).
Visible = false
};
// Interop requires objects.
object oMissing = System.Reflection.Missing.Value;
object isVisible = true;
object readOnly = true;
object oInput = input;
object oOutput = input.Replace(".docx", ".pdf");
object oFormat = WdSaveFormat.wdFormatPDF;
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
// Load a document into our instance of word.exe
_Document oDoc = oWord.Documents.Open(
ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing
);
// Make this document the active document.
oDoc.Activate();
// Save this document using Word
oDoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing
);
oDoc.Close(ref saveChanges, ref oMissing, ref oMissing);
// Always close Word.exe.
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment