Keywords: Asp.net, Word, Excel, PDF
Refer to an article by netizensArticleHttp://www.vuu.cn/vuu/vuu-a/4302.html)
--- ASP. net implementation to convert Word PDF Format Author: Anonymous Source: dynamic website production guide time: 2006-3-28 due to a customer's project needs to convert Word documents into PDF format, so I wrote this real-site tutorial
Requirement analysis: the customer's project is mainly based on the B/S structure. A WORD file is automatically converted to a PDF file in the background. After actual tests, if the word document contains more than 100 pages, the conversion takes about 20 minutes (Environment: CPU is Pentium M 1.6g, 95% M memory), and the CPU usage is almost ~ 100%. This result tells the customer that the customer proposes to automatically convert the PDF file after the customer gets off work. If the user confirms that he wants to view the PDF file, if the PDF file is not converted, the user will provide the user with a selection, is it converted to PDF now or automatically converted by the server after the customer leaves work.
Project functions: two functions are required for requirement analysis.
The first is the background conversion of the B/S structure, which must be submitted to the customer for selection.
Second: Windows service automatically converts Word documents to PDF
These two categories: Core ConversionProgramAll are executed in the thread mode, but the first function is for a Word file, and the second function is for all unconverted Word documents.
Analysis till now: we have started to convert it into practice!
I. required tools
Install required tools ms vs. net2003, MS office2003, Adobe Acrobat 7.0 professional,postscript.exe,gs811w32.exe
Installation of MS vs. net2003 is not described
Installation of MS office2003 is not described
Adobe Acrobat 7.0 professional installation instructions
Run the setup.exe file and enter the serial number to run the registration machine. Click the first line to view the serial number. copy and paste it to the Adobe Acrobat 7.0 professional installer dialog box, when the registration appears at the end of installation, click Phone... copy and paste the second line of serial number displayed in the installation program (the first line is the serial number generated by the Registration machine just now) to the second line of the registration machine, click the button on the right, click the third line authorization number and copy and paste it to the last line of the installation program to complete the installation and registration!
Postscript.exe can be installed by default. It is a script required for PDF conversion.
Gs811w32.exe can be installed by default. It is actually a PDF virtual printer driver.
Ii. configure a virtual printer
Go to the control panel of windows, enter the printer, and click the "Add Printer" icon. in the installation dialog box, click "Step by Step". When you select a printer, select "generic" in the manufacturer column. In the printer column, select "MS Publisher color printer ", click Next to complete the installation.
Iii. Start to write the first program (script program)
Why do we need to use the script program for conversion? In fact, the conversion is successful after the object of PDF Distiller is referenced to C #, but the entire PDF Distiller object cannot be released, an error occurs during the second conversion. Therefore, the conversion is implemented using the script program. in this way, we only need to call the script program in the C # program to convert Word to PDF.
Host script file name: convertdoc2pdf. js
Script File Content:
VaR files = wscript. arguments;
VaR FSO = new activexobject ("scripting. FileSystemObject ");
VaR word = new activexobject ("word. application ");
VaR PDF = new activexobject ("includistiller. includistiller.1 ");
Word. activeprinter = "MS Publisher color printer ";
// Files (0) is the word document file name
// Files (1) is the path to be saved after conversion
// After FSO. getbasename (Files (0) is called, it is the file name without a path or extension.
// Files. length is the number of file parameters. You can use a loop to convert multiple Word documents.
VaR docfile = files (0 );
VaR psfile = files (1) + FSO. getbasename (Files (0) + ". Ps ";
VaR pdffile = files (1) + FSO. getbasename (Files (0) + ". pdf ";
VaR logfile = files (1) + FSO. getbasename (Files (0) + ". log ";
Try {
VaR Doc = word. Documents. Open (docfile );
// Convert a Word file to a PS file;
Word. printout (false, false, 0, psfile );
Doc. Close (0 );
// Convert the PS file into a PDF file;
Pdf. filetopdf (psfile, pdffile ,"");
FSO. GetFile (psfile). Delete (); // Delete the PS script file
FSO. GetFile (logfile). Delete (); // Delete the converted Log File
Word. Quit ();
Wscript. Echo ("isuccess"); // success
Wscript. Quit (0 );
}
Catch (X)
{
Word. Quit ();
Wscript. Echo ("isfail"); // failed
Wscript. Quit (0 );
}
Then test the script program.
Start the MS-DOS and enter the following command:
C: \> cscript // nologo c: \ convertdoc2pdf. js c: \ test.doc c :\
Note:
After the operation is successful, we will see the testbench document.
The c: \ test.doc parameter corresponds to files (0) in the script program)
The c: \ parameter corresponds to files (1) in the script program)
You can rewrite the script to support multiple parameters. You can use the for loop to convert multiple Word documents at a time. The conversion function is not used here, this script is executed in the thread of C #, so that multiple Word documents can be converted.
4. Use C # To Call The convertdoc2pdf. js script
Create a C # windows application and add a button button1
Add a function named startconvertpdf
Public void startconvertpdf ()
{
Process proc = new process ();
Proc. startinfo. filename = "cmd.exe ";
Proc. startinfo. workingdirectory = @ "C :\";
Proc. startinfo. createnowindow = true;
Proc. startinfo. useshellexecute = false;
Proc. startinfo. redirectstandardinput = true; // input redirection
Proc. Start ();
Proc. standardinput. writeline (@ "cscript // nologo c: \ convertdoc2pdf. js c: \ test.doc c :\");
Proc. standardinput. writeline ("exit ");
Proc. waitforexit ();
}
Then add the call thread'sCode
Private void button#click (Object sender, system. eventargs E)
{
// Define the line program
Thread thconvert = new thread (New threadstart (startconvertdata ));
Thconvert. Start ();
}
Note: When testing the above C # program, you must add the following namespace
Using system. diagnostics;
Using system. Threading;
5. Robust C # Call Code (which can be placed in B/S Systems for actual consideration)
After completing the C # test in step 1, the readers may find a problem, that is, how to get the output result after the script is run and how to pass parameters to the startconvertdata method called in the thread
1: Passing parameters. Here we can also use a tutorial to show you how to pass parameters through methods in the thread. Now we will talk about a solution. There are many such solutions. I use a class, initialize this class, and then call the method of this class as the method of thread execution
2: Get the output result of the script, use the process object output redirection, that is to change the output direction, so that the script is not output to the console (MS-DOS window), but redirected to the C # program, the asynchronous callback method of the thread is used to display the script running result.
Add a new class named topdf
Using system;
Using system. diagnostics;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Namespace doc2pdf
{
Public class topdf
{
Private string strword = ""; // The Word file here does not contain the path
Private string Spath = "";
Public String sexecresult = "";
Public bool bsuccess = false;
Public topdf (string sparamword, string sparampath)
{
Strword = sparamword;
Spath = sparampath;
}
Public void startconvertpdf ()
{
Process proc = new process ();
Proc. startinfo. filename = "cmd.exe ";
Proc. startinfo. workingdirectory = Spath;
Proc. startinfo. createnowindow = true;
Proc. startinfo. useshellexecute = false;
Proc. startinfo. redirectstandardinput = true; // standard input redirection
Proc. startinfo. redirectstandardoutput = true; // standard output redirection
Proc. Start ();
Proc. standardinput. writeline ("cscript // nologo" + Spath + "convertdoc2pdf. js" + Spath + strword + "" + Spath );
Proc. standardinput. writeline ("exit ");
Sexecresult = Proc. standardoutput. readtoend (); // return the script execution result
Proc. waitforexit ();
Proc. Close ();
}
Public void endconvertpdf (system. iasyncresult AR) // The AR parameter must be written, which is the callback function after the thread completes execution.
{
If (sexecresult. indexof ("isuccess ")! =-1) bsuccess = true;
Else if (sexecresult. indexof ("isfail ")! =-1) bsuccess = false;
// If it is stored in the B/S system, you can write the database here, whether it is successful or failed, and use a WebService program to continuously check the database. This WebService program is not placed in this callback function
// If it is placed in the C/S system, the callback function can not be placed in the class to call the result in the Form program.
}
}
}
Rewrite the code in the original button#click event
Private void button#click (Object sender, system. eventargs E)
{
Topdf my2pdf = new topdf ("test.doc", "C :\\");
Threadstart thstartconvert = new threadstart (my2pdf. startconvertpdf); // start the asynchronous call thread
Thstartconvert. begininvoke (New asynccallback (my2pdf. endconvertpdf), null); // sets the callback function of the asynchronous thread.
// If you need to convert multiple words, you can use a loop
// If it is a B/S system, you can put this code in aspx and continuously access the WebService program by combining the client's technology of refreshing new data display, to determine whether the PDF conversion is successful or fails
}
6. Compile a more robust C # Call Code (for actual consideration, it can be placed in a Windows Service Program)
In actual use, due to the high CPU usage when converting PDF files, consider converting only one word document at a time, discard the use of callback functions in asynchronous threads, and consider a Windows service program.
Write a function checkdata2convert (), constantly check for non-converted Word documents, and use the cyclic call to execute the conversion method startconvertpdf in the topdf class.
// The following is a wildcard code. You can complete it as needed.
// Bool bstart is a global variable that controls the entry and exit of a loop
// Example: Start checking and converting at, then bstart = true at, and start the conversion thread
// Stop the conversion thread at 6: 30, bstart = fasle;
Private void checkdata2convert ()
{
// Check the documents that are not converted under the specified directory. You can also check the documents that are not converted in the database.
String Spath = system. Threading. thread. getdomain (). basedirectory; // The current path.
While (bstart)
{
Int ifilecount = checkword (); // checkword is a method that checks the Word documents that are not converted and returns the number of files that are not converted. The code for this method is written by the reader.
For (INT I = 0; I <ifilecount; I ++)
{
String sword = getwordfilename (I) // getwordfilename is a method that returns a Word file name without a path. The code of this method is written by the reader.
// The startconvertpdf () method in the topdf class uses a Word file name without a path
Topdf my2pdf = new topdf (sword, Spath );
My2pdf. startconvertpdf ();
If (my2pdf. sexecresult. indexof ("isuccess ")! =-1)
{
// Succeeded, write logs, or write back the database
}
Else if (my2pdf. sexecresult. indexof ("isfail ")! =-1)
{
// Failed, write logs, or write back the database
}
}
If (! Bstart) break;
Thread. Sleep (1000 );
}
}
Then, in the service start event, start the thread
Protected override void onstart (string [] ARGs)
{
// You can use a start timer to check whether the start time is reached. When the start time is reached, the execution thread starts. The start execution thread here can be placed in the start scheduled event.
// You can use an end timer to check whether the end time is reached. When the end time is reached, the thread is terminated. The code of the end thread can be placed in the end scheduled event.
// Note: the timer in the component should be used instead of the timer in Windows Forms.
// The Class Name of the timer is system. Timers. Timer. Do not make a mistake. Otherwise, the timer will not run normally.
Bstart = true;
Thread thconvert = new thread (New threadstart (startconvertdata ));
Thconvert. Start ();
}
Then, in the service end event, set the stop thread identifier bstart = false
Protected override void onstop ()
{
Bstart = false;
// Why does the thread not stop at the second time? Because the thread is converting the Word document but it is not finished, only the stop mark is set. After the conversion is complete, the thread execution is also finished.
}
Conclusion:
Adobe Acrobat 7.0 professional,postscript.exe,gs811w32.exe files can be downloaded from itbaby.jss.cn, all of which are included in the compressed files of the same RAR.
Itbaby.jss.cn is a dynamic domain name. The host is in the author's home. If the website cannot be accessed, the computer is not on. Please try again a few days later.