ASP. Net converts Word to PDF Format

Source: Internet
Author: User

As a customer's project needs to convert Word documents into PDF format, this real-site tutorial is provided.

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: 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 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: est.doc c:

Note:

After the operation is successful, we will see the testbench document.

C: The est.doc parameter corresponds to files (0) in the script program)

C: The 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.

There are 3 pages in this news. Currently, there are 3 pages in 1st.


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.