It's a very common ask in a business settings to have .docx files that need to be converted into .pdf files. There might be .docx templates that need to be sent to customers, but you would prefer to send them a .pdf instead of a .docx file. Fortunately for us, there is an easy way to do that and automate the process using C#!
The only requirement you will need is a portable installation of LibreOffice. You can find the portable installations here. The portable installation gives you raw .exe's that we will use as a dependency in our C# code. It is the LibreOffice LibreOfficeWriterPortable.exe file that will do the conversion of the .docx file into a .pdf file. Install the portable version of LibreOffice and make note of where the LibreOfficeWriterPortable.exe is saved.
In order to programmatically convert .docx files to .pdf files, we need to make use of the LibreOffice CLI. As with any CLI, we need to first pass in the path of the .exe/executable as the first parameter, followed by any required or optional flags in order to use the CLI. In C#, this is how we can do that.
You'll notice at the end of the code, we sleep the thread in a loop waiting for the file to drop in our filesystem. This was a behavior I noticed when using the LibreOffice CLI; it wasn't until a few moments after the process exited did LibreOffice create the .pdf file. This code is an improvement/bugfix from the library I was originally using at https://github.com/smartinmedia/Net-Core-DocX-HTML-To-PDF-Converter.