Last active
July 7, 2023 06:21
-
-
Save ArthurEzenwanne/c443e8bbc67af312aea05c632652ab56 to your computer and use it in GitHub Desktop.
C# code to convert .docx to .pdf using Microsoft.Office.Interop.Word
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
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...
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.
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
thank you, it helped me a lot.