Asp. NET program implementation converts word to PDF format

Source: Internet
Author: User
Tags format bool exit copy execution log thread domain name
asp.net|pdf|word| Procedure | Conversion Preface: Because a customer's project needs to convert Word documents to PDF format, we have written a real-site tutorial

Demand Analysis: Customer's project to B/s structure, providing a Word file in the background automatically converted to PDF, after the actual test, if the Word document has more than 100 pages, the conversion takes about 20 minutes (environment: CPU is Pentium M 1.6g,512m memory), The overall CPU occupancy rate is almost 95%~100%, this result tells the customer later, the customer proposes: to the customer after work, automatically convert the PDF, and if the user confirmed to view the PDF document, if there is no conversion, provided to the customer selection, is now converted to PDF, or the server after the customer after work, Automatic conversion.

Project function: Write two functions according to requirement analysis

The first is: b/s structure background conversion, to submit to the customer selection

The second is: Windows services automatically convert Word documents to PDF

These two categories: the core of the conversion programs are implemented in a threaded manner, but the first feature is for a Word file, the second feature for all of the converted Word documents.

Analysis to now: We start the actual conversion!

   One: Essential Tools

Install the necessary tools MS Vs.net2003,ms office2003,adobe Acrobat 7.0 Professional,postscript.exe,gs811w32.exe

Installation of MS vs.net2003 does not indicate

Installation of MS Office2003 does not indicate

Adobe Acrobat 7.0 Professional Installation Instructions

Run Setup.exe file, the input serial number appears, run the registration machine, with the mouse in the first line under the brush can see the serial number, copy paste to Adobe Acrobat 7.0 Professional Installer dialog box, installed to the last registration, click Phone ... Copy and paste the second line of serial numbers displayed in the installer (the first line is the serial number generated by the Registrar) to the second line of the registration machine. Click on the right button, and then use the mouse to brush the third line of authorization number out, copy and paste it to the last line of the installer, complete the installation registration!

Postscript.exe default installation is OK, it is a PDF conversion required for the script

Gs811w32.exe default installation can be, it is actually a PDF virtual printer driver

   Two: Configure a virtual printer

Enter the Windows Control Panel, enter the printer, and click the "Add Printer" icon. In the Setup dialog box, click "Generic", select "" In the manufacturer column, and in the printer column, select MS Publisher Color Printer. Then press the next step, knowing that the installation is over.

   Three: Start writing the first program (script program)

Why to use a script to convert it, in fact, the actual test process, the use of PDF Distiller object reference to C #, the conversion succeeds, but the entire PDF distiller object can not be released, the second time to convert, there is an error, So we use a scripting program to implement the transformation. So we can convert word to PDF as long as we invoke the script in C # 's program.

Host script filename: convertdoc2pdf.js

Script File Contents:

var files = wscript.arguments;
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var word = new ActiveXObject ("Word.Application");
var PDF = new ActiveXObject ("Pdfdistiller.pdfdistiller.1");
Word. ActivePrinter = "MS Publisher Color Printer";

Files (0) for Word document file name
Files (1) is the path that needs to be saved after conversion
Invoke the FSO. Getbasename (Files (0)), no path, no extension, filename
Files.length is the number of file parameters, using loops to support the conversion of 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);
Translate Word file into PS file;
Word. PrintOut (False, False, 0, psfile);
Doc. Close (0);

ps file converted into PDF file;
Pdf. Filetopdf (Psfile,pdffile, "");

Fso. GetFile (PsFile). Delete ()//Remove PS script file
Fso. GetFile (logfile). Delete ()//Remove the converted log file

Word. Quit ();
WScript.Echo ("Isuccess");/success
Wscript.Quit (0);
}
catch (x)
{
Word. Quit ();
WScript.Echo ("Isfail");//Failure
Wscript.Quit (0);
}
Then test the script program

Start MS-DOS and enter the following command:

C:\>cscript//nologo c:\ConvertDoc2PDF.js C:\Test.doc c:\

Description

The Test.pdf document will be visible when the operation is successful

The C:\Test.doc parameter corresponds to the files in the script (0)

The C:\ parameter corresponds to the files in the script (1)

You can do this by rewriting the script, supporting multiple parameters, using a For loop, converting multiple Word documents at a time, not using multiple file conversions, considering that the script is executed in a C # thread, so you can also convert multiple Word documents.

   four: Using C # to invoke the Convertdoc2pdf.js script

Create a new C # Windows application, add a button button1

Add a function, the name of the letter 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 calling thread's code in the Click event of the button

private void Button1_Click (object sender, System.EventArgs e)
{
Define a line program
Thread Thconvert = new Thread (new ThreadStart (Startconvertdata));
Thconvert.start ();
}
Note: When testing the above C # program, you must add the following namespaces

