Sometimes, we need to preview the word document online. Of course, we can use NPOI to extract the text and tables in Word and then display it on the web page, but this will lose the original format and pictures in Word. A better way is to convert word into pdf and then let customers preview it. Let’s take a look at two solutions based on Office and WPS.
1. Office-based solutions
As the title says, Office-based means that Office is required to be installed on the server. We use C# code to call the COM interface to convert Word into PDF. Let’s take a look at the specific implementation, first quote, and then write the following code:
public bool WordToPDF(string sourcePath, string targetPath) { bool result = false; application = new (); Document document = null; try { = false; document = (sourcePath); (targetPath, ); result = true; } catch (Exception e) { (); result = false; } finally { (); } return result; }
2. WPS-based solution
The biggest benefit of WPS is of course free, and it also has small size. To implement Word to PDF conversion, of course, this requires that the server must have WPS installed, and we still call the COM interface, and then write the following code:
public bool WordToPdfWithWPS(string sourcePath, string targetPath) { app = new (); doc = null; try { doc = (sourcePath, true, true, false, null, null, false, "", null, 100, 0, true, true, 0, true); (targetPath, "", ""); } catch (Exception ex) { (); return false; } finally { (); } return true; }
3. Enterprise-level solutions
For large enterprises, there are often multiple servers, and it is impossible to install office or WPS on each server, or the company does not want to install these useless software on the server at all. What should I do at this time? After all, installing these software on the server is a waste of resources.
Of course, the functions still need to be implemented, so how to solve them? In fact, we can install ofiice or WPS software on one server, and then deploy WCF services or remoting and other WebServices. Other servers can call this service to realize Word to PDF conversion.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.