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)   ; { 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; }   ; }}
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