@[toc] java word 转 PDF|HTML|PNG
word文档转换图片或pdf格式,需要使用(aspose-words-15.8.0-jdk16.jar)

  • 链接:https://pan.baidu.com/s/1z08E3IkJ8BbTrkJspF_osA
  • 提取码:8qpu
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

word 转 PDF

    /*** word转pdf* @param docPath* @param savePath*/public static void word2pdf(String docPath,String savePath){try {String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());License license = new License();license.setLicense(is);Document document = new Document(docPath);document.save(new FileOutputStream(new File(savePath)),SaveFormat.PDF);} catch (Exception e) {e.printStackTrace();}}

word转HTML

/*** word转HTML* @param docPath* @param savePath*/public static void word2HTML(String docPath,String savePath){try {String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());License license = new License();license.setLicense(is);Document document = new Document(docPath);document.save(new FileOutputStream(new File(savePath)),SaveFormat.HTML);} catch (Exception e) {e.printStackTrace();}}

word和txt文件转换图片

    /*** word和txt文件转换图片* @param inputStream* @param pageNum* @return* @throws Exception*/private static List<BufferedImage> wordToImg(InputStream inputStream, int pageNum) throws Exception {if (!isWordLicense()) {return null;}try {Date start = new Date();Document doc = new Document(inputStream);ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);options.setPrettyFormat(true);options.setUseAntiAliasing(true);options.setUseHighQualityRendering(true);int pageCount = doc.getPageCount();//生成前pageCount张,这可以限制输出长图时的页数/*if (pageCount > pageNum) {pageCount = pageNum;}*/List<BufferedImage> imageList = new ArrayList<BufferedImage>();for (int i = 0; i < pageCount; i++) {OutputStream output = new ByteArrayOutputStream();options.setPageIndex(i);//setPageSet(com.aspose.words.PageSet value)//options.setPageSet(i);doc.save(output, options);ImageInputStream imageInputStream = javax.imageio.ImageIO.createImageInputStream(parse(output));imageList.add(javax.imageio.ImageIO.read(imageInputStream));}List<BufferedImage> imageList2 = new ArrayList<BufferedImage>();//这个重新生成新的图片是因为直接输出的图片底色为红色for(int j=0; j<imageList.size(); j++){// 生成新图片BufferedImage destImage = imageList.get(j);int w1 = destImage.getWidth();int h1 = destImage.getHeight();destImage = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);Graphics2D g2 = (Graphics2D) destImage.getGraphics();g2.setBackground(Color.LIGHT_GRAY);g2.clearRect(0, 0, w1, h1);g2.setPaint(Color.RED);// 从图片中读取RGBint[] ImageArrayOne = new int[w1 * h1];ImageArrayOne = imageList.get(j).getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中destImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGBimageList2.add(destImage);}Date end = new Date();long l=end.getTime()-start.getTime();long hour= l / (1000 * 60 * 60);long min=  (l-hour*(1000 * 60 * 60 ))/(1000* 60);long s= (l-hour*(1000 * 60 * 60 )-min*1000*60)/(1000);long ss= (l-hour*(1000 * 60 * 60 )-min*1000*60 -s*1000)/(1000/60);System.out.println("word转图片时间:"+min+"分"+s+"秒" + ss + "毫秒");//hour+"小时"+return imageList2;} catch (Exception e) {e.printStackTrace();throw e;}}//outputStream转inputStreampublic static ByteArrayInputStream parse(OutputStream out) throws Exception {ByteArrayOutputStream baos = new ByteArrayOutputStream();baos = (ByteArrayOutputStream) out;ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());return swapStream;}

去水印

    /*** 验证aspose.word组件是否授权:无授权的文件有水印标记* 需要使用(aspose-words-15.8.0-jdk16.jar),版本要对应。无水印* @return*/public static boolean isWordLicense() {boolean result = false;try {String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes());com.aspose.words.License license = new com.aspose.words.License();license.setLicense(inputStream);result = true;} catch (Exception e) {e.printStackTrace();}return result;}

合并任数量的图片成一张图片

