officetopdf

OfficeToPdf.java



package com.ekingstar.zcgl.bean;


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;


import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


/**
 * @author DXJ


 * @describe 进行PDF文件的各种转换
 * @serialData 2013-10-30 
 */


public class OfficeToPdf {


static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// word转PDF 格式
static final int ppSaveAsPDF = 32;// ppt 转PDF 格式


public static void word2pdf(String inputFile, String pdfFile) {
ActiveXComponent app = null;
Dispatch doc = null;
try {
// 打开word应用程序
app = new ActiveXComponent("Word.Application");
// 设置word不可见
app.setProperty("Visible", false);
// 获得word中所有打开的文档,返回Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
// 调用Document对象的SaveAs方法,将文档保存为pdf格式
/*
* Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF
* //word保存为pdf格式宏,值为17 );
*/
Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF // word保存为pdf格式宏,值为17
);
} catch (Exception e) {
System.out.println("word2pdf出现错误");
} finally {
// 关闭文档
if (doc != null) {
Dispatch.call(doc, "Close", false);
}
// 关闭word应用程序
if (app != null) {
app.invoke("Quit", 0);
app = null;
}


}


}


// 转换文档到PDF
public static void ppt2pdf(String source, String target) {
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Powerpoint.Application"); // 启动PPT
// app.setProperty("Visible", new Variant(false)); PPT转换时不允许隐藏


Dispatch presentations = app.getProperty("Presentations").toDispatch();
Dispatch presentation = Dispatch.call(presentations, //
"Open", source, // FileName
true, // ReadOnly
true, // Untitled 指定文件是否有标题。
false // WithWindow 指定文件是否可见。
).toDispatch();


File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(presentation, //
"SaveAs", //
target, // FileName
ppSaveAsPDF);


Dispatch.call(presentation, "Close");
long end = System.currentTimeMillis();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (app != null)
app.invoke("Quit");
}
}


public static void excel2pdf(String source, String target) {
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch workbook = null;
try {
app = new ActiveXComponent("Excel.Application"); // 启动excel
app.setProperty("Visible", false); // 隐藏转换窗口
Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method,
new Object[] { source, new Variant(false), new Variant(false) }, new int[3]).toDispatch();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method,
new Object[] { target, new Variant(57), new Variant(false), new Variant(57), new Variant(57),
new Variant(false), new Variant(true), new Variant(57), new Variant(true),
new Variant(true), new Variant(true) },
new int[1]);
// long end = System.currentTimeMillis();
} catch (Exception e) {
// e.printStackTrace();
System.out.println("excel2pdf出现错误");
} finally {
if (workbook != null) {
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);
}
if (app != null) {
app.invoke("Quit", new Variant[] {});
app = null;
}
ComThread.Release();
System.gc();


}
}


// pdf转swf,
public static int pdf2swf(String swfToolsPath, String sourceFile, String destFile) {
// 目标路径不存在则建立目标路径
File dest = new File(destFile);
if (!dest.getParentFile().exists())
dest.getParentFile().mkdirs();


// 源文件不存在则返回 -1
File source = new File(sourceFile);
if (!source.exists())
return -1;


Reader reader = null;


// 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
try {
// 调用pdf2swf命令进行转换,windows下的格式是:pdf2swf -i - sourceFilePath.pdf -o
// destFilePath.swf
// LINUX下的格式是:/usr/bin/pdf2swf -o cwh1.swf -f cwh1.pdf -s
// languagedir=/usr/local/share/xpdf/chinese-simplified -T 9 -t -s
// storeallcharacters -s poly2bitmap
// 如果文明名里有空格,命令行里执行时会找不到文件
if (sourceFile.indexOf(" ") >= 0)
sourceFile = "\"" + sourceFile + "\"";
if (destFile.indexOf(" ") >= 0)
destFile = "\"" + destFile + "\"";
String command = swfToolsPath + " -i " + sourceFile + " -o " + destFile + " -T 9 -s languagedir=C:/xpdf/xpdf-chinese-simplified";
//String command = swfToolsPath + " " + sourceFile + " " + destFile + " -s languagedir=C:/xpdf/xpdf-chinese-simplified";
//String command = swfToolsPath + " -o " + destFile + " -t " + sourceFile + " -s languagedir=C:/xpdf/xpdf-chinese-simplified";
//System.out.println(command);
Process pro = Runtime.getRuntime().exec(command);


reader = new InputStreamReader(pro.getInputStream());
BufferedReader bufferedReader = new BufferedReader(reader);
while (bufferedReader.readLine() != null)
;
pro.waitFor();
return pro.exitValue();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


return 1;
}






public static void main(String[] args) {
excel2pdf(("d:" + File.separator+ "1.docx"),("e:" + File.separator+ "xind.docx"));
}


}


