ASP. NET implements automatic conversion of WORD files to PDF files

Source: Internet
Author: User

Requirement analysis: the customer's project is mainly based on the B/S structure, providing a WORD file to automatically Convert PDF files 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, whether to convert the PDF file or the server automatically 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 WORD file automatic conversion PDF

These two categories: the Core Conversion Program is executed in the thread mode, but the first function is for a WORD file, and the second function is for all unconverted WORD files.

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 driver of a PDF virtual printer.

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:

 
 
  1. VarFiles=WScript. Arguments;
  2. VarFso=NewActiveXObject ("Scripting. FileSystemObject ");
  3. VarWord=NewActiveXObject ("Word. Application ");
  4. VarPDF=NewActiveXObject ("includistiller. includistiller.1 ");
  5. Word. ActivePrinter="MS Publisher Color Printer";
  6.  
  7. // Files (0) is the word document file name
  8. // Files (1) is the path to be saved after conversion
  9. // After fso. GetBaseName (files (0) is called, it is the file name without a path or extension.
  10. // Files. length is the number of file parameters. You can use a loop to convert multiple Word documents.
  11.  
  12. VarDocfile=Files(0 );
  13. VarPsfile=Files(1) + fso. GetBaseName (files (0) + ". ps ";
  14. VarPdffile=Files(1) + fso. GetBaseName (files (0) + ". pdf ";
  15. VarLogfile=Files(1) + fso. GetBaseName (files (0) + ". log ";
  16.  
  17. Try {
  18. VarDoc=Word. Documents. Open (docfile );
  19. // Convert a WORD file to a PS file;
  20. Word. PrintOut (false, false, 0, psfile );
  21. Doc. Close (0 );
  22.  
  23. // Convert the PS file into a PDF file;
  24. PDF. FileToPDF (psfile, pdffile ,"");
  25.  
  26. Fso. GetFile (psfile). Delete (); // Delete the PS script file
  27. Fso. GetFile (logfile). Delete (); // Delete the converted Log File
  28.  
  29. Word. Quit ();
  30. WScript. Echo ("isuccess"); // success
  31. WScript. Quit (0 );
  32. }
  33. Catch (x)
  34. {
  35. Word. Quit ();
  36. WScript. Echo ("isfail"); // failed
  37. WScript. Quit (0 );
  38. }

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, add a button button1 to add a function, function name StartConvertPDF

 
 
  1. PublicvoidStartConvertPDF ()
  2. {
  3. Processproc=NewProcess();
  4. Proc. StartInfo. FileName="Cmd.exe";
  5. Proc. StartInfo. WorkingDirectory= @ "C :\";
  6. Proc. StartInfo. CreateNoWindow=True;
  7. Proc. StartInfo. UseShellExecute=False;
  8. Proc. StartInfo. RedirectStandardInput=True; // Input redirection
  9.  
  10. Proc. Start ();
  11. Proc. StandardInput. WriteLine (@ "cscript // nologoc: \ ConvertDoc2PDF. jsc: \ test.doc c :\");
  12. Proc. StandardInput. WriteLine ("exit ");
  13. Proc. WaitForExit ();
  14. }

Conclusion:

The preceding introduction introduces ASP. NET's automatic conversion of WORD files to PDF files. Adobe Acrobat 7.0 Professional,postscript.exe,gs811w32.exe files can be downloaded in itbaby.jss.cn and are included in the same RAR compressed files.

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.

  1. Analysis on PageBase and MasterPage of ASP. NET
  2. XML Web Service Method of ASP. NET
  3. Detailed description of ASP. NET environment deployment
  4. Implement ASP. NET globalization
  5. Solve the Problem of ASP. net ajax script errors

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.