Link: http://blog.csdn.net/gavin_sw/archive/2007/04/11/1561254.aspx
For details, refer:
Http://www.matrix.org.cn/thread.shtml;jsessionid=B1E4B57897D51B59802D353CB6B32ACC? Topicid = 29594 & forumid = 17
Preparations:
1. Install "Adobe Acrobat 7.0 professional" and upgrade it to "7.0.5"
2. Install "gs811w32.rar" (by default, it is the script required for PDF conversion)
3. Install "postscript.rar" (it can be installed by default. It is actually a PDF virtual printer driver)
4. Virtual printer configuration, see http://www.matrix.org.cn/thread.shtml;jsessionid=B1E4B57897D51B59802D353CB6B32ACC? Topicid = 29594 & forumid = 17
Pay attention to the following: wordcom. setproperty ("activeprinter", new variant ("MS Publisher color printer "));
In this line of code, "MS Publisher color printer" corresponds to the name of the virtual printer installed. Use the following code to test.
/***//**
* @ Author Xuming Li
*
* @ Version 1.00
*
*/
Public class d2p ...{
Private activexcomponent wordcom = NULL;
Private object worddoc = NULL;
Private Final variant false = new variant (false );
Private Final variant true = new variant (true );
/***//**
* Open a Word document
*
* @ Param filepath
* Word documents
* @ Return returns the Word Document Object.
*/
Public Boolean openword (string filepath )...{
// Create ActiveX parts
Wordcom = new activexcomponent ("word. application ");
Try ...{
// Return the dispatch of wrdcom. Documents.
Dispatch wrddocs = wordcom. getproperty ("events"). todispatch ();
// Call wrdcom. Documents. Open to open the specified Word document and return worddoc
Worddoc = Dispatch. Invoke (wrddocs, "open", dispatch. method,
New object []... {filepath}, new int [1]). todispatch ();
Return true;
} Catch (exception ex )...{
Ex. printstacktrace ();
}
Return false;
}
/***//**
* Close Word documents
*/
Public void closeword ()...{
// Close the Word file
Wordcom. Invoke ("quit", new variant []... {});
}
/***//**
** After printing a Word document as a PS file, use distiller to convert the PS file to a PDF file *
*
* @ Param sourcefilepath
* Source file path *
* @ Param destinpsfilepath
* First generated PS file path *
* @ Param destinpdffilepath
* Generate a PDF file path
*/
Public void doctopdf (string sourcefilepath, string destinpsfilepath,
String destinpdffilepath )...{
If (! Openword (sourcefilepath ))...{
Closeword ();
Return;
}
// Create a COM Object for Adobe Distiller
Activexcomponent distiller = new activexcomponent (
"Includistiller. includistiller.1 ");
Try ...{
// Set the current printer. The name of my Adobe distiller printer is "Adobe PDF"
Wordcom. setproperty ("activeprinter", new variant ("MS Publisher color printer "));
// Set the printout parameter to print the Word document as a postscript document. Currently, only the first five parameters are used. For more information, see the office development APIs of msdn.
// Whether to run in the background
Variant background = false;
// Whether to append the print
Variant append = false;
// Print all documents
Int wdprintalldocument = 0;
Variant range = new variant (wdprintalldocument );
// Path of the output postscript File
Variant outputfilename = new variant (destinpsfilepath );
Dispatch. calln (dispatch) worddoc, "printout", new variant []... {
Background, append, range, outputfilename });
System. Out. println ("printed from Word documents to PS documents! ");
// Parameters used to call the filetopdf method of the distiller object. For details, refer to the distiller API manual.
// Enter the PS document path
Variant inputpostscriptfilepath = new variant (destinpsfilepath );
// Path of the output PDF document
Variant outputpdffilepath = new variant (destinpdffilepath );
// Define the filetopdf method to use Adobe PDF to set the file path. Here, no value is assigned to indicate that the PDF configuration file is not used.
Variant upload option = new variant ("");
// Call the filetopdf method to convert the PS document to a PDF document
Dispatch. calln (distiller, "filetopdf", new variant []... {
Inputpostscriptfilepath, outputpdffilepath, using option });
System. Out. println ("converted from PS to PDF! ");
} Catch (exception ex )...{
Ex. printstacktrace ();