ASP. NET uses third-party plug-ins DocX to generate Word conversion to PDF printing, docxpdf

Source: Internet
Author: User
Tags delete cache

ASP. NET uses third-party plug-ins DocX to generate Word conversion to PDF printing, docxpdf

Customer requirements:

  In the last two blogs, I used Word to print expense reimbursement creden。 and print them in batches.

Although it can be printed, there are still problems. Some customer machines may have problems with MS Office installation, so Word generated through DocX cannot be opened on the customer machine, so there are still some flaws.

Another problem is that the printing experience is poor, because the word needs to be downloaded for printing each time, so the user experience is not very good.

Previously, I thought about using the office control to process the plug-in. However, because the browser of each client has different levels of support for the plug-in, this solution was quickly rejected.

Finally, we decided to convert Word to PDF on the server, and then open the PDF online to print the certificate.

Solution:

Convert Word to PDF on the server, and open the PDF online to print the certificate.

The third-party plug-in used here is NETOffice.

. NET encapsulated assembly, used to access the following MS Office applications: Office, Excel, Word, Outlook, PowerPoint, Visio

Office integration, no version restrictions

Including all functions of Office, and

 

For details, please refer to the official website: http://netoffice.codeplex.com/

 Demo code:

Demo interface:

  

  

Production Word program code:

