I need to display Word .doc
and .docx
files in a browser. There's no real client-side way to do this and these documents can't be shared with Google docs or Microsoft Office 365 for legal reasons.
Browsers can't display Word, but can display PDF, so I want to convert these docs to PDF on the server and then display that.
I know this can be done using Microsoft.Office.Interop.Word
, but my application is .NET Core and does not have access to Office interop. It could be running on Azure, but it could also be running in a Docker container on anything else.
There appear to be lots of similar questions to this, however most are asking about full- framework .NET or assuming that the server is a Windows OS and any answer is no use to me.
How do I convert .doc
and .docx
files to .pdf
without access to Microsoft.Office.Interop.Word
?
I've recently done this with spire.doc. It has a limit of 3 pages for the free version but it can easily convert a docx file into PDF using something like this
I then sew these individual pdfs together later using itextsharp.pdf
I dont know if this suits your use case as you havent specified the size of the documents you're trying to write but if they're >3 pages or you can manipulate them to be less than 3 pages it will allow you to convert them into pdfs
This was such a PITA, no wonder all the 3rd party solutions are charging $500 per developer.
Good news is the Open XML SDK recently added support for .Net Standard so it looks like you're in luck with the
.docx
format.Bad news at the moment there isn't a lot of choice for PDF generation libraries on .NET Core. Since it doesn't look like you want to pay for one and you cant legally use a 3rd party service we have little choice except to roll our own.
The main problem is getting the Word Document Content transformed to PDF. One of the popular ways is reading the Docx into HTML and exporting that to PDF. It was hard to find, but there is .Net Core version of the OpenXMLSDK-PowerTools that supports transforming Docx to HTML. The Pull Request is "about to be accepted", you can get it from here:
https://github.com/OfficeDev/Open-Xml-PowerTools/tree/abfbaac510d0d60e2f492503c60ef897247716cf
Now that we can extract document content to HTML we need to convert it to PDF. There are a few libraries to convert HTML to PDF, for example DinkToPdf is a cross-platform wrapper around the Webkit HTML to PDF library libwkhtmltox.
I thought DinkToPdf was better than https://code.msdn.microsoft.com/How-to-export-HTML-to-PDF-c5afd0ce
Docx to HTML
Lets put this altogether, download the OpenXMLSDK-PowerTools .Net Core project and build it (just the OpenXMLPowerTools.Core and the OpenXMLPowerTools.Core.Example - ignore the other project). Set the OpenXMLPowerTools.Core.Example as StartUp project. Run the console project:
If you run the project you will see the HTML looks almost exactly like the content in the Word document:
However if you try a Word Document with pictures or links you will notice they're missing or broken.
This CodeProject article addresses these issues: https://www.codeproject.com/Articles/1162184/Csharp-Docx-to-HTML-to-Docx
I had to change the
static Uri FixUri(string brokenUri)
method to return aUri
and I added user friendly error messages.Now we can get images:
If you only want to show Word .docx files in a web browser its better not to convert the HTML to PDF as that will significantly increase bandwidth. You could store the HTML in a file system, cloud, or in a dB using a VPP Technology.
HTML to PDF
Next thing we need to do is pass the HTML to DinkToPdf. Download the DinkToPdf (90 MB) solution. Build the solution - it will take a while for all the packages to be restored and for the solution to Compile.
IMPORTANT:
These dlls are in the v0.12.4 folder. Depending on your PC, 32 or 64 bit, copy the 3 files to the DinkToPdf-master\DinkToPfd.TestConsoleApp\bin\Debug\netcoreapp1.1 folder.
IMPORTANT 2:
Set the DinkToPfd.TestConsoleApp as StartUp project and change the Program.cs file to read the htmlContent from the HTML file saved with Open-Xml-PowerTools instead of the Lorium Ipsom text.
The result of the Docx vs the PDF is quite impressive and I doubt many people would pick out many differences (especially if they never see the original):
Ps. I realise you wanted to convert both
.doc
and.docx
to PDF. I'd suggest making a service yourself to convert .doc to docx using a specific non-server Windows/Microsoft technology. The doc format is binary and is not intended for server side automation of office.Using the LibreOffice binary
The LibreOffice project is a Open Source cross-platform alternative for MS Office. We can use its capabilities to export
doc
anddocx
files toPDF
. Currently, LibreOffice has no official API for .NET, therefore, we will talk directly to thesoffice
binary.It is a kind of a "hacky" solution, but I think it is the solution with less amount of bugs and maintaining costs possible. Another advantage of this method is that you are not restricted to converting from
doc
anddocx
: you can convert it from every format LibreOffice support (e.g. odt, html, spreadsheet, and more).The implementation
I wrote a simple
c#
program that uses thesoffice
binary. This is just a proof-of-concept (and my first program inc#
). It supportsWindows
out of the box andLinux
only if the LibreOffice package has been installed.This is
main.cs
:Resources
Results
I had tested it on Arch Linux, compiled with
mono
. I run it using mon and the Linux binary, and withwine
: using the Windows binary.You can find the results in the Tests directory:
Input files: testdoc.doc, testdocx.docx
Outputs:
Wine: testdoc, testdocx.
Mono: testdoc, testdocx.