Open the Office on the server and convert it to PDF using virtual printing.

Source: Internet
Author: User

Open the Office on the server and convert it to PDF using virtual printing.

1 [webmethod] 2 Public bool convertwordtopdf (string wordpath) 3 {4 bool ret = false; 5 # Region 6 // string dataname = Collection [I]. filename; // local file name (with Path) 7 // string IP = request. userhostaddress; 8 // string wordname = wordpath; 9 string wordpath = wordpath; 10 string pdfpath = wordpath. split ('. ') [0] + ". PDF "; 11 oword. _ document m_document = NULL; 12 oword. _ application m_wordapplication = NULL; 13 object omissing = Type. missing; 14 oword. applicationclass word = new Microsoft. office. interOP. word. applicationclass (); 15 try16 {17 object OBJ = system. reflection. bindingflags. invokemethod; 18 type wordtype = word. getType (); 19 oword. required ents docs = word. documents; 20 Type docstype = docs. getType (); 21 object objdocname = wordpath; 22 oword. document Doc = (oword. document) docstype. invokemember ("open", system. reflection. Bindingflags. invokemethod, null, Docs, new object [] {objdocname, true, true}); 23 // print the output to the specified file 24 type doctype = Doc. getType (); 25 object printfilename = wordpath. split ('. ') [0] + ". ps "; 26 doctype. invokemember ("printout", system. reflection. bindingflags. invokemethod, null, Doc, new object [] {false, false, oword. wdprintoutrange. wdprintalldocument, printfilename}); 27 object savechanges = Microsoft. office. InterOP. Word. wdsaveoptions. wdsavechanges; 28 object saveaspath = wordpath. Split ('.') [0] + "new.doc"; 29 // must be saved! 30 Doc. saveas (ref saveaspath, ref omissing, ref omissing, ref omissing, ref omissing); 31 // 32 doc must be disabled. close (ref savechanges, ref omissing, ref omissing); 33 wordtype. invokemember ("quit", system. reflection. bindingflags. invokemethod, null, word, null); 34 // delete the file as 35 try36 {37 system. io. file. delete (saveaspath. tostring (); 38} 39 catch40 {41} 42 string O1 = printfilename. tostring (); // synchronize with the PS file. 43 string O2 = pdfpath; 44 string O3 = ""; 45 // reference the object 46 try47 {48 export distxlib that converts PS to PDF. required distillerclass PDF = new required distxlib. required distillerclass (); 49 type required type = pdf. getType (); 50 struct type. invokemember ("filetopdf", system. reflection. bindingflags. invokemeth Od, null, PDF, new object [] {O1, O2, O3}); 51 PDF = NULL; 52 // system. io. file. delete ("123.log"); // clear the conversion Log File 53} 54 catch 55 {// throw new exception (" PS to PDF error! "); 56} 57 system. io. file. delete (printfilename. tostring (); // clear the PS file 58 59 system. io. file. delete (printfilename. tostring (). split ('. ') [0] + ". log "); // clear the conversion Log File 60 61 If (system. io. file. exists (pdfpath) 62 {63 ret = true; 64} 65 // to prevent the error that occurs when the dist.exe process is 66 foreach (process proc in system. diagnostics. process. getprocesses () 67 {68 int begpos; 69 int endpos; 70 71 string sprocname = Proc. tostring (); 72 begpos = sprocname. indexof ("(") + 1; 73 endpos = sprocname. indexof (")"); 74 75 sprocname = sprocname. substring (begpos, endpos-begpos); 76 77 If (sprocname. tolower (). compareto ("partition Dist") = 0) 78 {79 try80 {81 Proc. kill (); 82} 83 catch {} 84 break; 85} 86} 87} 88 catch (exception ex) 89 {90 91} 92 93 return ret; 94 # endregion95}
View code

 

Open the Office on the server and convert it to PDF using virtual printing.

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.