java实现word,ppt,excel,jpg转pdf

 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;
	 }
	 }
}


  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
实现PPTPDF,可以使用Apache POI和Apache PDFBox两个Java库。具体步骤如下: 1. 使用Apache POI读取PPT文件,获取每一页的内容和样式信息。 2. 创建一个PDF文档对象,使用Apache PDFBox。 3. 将每一页的内容和样式信息写入PDF文档对象。 4. 保存PDF文档对象到本地文件。 以下是一个简单的PPTPDFJava代码示例: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.tools.imageio.ImageIOUtil; public class PptToPdfConverter { public static void convert(String pptFilePath, String pdfFilePath) throws IOException { // 读取PPT文件 HSLFSlideShow ppt = new HSLFSlideShow(new FileInputStream(pptFilePath)); // 创建PDF文档对象 PDDocument pdf = new PDDocument(); // 遍历PPT每一页,将内容写入PDF文档对象 for (int i = 0; i < ppt.getSlides().size(); i++) { PDPage page = new PDPage(); pdf.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(pdf, page); contentStream.drawImage(new PDFRenderer(ppt.getSlides().get(i)).renderImageWithDPI(300), 0, 0); contentStream.close(); } // 保存PDF文档对象 pdf.save(pdfFilePath); pdf.close(); // 释放PPT文件资源 ppt.close(); } public static void main(String[] args) throws IOException { String pptFilePath = "test.ppt"; String pdfFilePath = "test.pdf"; convert(pptFilePath, pdfFilePath); } } ``` 该代码使用了Apache POI的HSLFSlideShow类读取PPT文件,使用了Apache PDFBox的PDDocument类创建PDF文档对象和PDPage类创建PDF页面对象,使用了PDFRenderer类将PPT页面转换PDF页面,使用了PDPageContentStream类将PDF页面写入PDF文档对象

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值