word,excel,jpeg 转 pdf
首先下载相关jar包:http://download.csdn.net/detail/xu281828044/6922499
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.jacob.activeX.ActiveXComponent;
- import com.jacob.com.Dispatch;
- import com.jacob.com.Variant;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Image;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.pdf.PdfWriter;
- public class Word2Pdf {
- static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
- static final int wdFormatPDF = 17;// word转PDF 格式
- static final int ppSaveAsPDF = 32;// ppt 转PDF 格式
- public static void main(String[] args) throws IOException {
- String source1 = "e:\\test.doc";
- String source2 = "e:\\asd.xlsx";
- String source3 = "e:\\aa.ppt";
- String target1 = "e:\\test1.pdf";
- String target2 = "e:\\test2.pdf";
- String target3 = "e:\\test3.pdf";
- Word2Pdf pdf = new Word2Pdf();
- // pdf.word2pdf(source1, target1);
- // pdf.excel2pdf(source2, target2);
- // pdf.ppt2pdf(source3, target3);
- // pdf.imgToPdf("e:/12345.jpg","e:/xu.pdf");
- }
- public void word2pdf(String source,String target){
- System.out.println("启动Word");
- long start = System.currentTimeMillis();
- ActiveXComponent app = null;
- try {
- app = new ActiveXComponent("Word.Application");
- app.setProperty("Visible", false);
- Dispatch docs = app.getProperty("Documents").toDispatch();
- System.out.println("打开文档" + source);
- Dispatch doc = Dispatch.call(docs,//
- "Open", //
- source,// FileName
- false,// ConfirmConversions
- true // ReadOnly
- ).toDispatch();
- System.out.println("转换文档到PDF " + target);
- File tofile = new File(target);
- if (tofile.exists()) {
- tofile.delete();
- }
- Dispatch.call(doc,//
- "SaveAs", //
- target, // FileName
- wdFormatPDF);
- Dispatch.call(doc, "Close", false);
- long end = System.currentTimeMillis();
- System.out.println("转换完成..用时:" + (end - start) + "ms.");
- } catch (Exception e) {
- System.out.println("========Error:文档转换失败:" + e.getMessage());
- } finally {
- if (app != null)
- app.invoke("Quit", wdDoNotSaveChanges);
- }
- }
- public void ppt2pdf(String source,String target){
- System.out.println("启动PPT");
- long start = System.currentTimeMillis();
- ActiveXComponent app = null;
- try {
- app = new ActiveXComponent("Powerpoint.Application");
- Dispatch presentations = app.getProperty("Presentations").toDispatch();
- System.out.println("打开文档" + source);
- Dispatch presentation = Dispatch.call(presentations,//
- "Open",
- source,// FileName
- true,// ReadOnly
- true,// Untitled 指定文件是否有标题。
- false // WithWindow 指定文件是否可见。
- ).toDispatch();
- System.out.println("转换文档到PDF " + target);
- 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();
- System.out.println("转换完成..用时:" + (end - start) + "ms.");
- } catch (Exception e) {
- System.out.println("========Error:文档转换失败:" + e.getMessage());
- } finally {
- if (app != null) app.invoke("Quit");
- }
- }
- public void excel2pdf(String source, String target) {
- System.out.println("启动Excel");
- long start = System.currentTimeMillis();
- ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel(Excel.Application)
- try {
- app.setProperty("Visible", false);
- Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
- System.out.println("打开文档" + source);
- Dispatch 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]);
- Variant f = new Variant(false);
- System.out.println("转换文档到PDF " + target);
- Dispatch.call(workbook, "Close", f);
- long end = System.currentTimeMillis();
- System.out.println("转换完成..用时:" + (end - start) + "ms.");
- } catch (Exception e) {
- System.out.println("========Error:文档转换失败:" + e.getMessage());
- }finally {
- if (app != null){
- app.invoke("Quit", new Variant[] {});
- }
- }
- }
- public boolean imgToPdf(String imgFilePath, String pdfFilePath)throws IOException {
- File file=new File(imgFilePath);
- if(file.exists()){
- Document document = new Document();
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(pdfFilePath);
- PdfWriter.getInstance(document, fos);
- // 添加PDF文档的某些信息,比如作者,主题等等
- document.addAuthor("arui");
- document.addSubject("test pdf.");
- // 设置文档的大小
- document.setPageSize(PageSize.A4);
- // 打开文档
- document.open();
- // 写入一段文字
- //document.add(new Paragraph("JUST TEST ..."));
- // 读取一个图片
- Image image = Image.getInstance(imgFilePath);
- float imageHeight=image.getScaledHeight();
- float imageWidth=image.getScaledWidth();
- int i=0;
- while(imageHeight>500||imageWidth>500){
- image.scalePercent(100-i);
- i++;
- imageHeight=image.getScaledHeight();
- imageWidth=image.getScaledWidth();
- System.out.println("imageHeight->"+imageHeight);
- System.out.println("imageWidth->"+imageWidth);
- }
- image.setAlignment(Image.ALIGN_CENTER);
- // //设置图片的绝对位置
- // image.setAbsolutePosition(0, 0);
- // image.scaleAbsolute(500, 400);
- // 插入一个图片
- document.add(image);
- } catch (DocumentException de) {
- System.out.println(de.getMessage());
- } catch (IOException ioe) {
- System.out.println(ioe.getMessage());
- }
- document.close();
- fos.flush();
- fos.close();
- return true;
- }else{
- return false;
- }
- }
- }
相关推荐
Entry.java这个类的原理是先通过OfficeToPDF.java把文档转成pdf,然后再通过Pdf2Jpg.java这个类把PDF转成JPG或者PNG,想转成什么格式图片在Pdf2Jpg.java这个类当中设置一下,代码经过测试,完全没有问题,如果碰到...
Java通过OpenOffice将word、ppt、excel、图片、txt文档转换成PDF-附件资源
SearchWord 包含对Word和EXCEL、PPT文件的处理 VApplet和vid2jpg JMF示例 pageSim 计算网页相似度 SST 计算网站风格树 cobra 基于视觉的网页分块算法 HTIS HTIS算法 PageRank PageRank算法 Link 链接 WebGraph Web图...
直接点击使用,不用破解,不用付费,免费pdf转word,jpg,ppt,excel,省去vip会员费,资源共享,欢迎大家下载使用,希望为您省去大量的费用,时间
支持word excel ppt,pdf等办公文档 支持txt,java,php,py,md,js,css等所有纯文本 支持zip,rar,jar,tar,gzip等压缩包 支持jpg,jpeg,png,gif等图片预览(翻转,缩放,镜像) 支持mp3,mp4,flv等多媒体文件预览 ...
打印实用程序java print util,使用的jacob和pdfbox java调用打印机打印的工具类,用到了jacob和pdfbox您可以使用打印机简化png,jpg,pdf,word,excel,ppt的打印。 如果该实用程序广受欢迎,我将在近期将中文注释...
支持word excel ppt,pdf等办公文档 支持txt,java,php,py,md,js,css等所有纯文本 支持zip,rar,jar,tar,gzip等压缩包 支持jpg,jpeg,png,gif等图片预览(翻转,缩放,镜像) 支持mp3,mp4,flv等多媒体文件预览...
对标的补充产品有【】【 】【 】等,在取得公司高层同意后以Apache协议开源出来反哺社区,在此特别感谢@唐该项目使用流行的弹簧靴子构造,易上手和部署,基本支持主流办公文档的在线预览,如doc,docx,Excel,pdf,...
伟创万能阅读器是一款体积小功能强大的阅读工具,支持PDF、word,excel、ppt,chm,txt,java,css等几百种文本格式快速打开,并且还可以打开jpg,jpeg,png,psd等多种图片格式,可以解决
此外,还可支持附件按需压缩下载、多账户使用和邮件过滤功能,并且支持阅读的附件种类繁多,包括Word、Excel、PPT、PDF、JPG、GIF、HTML、PNG、BMP、TXT、ZIP等多种格式。其中“手机邮箱”还可支持LotusNotes、...
网管教程 从入门到精通软件篇 ★一。★详细的xp修复控制台命令和用法!!! 放入xp(2000)的光盘,安装时候选R...JPE,JPEG,JPG:JPEG图形文件 JS:javascript源文件 JSP:HTML网页,其中包含有对一个Java servlet...
".jpg"="application/x-jpg" ".js"="application/x-javascript" ".jsp"="text/html" ".la1"="audio/x-liquid-file" ".lar"="application/x-laplayer-reg" ".latex"="application/x-latex" ".lavs"="audio/x-liquid-...