1 private void ExportWord () 2 {3 4 5 // template Path 6 string mWordTemplateSrc = Request. physicalPath. replace ("ExportWordAPI. aspx "," ExportWordTemplate.docx "); 7 8 // Word file path 9 string mGuid = System. guid. newGuid (). toString (); 10 string mWordSrc = Request. physicalPath. replace ("ExportWord \ ExportWordAPI. aspx "," WordToPDF \ Temp \ "+ mGuid + ". docx "); 11 string IsPDF = Request. queryString ["IsPDF"]; 12 13 Memor YStream stream = new MemoryStream (); 14 using (DocX docPrint = DocX. create (stream, DocumentTypes. template) 15 {16 DocX doc; 17 string mIds = Request. queryString ["SheetNo"]; 18 string [] mSheets = mIds. split ('|'); 19 // int iLength = 0; 20 int iPrintCount = 0; 21 int mParagraphsIndex = 0; 22 foreach (string sheetNo in mSheets) 23 {24 // iLength ++; 25 if (string. isNullOrEmpty (sheetNo. trim () contin Ue; 26 27 iPrintCount ++; 28 29 // load template 30 doc = DocX. load (mWordTemplateSrc); 31 // output the printed content to the template 32 ExportWord (ref doc, "FYBX", sheetNo); 33 34 if (iPrintCount> 1) 35 {36 mParagraphsIndex = docPrint. paragraphs. count-1; 37 docPrint. paragraphs [mParagraphsIndex]. insertPageBreakAfterSelf (); 38 mParagraphsIndex = docPrint. paragraphs. count-1; 39 docPrint. paragraphs [mParagraphsIndex]. setLineSpacing (LineS PacingType. line, 0); 40 docPrint. paragraphs [mParagraphsIndex]. setLineSpacing (LineSpacingType. before, 0); 41 docPrint. paragraphs [mParagraphsIndex]. setLineSpacing (LineSpacingType. after, 0); 42} 43 // insert printed content into printed word 44 docPrint. insertDocument (doc, true); 45} 46 if (IsPDF = "Y") 47 {48 docPrint. saveAs (mWordSrc); 49} 50 else 51 {52 docPrint. save (); 53} 54} 55 56 57 if (IsPDF = "Y") 58 {59 Resp Onse. Redirect (".../WordToPDF. aspx? Guid = "+ mGuid); 60} 61 else 62 {63 byte [] tAryByte = stream. toArray (); 64 Response. clear (); 65 Response. addHeader ("Content-Length", tAryByte. length. toString (); 66 Response. appendHeader ("Content-Disposition", string. format ("attachment=filename=exportword_1_02.16.docx", DateTime. now. toString ("yyMMddHHmmss"); 67 Response. contentType = "applications/vnd.openxmlformats-officedocument.wordprocessingml.doc ument"; 68 Response. binaryWrite (tAryByte); 69 Response. flush (); 70 Response. end (); 71} 72} 73 74 private void ExportWord (ref DocX doc, string pFormID, string pSheetNO) 75 {76 IDictionary par = new Dictionary <string, object> (); 77 string SQL = "SELECT * FROM head WHERE head001 = @ head001 AND head002 = @ head002"; 78 par. clear (); 79 par. add ("head001", pFormID); 80 par. add ("head002", pSheetNO); 81 DataTable dtHead = SqlDBHelper. executeDataTable (this. sqlServerConnection, SQL, par); // single-header data 82 83 SQL = "SELECT * FROM body WHERE body001 = @ body001 AND body002 = @ body002"; // single data 84 par. clear (); 85 par. add ("body001", pFormID); 86 par. add ("body002", pSheetNO); 87 DataTable dtBody = SqlDBHelper. executeDataTable (this. sqlServerConnection, SQL, par); 88 89 90 thisReplaceText (ref doc, "head002", dtHead. rows [0] ["head002"]. toString (); 91 thisReplaceText (ref doc, "head003", dtHead. rows [0] ["head003"]. toString (); 92 thisReplaceText (ref doc, "head004", dtHead. rows [0] ["head004"]. toString (); 93 thisReplaceText (ref doc, "head005", dtHead. rows [0] ["head005"]. toString (); 94 thisReplaceText (ref doc, "head006", dtHead. rows [0] ["head006"]. toString (); 95 thisReplaceText (ref doc, "head008", dtHead. rows [0] ["head008"]. toString (); 96 thisReplaceText (ref doc, "head009", dtHead. rows [0] ["head009"]. toString (); 97 thisReplaceText (ref doc, "M", dtHead. rows [0] ["head010"]. toString (); 98 99 int bodyRowIndex = 0; 100 101 int rowLength = 8; 102 Row insertRow = doc. tables [0]. rows [rowLength]; 103 104 int mRowsLength = 0; 105 foreach (DataRow dr in dtBody. rows) 106 {107 mRowsLength + = 1; 108 if (mRowsLength <= 5) 109 {110 doc. tables [0]. rows [2 + mRowsLength]. cells [0]. paragraphs [0]. append (dr ["body003"]. toString (); 111 doc. tables [0]. rows [2 + mRowsLength]. cells [1]. paragraphs [0]. append (dr ["body004"]. toString (); 112 doc. tables [0]. rows [2 + mRowsLength]. cells [2]. paragraphs [0]. append (dr ["body005"]. toString (); 113} 114 else115 {116 // obtain the object of the template row (generally a blank row with only no data on the shelf) 117 doc. tables [0]. rows [rowLength + bodyRowIndex]. xml. addAfterSelf (insertRow. xml), 118 bodyRowIndex + = 1; 119 doc. tables [0]. rows [rowLength + bodyRowIndex]. cells [0]. paragraphs [0]. append (dr ["body003"]. toString (); 120 doc. tables [0]. rows [rowLength + bodyRowIndex]. cells [1]. paragraphs [0]. append (dr ["body004"]. toString (); 121 doc. tables [0]. rows [rowLength + bodyRowIndex]. cells [2]. paragraphs [0]. append (dr ["body005"]. toString (); 122} 123} 124 // Delete the template row. Otherwise, there will be an empty row of 125 doc. tables [0]. removeRow (rowLength); 126} 127 private void thisReplaceText (ref DocX document, string o, string n) 128 {129 string OO = "[" + o + "]"; 131 document. replaceText (OO, n, false, System. text. regularExpressions. regexOptions. none, null, null, MatchFormattingOptions. subsetMatch, true, false); 132}
View Code

Convert Word to PDF code:

1 string guid = Request. queryString ["guid"]; 2 3 // template Path 4 string mWordSrc = Request. physicalPath. replace ("WordToPDF. aspx "," \ Temp \ "+ guid + ". docx "); 5 string mPDFSrc = Request. physicalPath. replace ("WordToPDF. aspx "," \ Temp \ "+ guid + ". pdf "); 6 7 try 8 {9 Word. application wordApplication = new Word. application (); 10 Word. document pDoc = wordApplication. documents. open (mWordSrc); 11 pDoc. activate (); 12 13 pDoc. saveAs (mPDFSrc, WdSaveFormat. wdFormatPDF); 14 wordApplication. quit (); 15 wordApplication. dispose (); 16 17 if (System. IO. file. exists (mPDFSrc) 18 {19 byte [] tAryByte = System. IO. file. readAllBytes (mPDFSrc); 20 Response. clear (); 21 Response. addHeader ("Content-Length", tAryByte. length. toString (); 22 Response. appendHeader ("Content-Disposition", string. format ("inline1_filename=exportword21__1_02.16.docx", DateTime. now. toString ("yyMMddHHmmss"); 23 Response. contentType = "application/pdf"; 24 Response. binaryWrite (tAryByte); 25 Response. flush (); 26 Response. end (); 27 28} 29} 30 catch (Exception ex) 31 {32 Response. clear (); 33 Response. write (ex. message); 34 Response. flush (); 35 Response. end (); 36} 37 finally38 {39 // Delete cache file 40 if (System. IO. file. exists (mPDFSrc) System. IO. file. delete (mPDFSrc); 41 if (System. IO. file. exists (mWordSrc) System. IO. file. delete (mWordSrc); 42 43}
View Code

 

Code address:Http://pan.baidu.com/s/1sltlp5j

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.