C # using WPS (API V9) to turn word into PDF

Source: Internet
Author: User

We don't produce code just code for porters

Let's take a look at a piece of code that won't run. Various objects are not applied to the instance:

Using System;

Using system.collections.generic;using system.linq;using system.text;namespace word2pdfwithwps{    Internal class program    {        private static void Main (string[] args)     &nbs P   {            var word = @ "d:\c# gadget \word2pdfwithwps\word2pdfwithwps\ 76c309855c1e240242693fd7d7c74d7e.docx ";            var pdf = @" d:\c# gadget \1.pdf ";  & nbsp         wordexportaspdf (Word, pdf);            Console.readkey ();       }        public static bool Wordtopdfwithwps (string sourcepath, String Targetpat h)         {            Word.applicationclass app = new Word.applicationc Lass ();            Word.Document doc = null;            TRY&NB Sp           {                doc = app. Documents.Open (SourcePath, True, True, false, NULL, NULL, FALSE, "", NULL, 0, True, True, 0, true);    &nbs P           Doc. SaveAs (TargetPath);           }            catch (Exception E x)             {                Console.WriteLine (ex. Message);                return false;           }& nbsp           finally            {          &N Bsp    //doc. Close ();           }            return true;    &NB Sp  }        public static string Wordexportaspdf (String fileName, String outputfilename)   &NB Sp   &NBSp {            String issucceed = "OK";            Word.wdexport Format FileFormat = word.wdexportformat.wdexportformatpdf;            Word.Application WordApp = null;            if (WordApp = = null) WordApp = new Word.Application ();  &nbs P         word._document WordDoc = null;            try    &NBSP ;       {                WordDoc = WordApp.Documents.Open (FileName, F Alse, True);                Worddoc.exportasfixedformat (OutputFileName, FileFormat);           }            catch (Exception ex) &nbsp ;           {                issucceed = ex. message;          }            finally            {      & nbsp        //if (WordDoc! = null)                //{               //   worddoc.close (false);               /   worddoc = null;               //}    &NBS P          //if (WordApp! = null)                //{               //   wordapp.quit (false);          & nbsp    /   wordapp = null;               //}    &NBS P      }            return issucceed;       }  &nbsp ; }}


And then to the WPS official forum to download a written by someone else, as long as the installation of WPS can be used directly

Address: http://bbs.wps.cn/forum.php?mod=viewthread&tid=22434594&highlight=C%23

Main conversion Code

Using system;using system.io;using Word;namespace wpstopdf{class wps2pdf:idisposable {dynamic wps; Public Wps2pdf () {

Here to create a WPS instance do not know what to use the Sao operation is not reported to miss the local installation is wps2016

            Type type = Type.gettypefromprogid ("kwps.application");      &NBS P     wps = activator.createinstance (type);       }        public void to PDF (String wpsfilename, string pdffilename = null)         {            I F (wpsfilename = = null) {throw new ArgumentNullException ("Wpsfilename");}             if (pdffilename = = null)             {                Pdffilename = path.changeextension (Wpsfilename, "PDF");      &NB Sp    }            Console.WriteLine (string. Format (@ "Converting [{0}]     , [{1}]", Wpsfilename, Pdffilename));             //is full of dynamic   I look at my face.             Dynamic doc = wps. Documents. Open (Wpsfilename, visible:false);//This sentence is probably opened with WPS  word   not display interface             Doc. ExportAsFixedFormat (Pdffilename, wdexportformat.wdexportformatpdf);//doc   ext pdf             Doc. Close ();       }        public void Dispose ()         {            if (wps! = null) {wps. Quit (); }       }   }}

C # using WPS (API V9) to turn word into PDF

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.