C # Summary of converting Word to PDF method (office and WPS Two scenarios)

Source: Internet
Author: User

Sometimes we need to preview Word documents online, and of course we can use Npoi to extract text and tables from Word and then display them on top of the page, but this will lose the formatting and images that are in Word. A good way to do this is to convert Word to PDF and then let the customer preview it, and look at two solutions based on office and WPS.

one, office-based Solutions

As the title says, office-based is required to have Office installed on the server. We use C # code to invoke the COM interface, which enables word to be converted to PDF. Here's a look at the implementation, first referencing Microsoft.Office.Interop.Word.dll, and then writing the following code:

 Public BOOLWordtopdf (stringSourcePath,stringTargetPath) {           BOOLresult =false; Microsoft.Office.Interop.Word.Application Application=NewMicrosoft.Office.Interop.Word.Application (); Document Document=NULL; Try{application. Visible=false; Document=application.              Documents.Open (SourcePath); Document.              ExportAsFixedFormat (TargetPath, wdexportformat.wdexportformatpdf); Result=true; }           Catch(Exception e) {Console.WriteLine (e.message); Result=false; }           finally{document.           Close (); }           returnresult; }

Second, WPS-based solutions

The biggest benefit of WPS is of course free, and there is a small size. To achieve word-to-pdf conversion, of course, this requires that the server must have WPS installed, we are still calling the COM interface, and then write the following code:

 Public BOOLWordtopdfwithwps (stringSourcePath,stringTargetPath) {WPS. ApplicationClass app=NewWPS.            ApplicationClass (); Wps. Document Doc=NULL; Try{doc= App. Documents.Open (SourcePath,true,true,false,NULL,NULL,false,"",NULL, -,0,true,true,0,true); Doc. Exportpdf (TargetPath,"",""); }           Catch(Exception ex) {Console.WriteLine (ex).              Message); return false; }           finally{doc.           Close (); }           return true; }

three, enterprise-level solutions

For large enterprises, there are often multiple servers, it is not possible to install Office on each server or WPS, or the company simply does not want to install these useless software on the server. What should we do at this time? After all, installing these software on the server is a waste of resources.

Of course, the function is still to be implemented, then how to solve it? In fact, we can install Ofiice or WPS software on one server, and then deploy a WCF service or remoting such as WebService, other servers can invoke this service to achieve word to PDF conversion.

Cloud drizzling

QQ Exchange Group: 243633526

Blog Address: http://www.cnblogs.com/yunfeifei/

Disclaimer: The original text of this blog only represents my work in a certain time to summarize the views or conclusions, and my unit does not have a direct interest in the relationship. Non-commercial, unauthorized, post please keep the status quo, reprint must retain this paragraph statement, and in the article page obvious location to the original connection.

If you feel that my blog is helpful to everyone, please recommend supporting one, give me the motivation to write.

C # Summary of converting Word to PDF method (office and WPS Two scenarios)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.