word转出图片(使用免费插件)02
来源:互联网 发布:2017nba总决赛欧文数据 编辑:程序博客网 时间:2024/06/18 08:20
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();
}
}
未完待续------------------------------------------------------------------------------------------------------------------------------------------------
- word转出图片(使用免费插件)02
- word转出图片(使用免费插件)01
- word转出图片(使用免费插件)03
- 关于PDF转Word转出来是图片及乱码多的解决方法分享
- 如何使用免费PDF第三方插件从PDF文档中提取文本和图片
- 图片 (免费的可商业使用的图片素材)
- Godaddy域名转出步聚 (转)
- 可以免费使用的便捷留言板插件
- cad转出高清图片的方法。分享
- 自动上传本地图片和word图片(word图片需使用从word粘贴功能)
- 使用Activex插件复制剪贴板中的word或excel内容到web HTML编辑器并自动上传图片或文件
- 使用Delphi开发Office Word插件
- 使用Delphi开发Office Word插件
- Word中使用代码高亮插件
- 免费生成二维码图片API使用
- Word插件
- 使用VC向word中插入图片
- word中使用宏批量插入图片
- 腾讯云 Ubuntu 安装并远程访问 MySQL
- 20171031
- Linux 系统 history 命令详解
- Jsp的九大对象,七大动作,三大指令
- 2017-10-31
- word转出图片(使用免费插件)02
- 学习php单利模式
- 复杂度分析之斐波那契数列
- Python 相关环境配置-Android,Python等
- idea Maven运行报错:-Dmaven.multiModuleProjectDirectory system propery is not set.
- Primes or Palindromes? CodeForces
- Java数据类型
- 2017-11-01 金银追踪,原油,美元,等分析
- 67. Add Binary