java實(shí)現(xiàn)PPT轉(zhuǎn)化為PDF
JACOB的方法,足可以解決這個(gè)問(wèn)題,但是我既然以前曾經(jīng)做過(guò)報(bào)表,就想嘗試不同的方法。
JACOB是一座連接JAVA和微軟的橋,所有的解析由微軟解析。POI是沒(méi)有微軟解析的那么原汁原味的,所以如果要求高的話,還是使用JACOB。
大致思路很簡(jiǎn)單,將PPT先轉(zhuǎn)化為圖片,然后將圖片寫入PDF。轉(zhuǎn)化圖片是用POI,操作PDF使用ITEX。不過(guò)這個(gè)方法的BUG就是轉(zhuǎn)化圖片的POI效果不是很好。
導(dǎo)入的包分別是:itextpdf-5.1.3.jar,poi-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar。
然后貼代碼了:
代碼沒(méi)有進(jìn)行參數(shù)統(tǒng)一,寫兩個(gè)方法:
package com.zzk.cn;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.record.Slide;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
public class PPTtoImage {
public static void main(String[] args) {
// 讀入PPT文件
File file = new File("D:/書本JVM總結(jié)7-9.ppt");
doPPTtoImage(file);
}
public static boolean doPPTtoImage(File file) {
boolean isppt = checkFile(file);
if (!isppt) {
System.out.println("你指定的文件不是ppt文檔!");
return false;
}
try {
FileInputStream is = new FileInputStream(file);
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
System.out.print("第" + i + "頁(yè)。");
if (slide[i].getNotesSheet() != null
&& slide[i].getNotesSheet().getTextRuns() != null) {
// 獲取第一個(gè)備注
System.out.println("備注:"
+ slide[i].getNotesSheet().getTextRuns()[0]
.getText());
}
TextRun[] truns = slide[i].getTextRuns();
for (int k = 0; k < truns.length; k++) {
RichTextRun[] rtruns = truns[k].getRichTextRuns();
for (int l = 0; l < rtruns.length; l++) {
rtruns[l].setFontIndex(1);
rtruns[l].setFontName("宋體");
// 獲取文本列表
System.out.println(rtruns[l].getText());
}
}
BufferedImage img = new BufferedImage(pgsize.width,
pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
pgsize.height));
slide[i].draw(graphics);
// 這里設(shè)置圖片的存放路徑和圖片的格式(jpeg,png,bmp等等),注意生成文件路徑
FileOutputStream out = new FileOutputStream("D:/testImage/pict_"
+ (i + 1) + ".jpeg");
javax.imageio.ImageIO.write(img, "jpeg", out);
out.close();
}
System.out.println("ok");
return true;
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
// function 檢查文件是否為PPT
public static boolean checkFile(File file) {
boolean isppt = false;
String filename = file.getName();
String suffixname = null;
if (filename != null && filename.indexOf(".") != -1) {
suffixname = filename.substring(filename.indexOf("."));
if (suffixname.equals(".ppt")) {
isppt = true;
}
return isppt;
} else {
return isppt;
}
}
}
第二段代碼:
package com.zzk.cn;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
public class ImagetoPDF {
public static void main(String[] args) {
System.out.println("Chapter 6 example 3: using a relative path for HTML");
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document, new FileOutputStream("D:/測(cè)試圖片.pdf"));
// HtmlWriter writer = HtmlWriter.getInstance(document, new FileOutputStream("Chap0603.html"));
// writer.setImagepath("../../images/kerstmis/");
// step 3: we open the document
document.open();
for(int i=1;i<=7;i++) {
// step 4: we add content
Image jpg = Image.getInstance("D:/testImage/pict_"+i+".jpeg");
jpg.scalePercent(50);
document.add(jpg);
}
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在idea中將java項(xiàng)目中的單個(gè)類打包成jar包操作
這篇文章主要介紹了在idea中將java項(xiàng)目中的單個(gè)類打包成jar包操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
SpringBoot整合Spring Security的詳細(xì)教程
這篇文章主要介紹了SpringBoot整合Spring Security的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
簡(jiǎn)單易懂的Java Map數(shù)據(jù)添加指南
Java提供了多種方法來(lái)往Map中添加數(shù)據(jù),開發(fā)者可以根據(jù)具體需求選擇合適的方法,需要的朋友可以參考下2023-11-11
Java中LocalDate日期格式轉(zhuǎn)換(使用系統(tǒng)時(shí)區(qū))
本文主要介紹了Java中LocalDate日期格式轉(zhuǎn)換(使用系統(tǒng)時(shí)區(qū)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2007-02-02
SpringBoot使用@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)的并行執(zhí)行
在SpringBoot中,如果使用@Scheduled注解來(lái)定義多個(gè)定時(shí)任務(wù),默認(rèn)情況下這些任務(wù)將會(huì)被安排在一個(gè)單線程的調(diào)度器中執(zhí)行,這意味著,這些任務(wù)將會(huì)串行執(zhí)行,而不是并行執(zhí)行,本文介紹了SpringBoot使用@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)的并行執(zhí)行,需要的朋友可以參考下2024-06-06
java實(shí)現(xiàn)socket從服務(wù)器連續(xù)獲取消息的示例
這篇文章主要介紹了java實(shí)現(xiàn)socket從服務(wù)器連續(xù)獲取消息的示例,需要的朋友可以參考下2014-04-04
Maven打包報(bào)錯(cuò):[WARNING] The POM for xxx 
本文主要介紹了Maven打包報(bào)錯(cuò):[WARNING] The POM for xxx is missing, no dependency inform,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
IntelliJ IDEA(或者JetBrains PyCharm)中彈出"IntelliJ IDEA License
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA(或者JetBrains PyCharm)中彈出"IntelliJ IDEA License Activation"的解決辦法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10

