赞
踩
Aspose.words版本19.5
- import com.aspose.words.*;
- import com.google.common.io.Files;
- import lombok.SneakyThrows;
-
- import javax.imageio.ImageIO;
- import javax.imageio.stream.ImageInputStream;
- import java.awt.*;
- import java.awt.Shape;
- import java.awt.image.BufferedImage;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
-
-
- /**
- * @author ninge
- * @version
- */
- public class AsposeUtil {
-
- /**
- * licence 验证
- *
- * @return
- * @throws Exception
- */
- public static boolean getLicense() throws Exception {
- boolean result = false;
- try {
- InputStream is = AsposeUtil.class
- .getResourceAsStream("/aspose/license.xml");
- License aposeLic = new License();
- aposeLic.setLicense(is);
- result = true;
- is.close();
- } catch (Exception e) {
- e.printStackTrace();
- throw e;
- }
- return result;
- }
-
-
- /**
- * 判断是否为空
- *
- * @param obj 字符串对象
- * @return
- */
- protected static boolean notNull(String obj) {
- if (obj != null && !obj.equals("") && !obj.equals("undefined")
- && !obj.trim().equals("") && obj.trim().length() > 0) {
- return true;
- }
- return false;
- }
-
-
- /**
- * @Description: word和txt文件转换图片
- */
- public static List<BufferedImage> wordToImg(InputStream inputStream, int pageNum) throws Exception {
- OutputStream output=null;
- List<BufferedImage> imageList = new ArrayList<BufferedImage>();
- try {
- Document doc = new Document(inputStream);
- ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
- options.setPrettyFormat(true);
- options.setUseAntiAliasing(true);
- options.setUseHighQualityRendering(true);
- int pageCount = doc.getPageCount();
- if (pageCount > pageNum&&pageNum!=0) {//生成前pageCount张
- pageCount = pageNum;
- }
- for (int i = 0; i < pageCount; i++) {
- //doc.save("",options);
-
- output = new ByteArrayOutputStream();
- options.setPageIndex(i);
- doc.save(output, options);
- ImageInputStream imageInputStream= ImageIO.createImageInputStream(parse(output));
- imageList.add(ImageIO.read(imageInputStream));
- }
- } catch (Exception e) {
- e.printStackTrace();
- throw e;
- }finally {
- output.close();
- }
- return imageList;
- }
- public static void singleImg(BufferedImage bufferedImage) throws IOException {
- Graphics2D g2 = (Graphics2D) bufferedImage.getGraphics();
- g2.setBackground(Color.LIGHT_GRAY);
- g2.clearRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
- g2.setPaint(Color.RED);
- ImageIO.write(bufferedImage,"jpg",new File("/Users/ninge/Desktop/output/"+ UUID.randomUUID()+".jpg"));
-
- }
-
- //outputStream转inputStream
- public static ByteArrayInputStream parse(OutputStream out) throws Exception {
- ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
- ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
- baos.close();
- swapStream.close();
- return swapStream;
- }
-
- /**
- * word转图片输入流
- * @param inputStream
- * @param pageNum
- * @return
- */
- @SneakyThrows
- public static List<ImageInputStream> getImageInputStreamList(InputStream inputStream,Integer pageNum){
- // 验证License
- if (!getLicense()) {
- return null;
- }
- OutputStream output=null;
- List<ImageInputStream> imageList = new ArrayList<ImageInputStream>();
- try {
- Document doc = new Document(inputStream);
- ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
- options.setPrettyFormat(true);
- options.setUseAntiAliasing(true);
- options.setUseHighQualityRendering(true);
- int pageCount = doc.getPageCount();
- if (pageCount > pageNum&&pageNum!=0) {//生成前pageCount张
- pageCount = pageNum;
- }
- for (int i = 0; i < pageCount; i++) {
- output = new ByteArrayOutputStream();
- options.setPageIndex(i);
- doc.save(output, options);
- ImageInputStream imageInputStream= ImageIO.createImageInputStream(parse(output));
- imageList.add(imageInputStream);
- imageInputStream.close();
- }
- output.close();
- inputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return imageList;
-
- }
-
- /**
- * doc to pdf
- *
- * @param docPath doc源文件
- * @param pdfPath pdf目标文件
- */
- public static void doc2PDF(String docPath, String pdfPath) {
- try {
- // 验证License
- if (!getLicense()) {
- return;
- }
-
- if (notNull(docPath) && notNull(pdfPath)) {
- File file = new File(pdfPath);
- FileOutputStream os = new FileOutputStream(file);
- Document doc = new Document(docPath);
-
- doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 合并任数量的图片成一张图片
- *
- * @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();
- // 从图片中读取RGB
- int[] 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;
- }
-
- /**
- * 将BufferedImage转换为InputStream
- * @param image
- * @return
- */
- public static InputStream bufferedImageToInputStream(BufferedImage image){
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- try {
- ImageIO.write(image, "png", os);
- InputStream input = new ByteArrayInputStream(os.toByteArray());
- return input;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- /**
- * 追加红头
- * @param title 标题
- * @param no 文号
- * @param inputStream 阿里云下载文件的流
- * @return
- * @throws Exception
- */
- public static InputStream appendRedContent(String title,String no,InputStream inputStream) throws Exception {
- // 验证License
- if (!getLicense()) {
- return null;
- }
- Document doc = new Document(inputStream);
- inputStream.close();
- DocumentBuilder builder = new DocumentBuilder(doc);
- // Font font = builder.getFont();
- // font.setSize(26);//设置字大小
- // font.setBold(true);
- // font.setColor(Color.RED);
- // font.setName("宋体");
- //font.setUnderline(Underline.DASH);
- //builder.getParagraphFormat().getAlignment()=PageVerticalAlignment.CENTER;
-
- //builder.writeln(title);//追加标题
- // font.setSize(16);
- // font.setColor(Color.BLACK);
- //追加表格 o(╥﹏╥)o
- // builder.startTable();
- // builder.insertCell();
- //
- // builder.endRow();
- // builder.endTable();
- //builder.writeln(no);//追加文号
- //builder.insertBreak(BreakType.LINE_BREAK);
- //builder.insertImage(AsposeUtil.class.getResourceAsStream("/static/red_line.png"));
- // builder.insertHtml("<table style=\"border:0px;width:560px;text-align:center;\" >" +
- // "<tr>" +
- // "<td style=\"font-size:30px;font-weight:bold;color:red;\">"+title+"</td>" +
- // "</tr>" +
- // "<tr>" +
- // "<td style=\"font-size:20px;font-weight:bold;border-bottom:red solid 3px;line-height:30px\">"+no+"</td>" +
- // "</tr>" +
- // "</table><br/>",true);
- builder.insertHtml("<div style=\"text-align:center;border-bottom:3px solid red;\">" +
- "<span style=\"font-size:35px;color:red;display:block;\">"+title+"</span>" +
- "<span style=\"font-size:25px;\">"+no+"</span>" +
- "</div><br/>");
- ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
- doc.save(dstStream, SaveFormat.DOCX);
- inputStream=new ByteArrayInputStream(dstStream.toByteArray());
- dstStream.close();
- return inputStream;
- }
-
- public static void main(String agrs[]){
-
-
- //本地word追加开始,只在word文档最开头最佳
- File file = new File("/Users/ninge/Desktop/spmk.docx");
- InputStream inputStream= appendRedContent("sss","桥通天下〔2020〕12 号",new FileInputStream(file));
- byte[] buffer = new byte[inputStream.available()];
- inputStream.read(buffer);
- File targetFile = new File("/Users/ninge/Desktop/spmk1.docx");
- Files.write(buffer, targetFile);
- inputStream.close();
- //本地word追加结束
- //转图片
- file = new File("/Users/ninge/Desktop/spmk1.docx");
- inputStream = new FileInputStream(file);
- List<BufferedImage> wordToImg = wordToImg(inputStream,0);
- BufferedImage mergeImage = mergeImage(true, wordToImg);
-
- ImageIO.write(mergeImage, "jpg", new File("/Users/ninge/Desktop/"+
- UUID.randomUUID()+".jpg"));
-
-
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。