Convert Word to PDF using Jacob

Source: Internet
Author: User
Tags api manual adobe distiller

Document introduction to this blog Jacob
Java operations on Microsoft Word jacob
Getting started with jacob and troubleshooting
Convert Word to Html or txt using Jacob
Convert Word to PDF using Jacob
Java calls word through jacob (dynamic content is generated based on the Word template)

 

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.

 

1. software to be used:
(1) Adobe Acrobat 8 Professional (Minimum Version 7.03)
(Personal non-commercial use) 8.0 cracked version: http://green.crsky.com/soft/2205.html (remember to download the patch)
Installation http://down1.greendown.cn//200611/AcroPro80_efg.rar
Cracking http://soft.greendown.cn//200611/AcroPro80_Crack.rar
(2)gs811w32.rar (script ps required for PDF conversion)
Http://www.allmail.com.cn/gs811w32.rar
(3366postscript.rar (driver of PDF virtual printer)
Http://www.pdfhome.com.cn/Resource/DownLoad/postscript.rar
(4) jacob. jar
Jacb_1.9.zip
(5) office 2003
 
2 principle:
Jacob. jar
Doc --> ps --> pdf --> office 2003 --> gs811w32 --> Adobe Acrobat 8 --> postscript --> printer
(For the installation of jacob And jar, see: getting started with jacob and troubleshooting)

3 install and run:
(1) install Adobe Acrobat 8 Professional

(2) install gs811w32.rar

(3) configure the printer (no real printer is required here)
Control Panel printer and other hardware printer and fax Add Printer
(If "operation cannot be completed" is displayed during adding. The print background program service is not running ."
Open Control Panel performance and Maintenance Management Tools service and find "Print Spooler"
Right-click Properties to start) Select a local printer (if there is no printer, check and install
Remove the plug-in Printer hook), next, select "Use the following port"
(My Document/*. pdf (Adobe PDF Port), next select printer, I choose
Apple's Color LaserWriter 12/600 (after work, you must buy an Apple hp if you have money)
Next step (remember the printer name: Apple color laserwriter 12/600)
Next step (if you do not have a printer, select: Do not test)

(4) install postscript.rar (during installation, pay attention to each step and select the options related to the previous settings)

(5) set Adobe Acrobat 8 Professional: select a PDF file, right-click the open mode, and select
Use open Adobe Acrobat 8 Professional to select File menu print setup... print Option
Attribute "Apple color laserwriter 12/600" OK

(6) run the following code:

 /** <Br/> * @ author XuMing Li <br/> * @ version 1.00, 2007-4-9 <br/> */<br/> import com. jacob. activeX. activeXComponent; <br/> import com.jacb.com. comThread; <br/> import com.jacb.com. dispatch; <br/> import com.jacb.com. variant; </p> <p> public class D2P {<br/> private ActiveXComponent wordCom = null; </p> <p> private Object wordDoc = null; </p> <p> private final Variant False = new Variant (fa Lse); </p> <p> private final Variant True = new Variant (true ); </p> <p>/** <br/> * Open the Word document <br/> * @ param filePath <br/> * Word document <br/> * @ return object <br/> */<br/> public boolean openWord (String filePath) {<br/> // create ActiveX part <br/> wordCom = new ActiveXComponent ("Word. application "); <br/> try {<br/> // return wrdCom. dispatch <br/> Dispatch wrdDocs = wordCom. getProperty ("events "). toDispatc H (); <br/> // call wrdCom. documents. open the specified Word file and return wordDoc <br/> wordDoc = Dispatch. invoke (wrdDocs, "Open", Dispatch. method, new Object [] {filePath}, new int [1]). toDispatch (); <br/> return true; <br/>} catch (Exception ex) {<br/> ex. printStackTrace (); <br/>}< br/> return false; <br/>}</p> <p>/** <br/> * close a Word document <br/> */<br/> public void closeWord () {<br/> // close the word file <br/> wordCom. invoke ("Quit", ne W Variant [] {}); <br/>}</p> <p>/** <br/> ** after printing a Word document as a PS file, use Distiller to convert a PS file to a PDF file * <br/> * @ param sourceFilePath <br/> * source file path * <br/> * @ param destinPSFilePath <br /> * first generated PS file path * <br/> * @ param destinPDFFilePath <br/> * generated PDF file path <br/> */<br/> public void docToPDF (string sourceFilePath, string destinPSFilePath, <br/> String destinPDFFilePath) {<br/> if (! OpenWord (sourceFilePath) {<br/> closeWord (); <br/> return; <br/>}< br/> // create a com Object for Adobe Distiller <br/> ActiveXComponent distiller = new ActiveXComponent ("nvidistiller. PDFDistiller.1 "); <br/> try {<br/> // set the current printer. The name of my Adobe Distiller printer is" Adobe PDF "<br/> wordCom. setProperty ("ActivePrinter", new Variant ("MS Publisher Color Printer"); <br/> // sets the printout parameter to print the Word document as a postscript document. Currently, only the first five parameters are used, if you want to use more functions, refer to the office development api of MSDN <br/> // whether to run in the Background <br/> Variant Background = False; <br/> // determine whether to Append the file. <br/> Variant Append = False; <br/> // print all documents. <br/> int wdPrintAllDocument = 0; <br/> Variant Range = new Variant (wdPrintAllDocument); <br/> // path of the output postscript file <br/> Variant OutputFileName = new Variant (destinPSFilePath ); </p> <p> Dispatch. callN (Dispatch) wordDoc, "PrintOut", new Variant [] {Backgr Ound, Append, Range, OutputFileName}); <br/> System. out. println ("printed as a ps document by a Word document! "); <Br/> // parameters used to call the FileToPDF method of the Distiller object, for more information, see the input ps documentation path in the Distiller Api manual <br/> Variant inputPostScriptFilePath = new Variant (destinPSFilePath ); <br/> // path of the output pdf document <br/> Variant outputPDFFilePath = new Variant (destinPDFFilePath ); <br/> // define the FileToPDF method. Use adobe pdf to set the file path, no value is assigned here, indicating that the pdf configuration file is not used <br/> Variant variable option = new Variant (""); <br/> // call the FileToPDF method to convert the ps document to a pdf document <br/> Dispatch. callN (distiller, "FileToPDF", new Variant [] {inputPostScriptFilePath, outputPDFFilePath, using option}); <br/> System. out. println ("converted from ps to pdf successfully! "); <Br/>}catch (Exception ex) {<br/> ex. printStackTrace (); <br/>}finally {<br/> closeWord (); <br/> wordCom = null; <br/> // release other com referenced in the program thread, such as Adobe nvidistiller <br/> ComThread. release (); <br/>}</p> <p> public static void main (String [] argv) {<br/> D2P d2p = new D2P (); <br/> d2p.doc ToPDF ("d:/12.doc"," d:/1p. ps "," d:/1p.pdf "); <br/> // you have created a Word document named 12.doc, the generated document will be under drive d <br/> // 1p.psand 1p.pdf (this is what we want) <br/>}< br/>}

 

Blog Source: http://blog.csdn.net/gavin_sw/archive/2007/04/11/1561254.aspx

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.