Commit 3252c463 authored by 夏彬's avatar 夏彬

修改程序

parent de26c439
......@@ -2,6 +2,8 @@ package cc.pp;
import com.aspose.slides.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
......@@ -60,6 +62,25 @@ public class PptToImages {
return result;
}
/**
* @param error
*/
static void usage(String error) {
String msg =
"Usage: PptToImages [options] <ppt or pptx file>\n" +
(error == null ? "" : ("Error: " + error + "\n")) +
"Options:\n" +
" -format <type> png,gif,jpg\n" +
" -scale <float> scale factor\n" +
" -outdir <dir> output directory, defaults to origin of the ppt/pptx file\n" +
" -type <type> image,html format for output\n" +
" -quiet do not write to console (for normal processing)\n";
System.out.println(msg);
// no System.exit here, as we also run in junit tests!
}
/**
* @param args
*/
......@@ -69,45 +90,105 @@ public class PptToImages {
return;
}
// 读入PPT文件
if (args.length != 1) {
System.out.println("Usage:");
System.out.println(" command file");
if (args.length == 0) {
usage(null);
return;
}
String type = "image";
File file = null;
String format = "jpg";
String outDictory = null;
boolean quiet = false;
Float scale = 1f;
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
if ("-scale".equals(args[i])) {
scale = Float.parseFloat(args[++i]);
} else if ("-format".equals(args[i])) {
format = args[++i];
} else if ("-outdir".equals(args[i])) {
outDictory = args[++i];
} else if ("-type".equals(args[i])) {
type = args[++i];
} else if ("-quiet".equals(args[i])) {
quiet = true;
}
} else {
file = new File(args[i]);
}
}
if (file == null || !file.exists()) {
usage("File not specified or it doesn't exist");
return;
}
System.out.println("文件名:" + args[0]);
File file = new File(args[0]);
if (!file.exists()) {
System.out.println("file not found");
if (!format.matches("^(png|gif|jpg)$")) {
usage("Invalid format given");
return;
}
String fileDictory = file.getParent();
String filename = file.getName();
String outDictory = fileDictory + '/' + filename.substring(0, filename.lastIndexOf("."));
File outFile = new File(outDictory);
if (!outFile.exists()) {
outFile.mkdirs();
if (!type.matches("^(image|html)$")) {
usage("Invalid type format given");
return;
}
if (scale < 0) {
usage("Invalid scale given");
return;
}
if (outDictory == null) {
String fileDictory = file.getParent();
String filename = file.getName();
outDictory = fileDictory + '/' + filename.substring(0, filename.lastIndexOf("."));
}
File outD = new File(outDictory);
if (!outD.exists()) {
outD.mkdirs();
} else {
File[] files = outFile.listFiles();//声明目录下所有的文件 files[];
File[] files = outD.listFiles();//声明目录下所有的文件 files[];
for (int i = 0; i < files.length; i++) {//遍历目录下所有的文件
files[i].delete();//把每个文件用这个方法进行迭代
}
}
System.out.println("开始处理");
if (!quiet) {
System.out.println("Processing " + file);
}
long startTime = System.currentTimeMillis(); //获取开始时间
doPPTtOrPPTXoImage(file, outDictory);
if (type.equals("image")) {
if (!quiet) {
System.out.println("image");
}
doPPTtOrPPTXoImage(file, outDictory, format, scale, quiet);
} else {
if (!quiet) {
System.out.println("html");
}
doPPTtOrPPTXoHtml(file, outDictory, quiet);
}
long endTime = System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间: " + (endTime - startTime) + "ms");
System.out.println("处理结束");
}
public static void doPPTtOrPPTXoImage(File file, String outPut) {
public static void doPPTtOrPPTXoHtml(File file, String outPut, Boolean quiet) {
String htmlDir = outPut;
Presentation pres = new Presentation(file.getAbsolutePath());
try {
System.out.println("开始转换" + file.getName() + " to " + htmlDir);
if (!quiet) {
System.out.println("开始转换" + file.getName() + " to " + htmlDir);
}
// Setting HTML Optionsls
HtmlOptions opts = new HtmlOptions();
opts.setHtmlFormatter(HtmlFormatter.createCustomFormatter(new CustomFormattingController()));
......@@ -116,7 +197,10 @@ public class PptToImages {
int total = pres.getSlides().size();
for (int i = 0; i < total; i++) {
System.out.println("共" + total + "页, 第" + (i + 1) + "页。");
if (!quiet) {
System.out.println("共" + total + "页, 第" + (i + 1) + "页。");
}
pres.save(htmlDir + "/slide_" + (i + 1) + ".html", new int[]{i + 1}, SaveFormat.Html, opts);
}
......@@ -127,4 +211,34 @@ public class PptToImages {
pres.dispose();
}
}
public static void doPPTtOrPPTXoImage(File file, String outPut, String format, Float scale, Boolean quiet) {
String htmlDir = outPut;
Presentation pres = new Presentation(file.getAbsolutePath());
try {
if (!quiet) {
System.out.println("开始转换" + file.getName() + " to " + htmlDir);
}
// Saving to individual files
int total = pres.getSlides().size();
for (int i = 0; i < total; i++) {
if (!quiet) {
System.out.println("共" + total + "页, 第" + (i + 1) + "页。");
}
ISlide sld = pres.getSlides().get_Item(i);
BufferedImage image = sld.getThumbnail(scale, scale);
ImageIO.write(image, format, new File(htmlDir + "/slide_" + (i + 1) + "." + format));
}
} catch (Exception ex) {
System.out.println(ex);
} finally {
if (pres != null)
pres.dispose();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment