Private string adobedomainprint = "Adobe PDF ";
Private string adobedisprint = "Acrobat Distiller ";
Private string regroot = "software // Adobe // Acrobat Distiller //";
Private string printerfilename = "nvidist.exe ";
Private string regname = "installpath ";
/// <Summary>
/// The installation path of nvidist.exe is obtained.
/// </Summary>
/// <Returns> </returns>
Private string getadobedisfilepath ()
{
Registrykey regkey = NULL;
Registrykey required distkey = NULL;
String printername = string. empty;
Int I;
String regrootversion = string. empty;
Regkey = registry. localmachine;
// Applicable to version 4-8 of DIST
For (I = 4; I <9; I ++)
{
Regrootversion = string. Format ("{0} {1}. 0", regroot, I );
Repeated distkey = regkey. opensubkey (regrootversion );
If (includistkey! = NULL)
{
Printername = includistkey. getvalue (regname, "") + "//" + printerfilename;
If (file. exists (printername ))
{
Return printername;
}
}
}
Throw new exception ("Acrobat Distiller printer not found! ");
}
/// <Summary>
/// Obtain Adobe printer
/// "Adobe PDF" or "Acrobat Distiller"
/// </Summary>
/// <Returns> </returns>
Private string getadobeprinter ()
{
Foreach (string printername in printersettings. installedprinters)
{
If (printername. toupper (). indexof (adobedomainprint. toupper ())! =-1 | printername. toupper (). indexof (adobedisprint. toupper ())! =-1)
{
Return printername;
}
}
Return string. empty;
}
Public void convertword2pdf (string sourcefile)
{
If (printersettings. installedprinters. Count = 0)
Throw new exception ("did not find the printer, please install the printer .");
If (! File. exists (sourcefile ))
Throw new exception (string. Format ("file not found: {0}", sourcefile ));
String strdir = path. getdirectoryname (sourcefile );
String strname = path. getfilenamewithoutextension (sourcefile );
String prnfile = string. Format ("{0} // {1}. PRN", strdir, strname );
String pdffile = string. Format ("{0} // 1_1_success", strdir, strname );
Object objprnfile = (object) prnfile;
Object background = true;
Object printtofile = true;
Object collate = true;
Object append = false;
Object manualduplexprint = false;
Object copies = false;
Object range = word. wdprintoutrange. wdprintalldocument;
Object missing = system. reflection. Missing. value;
String MSG = string. empty;
String adobeprinter = getadobeprinter ();
If (adobeprinter. Length = 0)
Throw new exception ("did not find Adobe PDF printer or Acrobat Distiller printer, please install the printer .");
Iword word = new word2003factory (). createword ();
// Open the Word Document
Word. Document Doc = word. opendocument (sourcefile );
String oldprint = Doc. application. activeprinter;
// Set Adobe printer as the default printer
Doc. application. activeprinter = adobeprinter;
Datetime start = datetime. now;
Datetime end = start. addseconds (20 );
// Print the Word document to the PRN File
Doc. printout (ref background, ref append, ref range, ref objprnfile, ref missing, ref missing,
Ref missing, ref printtofile, ref collate, ref missing, ref missing,
Ref missing, ref missing, ref missing );
While (! File. exists (prnfile ))
{
If (datetime. Now> end)
{
Throw new exception ("Word document print to PRN document overtime ");
}
Else
{
Application. doevents ();
}
}
Doc. application. activeprinter = oldprint;
Word. Close (false );
Doc = NULL;
WORD = NULL;
// PRN PDF
Process objprocess = new process ();
Objprocess. startinfo. createnowindow = true;
Objprocess. startinfo. useshellexecute = false;
Objprocess. startinfo. filename = getadobedisfilepath ();
Objprocess. startinfo. Arguments = prnfile;
Start = datetime. now;
End = start. addseconds (20 );
Objprocess. Start ();
While (! File. exists (pdffile ))
{
If (datetime. Now> end)
{
Throw new exception ("Word document print to PRN document overtime ");
}
Else
{
Application. doevents ();
}
}
Objprocess. closemainwindow ();
// If (file. exists (prnfile ))
//{
// File. Delete (prnfile );
//}
/*
Required distxlib. Required distiller pdfdis = new required distxlib. Required distiller ();
Pdfdis. filetopdf (prnfile, pdffile, String. Empty );
Start = datetime. now;
End = start. addseconds (20 );
While (! File. exists (pdffile ))
{
If (datetime. Now> end)
{
Throw new exception ("PRN document print to PDF document overtime ");
}
Else
{
Application. doevents ();
}
}
Pdfdis = NULL;
*/
}