A summary of how C # converts word to PDF

Source: Internet
Author: User
This article summarizes the C # conversion Word to PDF method, based on office and WPS two solutions, with a certain reference value, interested in small partners can refer to

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 bool Wordtopdf (string SourcePath, String TargetPath)    {      bool result = false;      Microsoft.Office.Interop.Word.Application application = new Microsoft.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 ();      }      return result;    }

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 bool Wordtopdfwithwps (string SourcePath, String TargetPath)    {      WPS. ApplicationClass app = new WPS. 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.

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.