Skip to content

Instantly share code, notes, and snippets.

@ArthurEzenwanne
Last active July 7, 2023 06:21
Show Gist options
  • Save ArthurEzenwanne/c443e8bbc67af312aea05c632652ab56 to your computer and use it in GitHub Desktop.
Save ArthurEzenwanne/c443e8bbc67af312aea05c632652ab56 to your computer and use it in GitHub Desktop.
C# code to convert .docx to .pdf using Microsoft.Office.Interop.Word
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
*/
Convert(@"C:\Users\Arthur\WordToPDFFiles\TestMemoDoc - Copy - Copy - Copy.docx",
@"C:\Users\Arthur\WordToPDFFiles\TestMemoDoc - Copy - Copy - Copy.pdf", WdSaveFormat.wdFormatPDF);
Console.WriteLine("Document... Converted!");
Console.ReadKey();
}
// Convert method
public static void Convert(string input, string output, WdSaveFormat format)
{
// 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; // Does not cause any word dialog to show up
//object readOnly = false; // Causes a word object dialog to show at the end of the conversion
object oInput = input;
object oOutput = output;
object oFormat = format;
// 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
);
// Always close Word.exe.
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
}
}
@k-elsheikh
Copy link

Thank you very much for the code it is working as expected. Can I use this one on production?

@ArthurEzenwanne
Copy link
Author

ArthurEzenwanne commented Mar 12, 2020 via email

@k-elsheikh
Copy link

Thanks dear.

My production environment will be very simple just an API to convert word document to PDF. The code is working perfectly I can see no issues. But, Have you tried this on production?

@tanjubozok
Copy link

thank you, it helped me a lot.

@Abhijeet1719
Copy link

System.IO.FileNotFoundException: 'Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.' getting this error...

@aldrashan
Copy link

System.IO.FileNotFoundException: 'Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified.' getting this error...

You need to make sure you have Microsoft Office installed on the pc the code is running on. Just using the Nuget package isn't enough.

@Magahaka
Copy link

Is there a reason why this type of conversion takes up to 5-8 minutes for single word document?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment