Java Word转Pdf

1。我的word转pdf用到了一个虚拟的打印机,安装一个Adobe Acrobat 7.0 Professional就可以了

2。配置虚拟打印机,开始--打印机和传真--添加打印机---一步一步的配置就行了。

3。点击配置的打印机右键首选项----设置----把不要发送字体到打印机取消勾选

4。下载Jacob.jar包。把jacob.jar所对应的Jacob.dll放在windows/sys32下或者jre/bin下

5。代码部分

ExpandedBlockStart.gif代码
package demo;

import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/*
 * 注意word转pdf要安装虚拟打印机,且要配置
 * 使用jacob框架,把dll文件放到jre/bin目录下
 
*/
public class WordToPdf {
    
private ActiveXComponent wordCom = null



    
private Object wordDoc = null



    
private final Variant False = new Variant(false); 



    
private final Variant True = new Variant(true); 



    
/** 

      * 打开word文档 

      *   

      * 
@param filePath word文档

      * 
@return 返回word文档对象 

      
*/ 

    
public boolean openWord(String filePath) { 

        
//建立ActiveX部件 

        wordCom 
= new ActiveXComponent("Word.Application"); 

        

        
try { 

            
//返回wrdCom.Documents的Dispatch 

            Dispatch wrdDocs 
= wordCom.getProperty("Documents").toDispatch(); 

            
//调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc 

            wordDoc 
= Dispatch.invoke(wrdDocs, "Open", Dispatch.Method, 

                    
new Object[] { filePath }, new int[1]).toDispatch(); 

            
return true

        } 
catch (Exception ex) { 

            ex.printStackTrace(); 

        } 

        
return false;

    } 



    
/** 

      * 关闭word文档 

      
*/ 

    
public void closeWord(boolean saveOnExit) {

        
if (wordCom!=null) {

            
//关闭word文件

            
//Dispatch.call(wordDoc, "Close", new Variant(saveOnExit));

            wordCom.invoke(
"Quit",new Variant[]{});

            
//wordCom.invoke("Quit",new Variant[0]);

            wordCom
=null;

            
//释放在程序线程中引用的其它com,比如Adobe PDFDistiller

            ComThread.Release();

        }

    } 



    
/** 

      * 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件

      *   

      * 
@param   sourceFilePath 

      *          源文件路径

      * 
@param   destinPSFilePath 

      *          首先生成的PS文件路径

      * 
@param   destinPDFFilePath 

      *          生成PDF文件路径 

      
*/ 

    
public void docToPDF(String sourceFilePath, String destinPSFilePath, 

                    String destinPDFFilePath) { 

        
if (!openWord(sourceFilePath)) { 

            closeWord(
true); 

            
return

        } 

        
//建立Adobe Distiller的com对象 

        ActiveXComponent distiller 
= new ActiveXComponent("PDFDistiller.PDFDistiller.1");

        
try { 

            
//设置当前使用的打印机,我的Adobe Distiller打印机名字为 "Adobe PDF" 

            wordCom.setProperty(
"ActivePrinter"new Variant("Adobe PDF"));

            
//设置printout的参数,将word文档打印为postscript文档。现在只使用了前5个参数,假如要使用更多的话可以参考MSDN的office开发相关api 

            
//是否在后台运行 

            Variant Background 
= False; 

            
//是否追加打印 

            Variant Append 
= False; 

            
//打印所有文档 

            
int wdPrintAllDocument = 0

            Variant Range 
= new Variant(wdPrintAllDocument); 

            
//输出的postscript文件的路径 

            Variant OutputFileName 
= new Variant(destinPSFilePath); 



            Dispatch.callN((Dispatch) wordDoc, 
"PrintOut"new Variant[] { 

                            Background, Append, Range, OutputFileName }); 

            System.out.println(
"由word文档打印为ps文档成功!"); 

            
//调用Distiller对象的FileToPDF方法所用的参数,具体内容参考Distiller Api手册 

            
//作为输入的ps文档路径 

            Variant inputPostScriptFilePath 
= new Variant(destinPSFilePath); 

            
//作为输出的pdf文档的路径 

            Variant outputPDFFilePath 
= new Variant(destinPDFFilePath); 

            
//定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件 

            Variant PDFOption 
= new Variant(""); 

            
//调用FileToPDF方法将ps文档转换为pdf文档 

            Dispatch.callN(distiller, 
"FileToPDF"new Variant[] { 

                            inputPostScriptFilePath, outputPDFFilePath, PDFOption }); 

            System.out.println(
"由ps文档转换为pdf文档成功!"); 

        } 
catch (Exception ex) { 

            ex.printStackTrace(); 

        } 
finally { 

            closeWord(
true); 

        } 

    } 



    
public static void main(String[] argv) { 

            WordToPdf d2p 
= new WordToPdf();
            d2p.docToPDF(
"D:\\test.doc""D:\\test.ps""D:\\test.pdf");
            
boolean   success   =   (new   File("D:\\test.ps")).delete();
            
if(success){
                System.out.println(
"删除打印机文件成功");
            }
         


    } 
}

 每种功能的实现方法有很多,希望各位可以交流不同的思想和方法。可以加QQ412546724。呵呵

 

转载于:https://www.cnblogs.com/jiayi/archive/2010/06/03/1750461.html

### 使用 Aspose.Words 实现 Java Word PDF Aspose.Words 是一个功能强大的商业库,支持在 Java 应用程序中读取、写入和Word 文档(包括 `.doc` 和 `.docx` 格式)为多种格式,如 PDF、HTML、XPS 等。该库无需依赖 Microsoft Office,适用于跨平台部署[^1]。 #### Maven 依赖配置 在项目中引入以下 Maven 依赖以使用 Aspose.Words: ```xml <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>23.7</version> <classifier>jdk17</classifier> </dependency> ``` > 注意:Aspose 是一个商业库,使用前需申请临时或正式许可证,或在构建时忽略警告以使用试用模式。 #### Word PDFJava 实现 以下是一个完整的 Java 方法示例,用于将 `.docx` 或 `.doc` 文件换为 PDF 格式: ```java import com.aspose.words.Document; import com.aspose.words.SaveFormat; import java.io.File; public class WordToPdfConverter { public static void convertWordToPdf(String inputPath, String outputPath) throws Exception { Document doc = new Document(inputPath); doc.save(outputPath, SaveFormat.PDF); } public static void main(String[] args) { try { convertWordToPdf("example.docx", "output.pdf"); System.out.println("换完成"); } catch (Exception e) { e.printStackTrace(); } } } ``` 该方法支持保留原始 Word 文档中的格式、图像、表格以及字体样式,适用于企业级文档换需求[^1]。 #### 使用 Hutool 辅助文件操作 Hutool 是一个轻量级工具库,可用于简化文件读写、Base64 编码等操作。例如,将 Word 文件读取为 Base64 字符串并换为 PDF: ```java import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.Base64; public class FileConversion { public static void main(String[] args) { String base64String = Base64.encode(FileUtil.readFromStream("example.docx")); byte[] decodedBytes = Base64.decode(base64String); File tempWord = File.createTempFile("temp", ".docx"); FileUtil.writeFromStream(new ByteArrayInputStream(decodedBytes), tempWord); WordToPdfConverter.convertWordToPdf(tempWord.getAbsolutePath(), "output.pdf"); } } ``` 这种方式适用于从网络传输或数据库中读取 Word 文件并换为 PDF 的场景。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值