/*** 合并任数量的图片成一张图片* @param isHorizontal true代表水平合并,fasle代表垂直合并* @param imgs 待合并的图片数组* @return* @throws IOException*/public static BufferedImage mergeImage(boolean isHorizontal, List<BufferedImage> imgs) throws IOException {// 生成新图片BufferedImage destImage = null;// 计算新图片的长和高int allw = 0, allh = 0, allwMax = 0, allhMax = 0;// 获取总长、总宽、最长、最宽for (int i = 0; i < imgs.size(); i++) {BufferedImage img = imgs.get(i);allw += img.getWidth();if (imgs.size() != i + 1) {allh += img.getHeight() + 5;} else {allh += img.getHeight();}if (img.getWidth() > allwMax) {allwMax = img.getWidth();}if (img.getHeight() > allhMax) {allhMax = img.getHeight();}}// 创建新图片if (isHorizontal) {destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);} else {destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);}Graphics2D g2 = (Graphics2D) destImage.getGraphics();g2.setBackground(Color.LIGHT_GRAY);g2.clearRect(0, 0, allw, allh);g2.setPaint(Color.RED);// 合并所有子图片到新图片int wx = 0, wy = 0;for (int i = 0; i < imgs.size(); i++) {BufferedImage img = imgs.get(i);int w1 = img.getWidth();int h1 = img.getHeight();// 从图片中读取RGBint[] ImageArrayOne = new int[w1 * h1];ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中if (isHorizontal) { // 水平方向合并destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB} else { // 垂直方向合并destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB}wx += w1;wy += h1 + 5;}return destImage;}

测试

public static void main(String[] args){//word转pdfword2pdf("C:\\11.docx","C:\\11.pdf");//word转htmlword2HTML("C:\\11.docx","C:\\11.html");//word转图片格式try {File file = new File("C:\\11.docx");InputStream inStream = new FileInputStream(file);List<BufferedImage> wordToImg = wordToImg(inStream,2);//for(int i=0; i<wordToImg.size(); i++){//保存图片(单张)ImageIO.write(wordToImg.get(i), "jpg", new File("C:\\"+ i +".png")); //将其保存在C:/imageSort/targetPIC/下}BufferedImage mergeImage = mergeImage(false, wordToImg);//保存图片(长图)ImageIO.write(mergeImage, "jpg", new File("C:\\Users\\86152\\Desktop\\word\\xx.png"));} catch (Exception e) {e.printStackTrace();}word2HTML("C:\\11.docx","C:\\11.html");}

第二种

import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.contentstream.operator.Operator;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.pdfparser.PDFStreamParser;
import org.apache.pdfbox.pdfwriter.ContentStreamWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDStream;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
//https://blog.csdn.net/csdnFlyFun/article/details/79523262?locationNum=1&fps=1
public class Word2PDF {public static void main(String[] args) throws IOException {//doc2pdf("C:\\word\\11.docx");}//替换pdf文本内容public static void replaceText(PDPage page, String searchString, String replacement) throws IOException {PDFStreamParser parser = new PDFStreamParser(page);List<?> tokens = parser.parse();for (int j = 0; j < tokens.size(); j++) {Object next = tokens.get(j);if (next instanceof Operator) {Operator op = (Operator) next;String pstring = "";int prej = 0;if (op.getName().equals("Tj")) {COSString previous = (COSString) tokens.get(j - 1);String string = previous.getString();string = string.replaceFirst(searchString, replacement);previous.setValue(string.getBytes());} else if (op.getName().equals("TJ")) {COSArray previous = (COSArray) tokens.get(j - 1);for (int k = 0; k < previous.size(); k++) {Object arrElement = previous.getObject(k);if (arrElement instanceof COSString) {COSString cosString = (COSString) arrElement;String string = cosString.getString();if (j == prej) {pstring += string;} else {prej = j;pstring = string;}}}if (searchString.equals(pstring.trim())) {COSString cosString2 = (COSString) previous.getObject(0);cosString2.setValue(replacement.getBytes());int total = previous.size() - 1;for (int k = total; k > 0; k--) {previous.remove(k);}}}}}List<PDStream> contents = new ArrayList<>();Iterator<PDStream> streams = page.getContentStreams();while (streams.hasNext()) {PDStream updatedStream = streams.next();OutputStream out = updatedStream.createOutputStream(COSName.FLATE_DECODE);ContentStreamWriter tokenWriter = new ContentStreamWriter(out);tokenWriter.writeTokens(tokens);contents.add(updatedStream);out.close();}page.setContents(contents);}//移除图片水印public static void removeImage(PDPage page, String cosName) {PDResources resources = page.getResources();COSDictionary dict1 = resources.getCOSObject();resources.getXObjectNames().forEach(e -> {if (resources.isImageXObject(e)) {COSDictionary dict2 = dict1.getCOSDictionary(COSName.XOBJECT);if (e.getName().equals(cosName)) {dict2.removeItem(e);}}page.setResources(new PDResources(dict1));});}//移除文字水印public static boolean removeWatermark(File file) {try {//通过文件名加载文档PDDocument document = Loader.loadPDF(file);PDPageTree pages = document.getPages();Iterator<PDPage> iter = pages.iterator();while (iter.hasNext()) {PDPage page = iter.next();//去除文字水印replaceText(page, "Evaluation Only. Created with Aspose.Words. Copyright 2003-2021 Aspose Pty Ltd.", "");replaceText(page, "Created with an evaluation copy of Aspose.Words. To discover the full versions of our APIs please", "");replaceText(page, "visit: https://products.aspose.com/words/", "");replaceText(page, "Created with an evaluation copy of Aspose.Words. To discover the full", "");replaceText(page, "versions of our APIs please visit: https://products.aspose.com/words/", "");replaceText(page, "This document was truncated here because it was created in the Evaluation", "");//去除文字水印replaceText(page, "Evaluation Only. Created with Aspose.Words. Copyright 2003-2021 Aspose", "");replaceText(page, "Evaluation Only. Created with Aspose.Words. Copyright 2003-2021 Aspose Pty Ltd.", "");replaceText(page, "Pty Ltd.", "");replaceText(page, "Created with an evaluation copy of Aspose.Words. To discover the full", "");replaceText(page, "versions of our APIs please visit: https://products.aspose.com/words/", "");replaceText(page, "This document was truncated here because it was created in the Evaluation", "");replaceText(page, "Created with an evaluation copy of Aspose.Words. To discover the full versions of our APIs please visit: https://products.aspose.com/words/", "");replaceText(page, "Created with an evaluation copy of Aspose.Words. To discover the full versions of", "");replaceText(page, "our APIs please visit: https://products.aspose.com/words/", "");//去除图片水印removeImage(page, "X1");}document.removePage(document.getNumberOfPages() - 1);file.delete();document.save(file);document.close();return true;} catch (IOException ex) {ex.printStackTrace();return false;}}//doc文件转pdf(目前最大支持21页)//public static void doc2pdf(String wordPath) {public static void doc2pdf(String wordPath,String pdfPath) {long old = System.currentTimeMillis();try {//新建一个pdf文档//String pdfPath=wordPath.substring(0,wordPath.lastIndexOf("."))+".pdf";File file = new File(pdfPath);FileOutputStream os = new FileOutputStream(file);//Address是将要被转化的word文档Document doc = new Document(wordPath);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换doc.save(os, SaveFormat.PDF);os.close();//去除水印removeWatermark(new File(pdfPath));//转化用时long now = System.currentTimeMillis();System.out.println("Word 转 Pdf 共耗时:" + ((now - old) / 1000.0) + "秒");} catch (Exception e) {System.out.println("Word 转 Pdf 失败...");e.printStackTrace();}}}

java word 转 PDF|HTML|PNG相关推荐

  1. java word转pdf 后通过 PdfReader 和 PdfStamper对pdf添加水印 通过poi等组件实现

    所需jar包地址 <!-- java 读取word文件里面的加颜色的字体 转pdf 使用 --><dependency><groupId> e-iceblue &l ...

  2. Java WORD转换PDF 并添加水印 (附赠jar提取链接)

    Java WORD转换PDF 并添加水印 直接上代码 实现功能 docx文档转换为PDF 转换之后排版不混乱 使用工具(Jar包) aspose-words-15.8.0-jdk16.jar(用于PD ...

  3. Java word转pdf(替换变量,转图片)

    菜鸟一个,不算原创,学习后的小结.有不足之处,请大家多多指教 Java word转pdf中遇到的问题: 1.在网上找到了一种方法,利用aspose-words,转换效果好但是有两个问题:一.转换时间长 ...

  4. aspose-word for java word转pdf 解决遇到的问题

    aspose-word for java word转pdf 解决遇到的问题 具体问题 在项目中使用aspose-word 把word转换为pdf 有次一份63页的文档转换出来的pdf为72页,正常情况 ...

  5. java word转pdf linux_Linux平台中使用PHP把word转pdf的实现方法

    Linux平台中使用PHP把word转pdf的实现方法 1.ubantu下安装libreoffice sudo apt-get install libreoffice 2.命令行执行word转pdf ...

  6. java word转pdf jacob_java使用jacob.jar将word转pdf

    这篇文章主要为大家详细介绍了java利用jacob.jar将word转pdf,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了java利用jacob.jar将word转pdf的具 ...

  7. Java word转pdf Linux/windows跨平台 格式完美(利用命令行调用libreoffice)

    参考了里面的libreoffice的用法:https://blog.csdn.net/qwert678000/article/details/72770109 需求描述 最近在做word报表的自动生成 ...

  8. java word转pdf【去水印】

    word转pdf pom添加的依赖: <!--word转pdf关键包--><dependency><groupId>com.aspose.words</gro ...

  9. Java word转pdf 精确获取文件页数(jacob)

    注意: 该项目需在windows下进行, 如果需要商用需准备Windows服务器 这里我们用到的工具是jacob 需要创建一个maven项目添加以下依赖 <dependency><g ...

最新文章

  1. java数组交集_Java 实例 – 数组交集
  2. 禁用计算机系统错误汇报,技术员给你关于win10关闭自动发送错误报告的具体方法...
  3. 第10章 指针(五) 使用地址-------指针的引用
  4. Linux和Windows路由配置
  5. 主机动手系列 — 怎么管理Suse Linux
  6. java hipster!_通过Java Hipster升级Spring Security OAuth和JUnit测试
  7. 移动互联网广告 - 第十更 - 广告投放运营 DashBoard - 2016/12/10
  8. python画PR曲线(precision-recall曲线)
  9. 如何让Excel单元格中的名字分散对齐
  10. wnoise matlab,MATLAB中用wnoise函数测试去噪算法
  11. android 富文本编辑器_富文本编辑器,还是Tinymce好一点?Angular/Vue集成最新版
  12. PyQt5-QComboBox控件使用实现省市级联效果
  13. 给所有使用FMX(FireMonkey)进行开发的忠告!
  14. c语言入门这一篇就够了-学习笔记(一万字)
  15. 阿泰,水晶报表--pull模式样板
  16. CentOS7-命令-创建用户(useradd)
  17. 自适应螺旋飞行麻雀搜索算法
  18. GB、MB、KB分别是什么意思。大小分别是多少
  19. C语言字符串string.h详解
  20. android lr 输出分辨率设置,Resolume Arena输出分辨率怎么调 设置分辨率的方法介绍...

热门文章

  1. windows添加系统变量
  2. 微信公众号开发步骤(一)
  3. 【雷达通信】 基于卡尔曼滤波实现GPS和INS联合导航含Matlab源码
  4. ARM Neon 简介
  5. 编程海龟图c语言,大家一起来探讨一下乌龟作图用c语言的实现方法
  6. AOP @AfterReturning的使用
  7. 原来还会有这样的事!
  8. 记一次在win10上的ASF挂卡经历
  9. 从零开始 荔枝派nano TF卡启动
  10. 传感器数据处理Ⅰ------常用里程计模型