Using System.Diagnostics;
Using System.Threading;
   Five: Robust C # calling code (practical considerations, can be placed in B/s system)

After completing step 4th of the C # test, the attentive reader may see a little problem, that is how to get the results of the script after the run, how to pass parameters to the Startconvertdata method called in the thread

1: Passing parameters, this can also be used a tutorial to tell you how the thread method to pass parameters, now talk about a scenario, a lot of this scenario, I take a class, initialize the class, and then call the method of the class as a thread execution method

2: Get the output of the script, using the output redirection of the process object, that is, changing the output direction so that the script does not output to the console (MS-DOS window), but redirects the output to the C # program and uses the thread's asynchronous callback method to display the results of the script run.

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 a 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 redirect
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 ()//returns the results of the script execution
Proc. WaitForExit ();
Proc. Close ();

}

The public void endconvertpdf (System.IAsyncResult ar)//ar parameter must be written, which is the callback function after the thread execution completes
{
if (Sexecresult.indexof ("isuccess")!=-1) bsuccess=true;
else if (Sexecresult.indexof ("Isfail")!=-1) Bsuccess=false;
If placed in the B/s system, you can write the database here, is success or failure, and a WebService program to constantly check the database, this WebService program is not placed in the back call function
If placed in the C/s system, the callback function may not be placed in the class to invoke the result in the form program
}
}
}
Overwrite the code in the original button1_click event

private void Button1_Click (object sender, System.EventArgs e)
{
Topdf my2pdf = new Topdf ("Test.doc", "c:\\");
ThreadStart Thstartconvert = new ThreadStart (my2pdf.startconvertpdf); Start Asynchronous Call Thread
Thstartconvert.begininvoke (New AsyncCallback (my2pdf.endconvertpdf), null);/set callback function for asynchronous thread

If you need to convert multiple word, you can use the loop
If it is b/s system, you can put this code in ASPX, combined with the client's no refresh display data technology, continuous access to the WebService program to determine whether the PDF conversion success or failure
}
   Six: Write more robust C # calling code(Actually, you can put it in a Windows service program.)

In practice, due to the high CPU occupancy rate when converting PDFs, consider converting a Word document at the same time, discarding the use of the asynchronous thread's callback function, and considering a Windows service program.

Write a function Checkdata2convert (), constantly checking for Word documents that are not converted, and using the loop call Topdf class to execute the conversion method startconvertpdf

The following is given, the generic code, the user according to their own needs, complete can be
BOOL bstart is a global variable and the entry and exit of control loops
Example: Start checking and converting 18:30, then 18:30, Bstart=true, and start the conversion thread
6:30 Stop conversion thread, Bstart=fasle;

private void Checkdata2convert ()
{
Check the Word document that is not converted in the specified directory, and you can also check the document that is 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 examines a Word document that is not currently converted, returns the number of files that have not been converted, and the code for the 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, and the code for the method is written by the reader
The Startconvertpdf () method in the Topdf class uses a Word file name with no path
Topdf my2pdf = new Topdf (sword, spath);
My2pdf.startconvertpdf ();

if (My2Pdf.sExecResult.IndexOf ("isuccess")!=-1)
{
Success, write log, or write back to the database
}
else if (my2Pdf.sExecResult.IndexOf ("Isfail")!=-1)
{
Failed, write log, or write back to database
}

}

if (!bstart) break;
Thread.Sleep (1000);
}
}
Then, in the start event of the service, start the thread

protected override void OnStart (string[) args)
{
You can use a start timer to check whether the start time, when the time is up, start the execution of the thread, where the start execution thread can be placed in the start Timer event
You can use an end timer to check whether the end time, when the time is up, to end the thread, the end thread code can be placed in the end timed event
Note: You should use timers in the component instead of Windows Forms.
The timer's class name is System.Timers.Timer, do not make a mistake, otherwise the execution will not be normal
bstart = true;
Thread Thconvert = new Thread (new ThreadStart (Startconvertdata));
Thconvert.start ();
}
Then, in the end event of the service, set the identity bstart= false for the stop thread

protected override void OnStop ()
{
bstart = false;
Why not stop the thread at the same time because, given that the thread is converting the Word document but not ending, only the stop identity is set, and the thread executes after the conversion completes.
}


  Conclusion:

Adobe Acrobat 7.0 Professional,postscript.exe,gs811w32.exe These three files can be downloaded in itbaby.jss.cn and are included in the same RAR compressed file.

itbaby.jss.cn is a dynamic domain name, host in the author's home, if the site can not access, indicating that the computer is not open, please try again a few days later.



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.