word转出图片(使用免费插件)02
word转图片方法:
1.word转pdf用的是poi,pdf转图片用的是icepdf
/**
* 将word文档, 转换成pdf, 中间替换掉变量
* @param source 源为word文档, 必须为docx文档
* @param target 目标输出
* @param params 需要替换的变量
* @throws Exception
*/
public static void wordConverterToPdf(InputStream source,
OutputStream target, Map<String, String> params) throws Exception {
wordConverterToPdf(source, target, null, params);
}
/**
* 将word文档, 转换成pdf, 中间替换掉变量
* @param source 源为word文档, 必须为docx文档
* @param target 目标输出
* @param params 需要替换的变量
* @param options PdfOptions.create().fontEncoding( "windows-1250" ) 或者其他
* @throws Exception
*/
public static void wordConverterToPdf(InputStream source, OutputStream target,
PdfOptions options,
Map<String, String> params) throws Exception {
//HWPFDocument doc=new HWPFDocument(source);
XWPFDocument doc = new XWPFDocument(source);
paragraphReplace(doc.getParagraphs(), params);
for (XWPFTable table : doc.getTables()) {
for (XWPFTableRow row : table.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
paragraphReplace(cell.getParagraphs(), params);
}
}
}
PdfConverter.getInstance().convert(doc, target, options);
}
/** 替换段落中内容 */
private static void paragraphReplace(List<XWPFParagraph> paragraphs, Map<String, String> params) {
if (MapUtils.isNotEmpty(params)) {
for (XWPFParagraph p : paragraphs){
for (XWPFRun r : p.getRuns()){
String content = r.getText(r.getTextPosition());
if(StringUtils.isNotEmpty(content) && params.containsKey(content)) {
r.setText(params.get(content), 0);
}
}
}
}
}
/**
* 将指定pdf文件的首页转换为指定路径的缩略图
*@param filepath 原文件路径,例如d:/test.pdf
*@param imagepath 图片生成路径,例如 d:/test-1.jpg
*@param zoom 缩略图显示倍数,1表示不缩放,0.3则缩小到30%
*/
public static void tranfer(String filepath, String imagepath, float zoom) throws Exception {
// ICEpdf document class
Document document = null;
float rotation = 0f;
document = new Document();
try
{
document.setFile(filepath);
} catch (Exception e1) {
e1.printStackTrace();
}
Integer maxPages = document.getPageTree().getNumberOfPages();
for (int i = 0; i < maxPages; i++) {
// maxPages = document.getPageTree().getNumberOfPages();
Iterator iter = null;
ImageWriter writer = null;
// 文件输出位置:
FileOutputStream out = null;
ImageOutputStream outImage = null;
BufferedImage img = (BufferedImage) document.getPageImage(i,
GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation,
zoom);
try {
File outFile = new File("/users/limeng/ccc4/ccc"+i+SUFF_IMAGE);
iter = ImageIO.getImageWritersBySuffix(FILETYPE_JPG);
writer = (ImageWriter) iter.next();
out = new FileOutputStream(outFile);
outImage = ImageIO.createImageOutputStream(out);
writer.setOutput(outImage);
writer.write(new IIOImage(img, null, null));
System.gc();
} catch (IOException e) {
e.printStackTrace();
} finally {
outImage.flush();
outImage.close();
out.close();
document.dispose();
System.gc();
}
}
}
public static void main(String[] args) {
try {
//word转pdf在走的是office服务,在其他系统上有问题,通用性不好
//pdf转图片方法在各个系统通用
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long c1=Calendar.getInstance().getTimeInMillis();
//路径写成这样,因为我的运行环境是mac系统
String filepath = "/users/limeng/aaa.docx";
String outpath = "/users/limeng/ccc.pdf";
InputStream source = new FileInputStream(filepath);
OutputStream target = new FileOutputStream(outpath);
Map<String, String> params = new HashMap<String, String>();
PdfOptions options = PdfOptions.create();
wordConverterToPdf(source, target, options, params);
tranfer(outpath,"/users/limeng/ccc.jpg",4f);
long c2=Calendar.getInstance().getTimeInMillis();
System.out.println((c2-c1)/1000);
} catch (Exception e) {
e.printStackTrace();
}
}
未完待续------------------------------------------------------------------------------------------------------------------------------------------------