能安装第三方软件的话,可以考虑以使用Spire.Doc for Java:
你可以在Java程序中添加 Spire.Doc.jar 文件作为依赖项。可以从这个链接下载 JAR 文件;如果使用Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository></repositories><dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.3</version>
</dependency></dependencies>
将Word转换成PDF:
Spire.Doc for Java拥有强大的文件转换功能,其提供了 Document. saveToFile(String fileName, FileFormat fileFormat) 方法可将 Word 文档转为多种格式的目标文件,下面是转为 PDF 的方法步骤:
实例化Document类的对象。
调用Document.loadFromFile()方法加载 Word 文档。
调用Document.saveToFile()方法将 Word 保存为 PDF 格式,并指定保存路径。
Java代码如下:
import com.spire.doc.*;public class WordToPDF{
public static void main(String[] args) {
//实例化Document类的对象
Document doc = new Document();
//加载Word
doc.loadFromFile("测试.docx");
//保存为PDF格式
doc.saveToFile("WordToPDF.pdf",FileFormat.PDF);
}
}
希望对您有帮助。
2. 使用java 将word 转为 PDF时, 取消 自动识别标签并转化为“带有标签的PDF”
创建Pdf时,其内容未加标签。仅当Pdf是加标签的Pdf时,方可将注释导出到Word文档。
在“导航栏”右键选择“添加/编辑工具集”,中选择添加“辅助工具”里面的“添加标签到文档”
就会在“导航栏”中出现下面用红色圈的“按钮”:
点击这个以后,Acrobat就会自动识别标签并转化为“带有标签的PDF”,就不会出现你“显示的这个错误了”。
3. java word转pdf
可以试试spire.doc for java转换Word到PDF
4. 在linux环境下,java怎么实现从word格式转换为pdf格式
WORD转换到PDF
WORD转换到PDF的步骤就相对简单了,我们只需要安装一款虚拟打印机软件“Virtual Pdf Printer”即可,它可以将你编辑好的WORD文档直接输出为PDF文件格式。(下载地址: http://www.newhua.com/soft/21190.htm)
安装后在WORD中点击“打开”→“打印”,在“打印机名称”中会看到多了一个“Virtual Printer”选项,选中它,点击确定后(未注册版本有10秒的等待时间),弹出“保存PDF文件”对话框,先选择文件的保存路径,再点击“生成设置”按钮,弹出“系统设置”面板(如图3),对输出后的PDF文件进行加密和字体等设置,点击两次确定,当前的WORD文档就被转换成PDF格式的文件了
5. 在linux环境下,java怎么实现从word格式转换为pdf格式
Word转换成pdf格式是比较简单的,我们使用pdf转换器就可以轻松将word转换成pdf格式
参考软件:迅捷pdf转换器
参考步骤:第一步:打开桌面上的迅捷pdf转换器,然后选择“其他文件转换成PDF”,再选择“word转成pdf”
第三步:转换好之后选择“输出”,把转换好的文件输出保存下来就可以了
6. 在linux环境下,java怎么实现从word格式转换为pdf格式
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* @author XuMing Li
*
* @version 1.00, 2007-4-9
*
*/
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);
/**
* 打开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() {
//关闭word文件
wordCom.invoke( "Quit ", new Variant[] {});
}
/**
* * 将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();
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();
}
}
public static void main(String[] argv) {
D2P d2p = new D2P();
// d2p.openWord( "c:/12.doc ");
// d2p.callWordMacro( "c:/12.docc ", "MyWordMacro ",
// new String[] { "这是调用word宏的测试程序 " });
d2p.docToPDF( "d:/12.doc ", "c:/1p.ps ", "c:/1p.pdf ");
}
}
7. Java如何将Word文件转成PDF文件
用Java内嵌iText生成PDF文档需要5个步骤:
①建立com.lowagie.text.Document对象的实例。
Document document = new Document();
②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));
③打开文档。
document.open();
④向文档中添加内容。
document.add(new Paragraph("Hello World"));
⑤关闭文档。
document.close();
通过上面的5个步骤,就能产生一个Helloworld.PDF的文件,文件内容为"Hello World"。