### 回答1: .NET 可以使用 Aspose.Words 和 ITextSharp 这两个开源免费库来把复杂的 Word 文件转换为 PDF。 以下是使用 Aspose.Words 转换的示例代码: ``` using Aspose.Words; // 创建一个新的文档 Document doc = new Document(); doc.LoadFromFile(@"D:\input.docx"); // 保存为 PDF 格式 doc.SaveToFile(@"D:\output.pdf", FileFormat.PDF); ``` 以下是使用 ITextSharp 转换的示例代码: ``` using iTextSharp.text.pdf; using iTextSharp.text.pdf.parser; using iTextSharp.text.pdf.parser.clipper; // 创建 PDF 读取器 PdfReader reader = new PdfReader(@"D:\input.pdf"); // 创建 PDF 写入器 PdfStamper stamper = new PdfStamper(reader, new FileStream(@"D:\output.pdf", FileMode.Create)); // 关闭写入器 stamper.Close(); ``` 请注意,Aspose.Words 是一个收费库,但提供了一个永久免费的开发人员版本。 ### 回答2: 在.NET开发中,可以使用多个开源免费库将复杂的Word文件转换为PDF,下面给出几个常用的库和相应的代码示例: 1. Aspose.Words Aspose.Words是一个强大的.NET库,可用于创建、读取和编辑Word文档,并支持将Word文件转换为PDF格式。 示例代码: ``` // 加载Word文件 Document doc = new Document("input.docx"); // 将Word文件保存为PDF doc.Save("output.pdf", SaveFormat.Pdf); ``` 2. iTextSharp iTextSharp是一个开放源代码的.NET PDF库,可以用于创建、读取和编辑PDF文件。它也支持从Word文件转换为PDF。 示例代码: ``` // 创建Word到PDF转换器 WordToPdfConverter converter = new WordToPdfConverter(); // 将Word文件转换为PDF converter.Convert("input.docx", "output.pdf"); ``` 3. Spire.Doc Spire.Doc是一个.NET库,用于生成、读取和编辑Word文档。它也支持将Word文件转换为PDF。 示例代码: ``` // 加载Word文件 Document doc = new Document("input.docx"); // 保存为PDF doc.SaveToFile("output.pdf", FileFormat.PDF); ``` 以上是一些常用的开源免费库和相应的代码示例,它们可以帮助开发人员将复杂的Word文件转换为PDF格式。 ### 回答3: 在.NET平台上,可以使用一些开源免费的库来将复杂的Word文件转换为PDF。下面是几个常用的库以及代码示例: 1. DocX库:DocX是一个用于处理Microsoft Word文档的.NET库,它提供了将Word文件转换为PDF的功能。以下是使用DocX库实现转换的示例代码: ```csharp using System; using System.IO; using Xceed.Words.NET; class Program { static void Main(string[] args) { string inputPath = "input.docx"; string outputPath = "output.pdf"; // 打开Word文档 using (DocX document = DocX.Load(inputPath)) { // 将Word文档保存为PDF document.SaveAs(outputPath); } } } ``` 2. OfficeToPDF库:OfficeToPDF是一个开源的命令行工具,它可以通过调用Microsoft Office来将各种Office格式文件转换为PDF。在.NET中,可以使用Process类来调用该工具进行转换。以下是示例代码: ```csharp using System; using System.Diagnostics; class Program { static void Main(string[] args) { string inputPath = "input.docx"; string outputPath = "output.pdf"; string officeToPdfToolPath = "OfficeToPDF.exe"; // 调用OfficeToPDF工具进行转换 Process process = new Process(); process.StartInfo.FileName = officeToPdfToolPath; process.StartInfo.Arguments = $"\"{inputPath}\" \"{outputPath}\""; process.Start(); process.WaitForExit(); } } ``` 以上就是.NET平台上使用开源免费库将复杂的Word文件转换为PDF的几个示例。你可以根据具体的需要选择合适的库进行使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值