文档相关工具

package com.lc.ibps.components.upload.util;

import java.awt.FontMetrics;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JLabel;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.lc.ibps.base.core.util.AppUtil;

import freemarker.template.Configuration;
import freemarker.template.Template;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @项目名称:
 * @类名称: DocumentUtils
 * @类描述: 文档工具类
 * @修改备注:
 * @version: 1.0
 */
public class DocumentUtils {
    private static int interval = -5;

    /**
     * 导出word
     * 
     * @方法名:exportWord
     * @参数 @param templateName .xml格式word模板
     * @参数 @param dataModel 数据
     * @参数 @return
     * @返回类型 String
     */
    public static String exportWord(String templateName, Object dataModel) {
        FileOutputStream fos = null;
        OutputStreamWriter writer = null;
        String outFile = null;
        try {
            // 导出路径
            String outPath = AppUtil.getProperty("file.upload");
            // 导出文件名
            String outName = templateName + "_output.doc";
            // 导出doc全路径
            outFile = outPath + File.separator + outName;
            // 临时目录
            String exceltemplates = outPath + File.separator + "exceltemplates";

            fos = new FileOutputStream(outFile);
            writer = new OutputStreamWriter(fos, "UTF-8");

            Configuration config = new Configuration(Configuration.VERSION_2_3_23);
            config.setDefaultEncoding("UTF-8");
            config.setDirectoryForTemplateLoading(new File(exceltemplates));
            // 模板路径
            Template template = config.getTemplate(templateName + ".xml", "UTF-8");
            template.process(dataModel, writer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return outFile;
    }

    /**
     * 模板、数据、生成word
     * 
     * @方法名:exportWord
     * @参数 @param templateName 模板名称
     * @参数 @param dataModel 数据
     * @参数 @param target 目标doc路径
     * @返回类型 void
     */
    public static void exportWord(String templateName, Object dataModel, String target) {
        FileOutputStream fos = null;
        OutputStreamWriter writer = null;
        // 临时目录
        String exceltemplates = AppUtil.getProperty("file.upload") + File.separator + "exceltemplates";
        try {
            fos = new FileOutputStream(target);
            writer = new OutputStreamWriter(fos, "UTF-8");

            Configuration config = new Configuration(Configuration.VERSION_2_3_23);
            config.setDefaultEncoding("UTF-8");
            config.setDirectoryForTemplateLoading(new File(exceltemplates));
            // 模板路径
            Template template = config.getTemplate(templateName + ".xml", "UTF-8");
            template.process(dataModel, writer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * Word转PDF
     * 
     * @方法名:convertPdf
     * @参数 @param templateName
     * @参数 @param wordPath
     * @参数 @return
     * @返回类型 String
     */
    public static String convertPdf(String templateName, String wordPath) {
        String outFile = null;
        FileOutputStream fos = null;
        try {
            // 导出文件名
            String outName = templateName + "_output.pdf";
            // 导出路径
            String outPath = AppUtil.getProperty("file.upload");
            // 导出doc全路径
            outFile = outPath + File.separator + outName;
            fos = new FileOutputStream(outFile);

            // 注册
            License aposeLic = new License();
            aposeLic.setLicense(
                    new FileInputStream(DocumentUtils.class.getClassLoader().getResource("license.xml").getPath()));

            // 转换
            Document doc = new Document(new FileInputStream(wordPath));
            doc.save(fos, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return outFile;
    }

    /**
     * word转成pdf
     * 
     * @方法名:wordToPdf
     * @参数 @param source doc路径
     * @参数 @param target 目标pdf路径
     * @返回类型 void
     */
    public static void wordToPdf(String source, String target) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target);

            // 注册
            License aposeLic = new License();

            String path = AppUtil.getProperty("file.upload") + File.separator + "exceltemplates" + File.separator
                    + "license.xml";
            aposeLic.setLicense(new FileInputStream(path));
            // 转换
            Document doc = new Document(fis);
            doc.save(fos, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * word转成pdf
     * 
     * @方法名:wordToPdf
     * @参数 @param wordList 源WORD文件信息
     * @返回类型 List<String>
     */
    public static List<String> wordToPdf(JSONArray wordList) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        String root = AppUtil.getProperty("file.upload");// 上传根目录

        String target = root + File.separator + "pdf";// 临时PDF存放目录

        File pdf = new File(target);
        if (!pdf.exists()) {
            pdf.mkdir();
        }
        List<String> pdfList = new ArrayList<>();
        try {
            String name = UUID.randomUUID().toString();
            // 注册
            License aposeLic = new License();// 注册license
            String path = root + File.separator + "exceltemplates" + File.separator + "license.xml";// 获取license模板
            aposeLic.setLicense(new FileInputStream(path));

            for (Object object : wordList) {
                JSONObject o = JSONObject.fromObject(object);

                String targetPath = target + File.separator + name + "_word_tmp" + ".pdf";

                fis = new FileInputStream(root + File.separator + o.getString("filePath"));
                fos = new FileOutputStream(targetPath);

                pdfList.add(targetPath);

                // 转换
                Document doc = new Document(fis);
                doc.save(fos, SaveFormat.PDF);
                // 流处理
                fos.flush();
                fos.close();
                fis.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                fos.flush();
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return pdfList;
    }

    /**
     * 读取word文本
     * 
     * @方法名:readWordToString
     * @参数 @param path
     * @参数 @return
     * @返回类型 String
     */
    /*
     * public static String readWordToString(String path) { String result = ""; try
     * { if (null != path && path.indexOf("doc") > 0) { if (path.endsWith(".doc")) {
     * InputStream is = new FileInputStream(path); WordExtractor ex = new
     * WordExtractor(is); result = ex.getText(); is.close(); } else if
     * (path.endsWith(".docx")) { OPCPackage o = POIXMLDocument.openPackage(path);
     * POIXMLTextExtractor extractor = new XWPFWordExtractor(o); result =
     * ((XWPFWordExtractor) extractor).getText(); o.close(); } } } catch (Exception
     * e) { e.printStackTrace(); result = ""; } return result; }
     */

    /**
     * 读取pdf文本
     * 
     * @方法名:readPdfToString
     * @参数 @param path
     * @参数 @return
     * @返回类型 String
     */
    public static String readPdfToString(String path) {
        try {
            FileInputStream fis = new FileInputStream(path);
            PDFParser p = new PDFParser(fis);
            p.parse();
            PDDocument pdd = p.getPDDocument();
            PDFTextStripper ts = new PDFTextStripper();
            String result = ts.getText(pdd);
            pdd.close();
            fis.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * PDF合并
     * 
     * @param pdfFilenames
     * @param targetFilename
     * @throws Exception
     */
    public static void combinPdf(List<String> pdfFilenames, String targetFilename) throws Exception {
        PdfReader reader = null;
        com.itextpdf.text.Document doc = new com.itextpdf.text.Document();
        PdfCopy pdfCopy = new PdfCopy(doc, new FileOutputStream(targetFilename));
        int pageCount = 0;
        doc.open();
        for (int i = 0; i < pdfFilenames.size(); ++i) {
            reader = new PdfReader(pdfFilenames.get(i));
            pageCount = reader.getNumberOfPages();
            for (int j = 1; j <= pageCount; ++j) {
                pdfCopy.addPage(pdfCopy.getImportedPage(reader, j));
            }
        }
        doc.close();
        pdfCopy.close();
        reader.close();
    }

    public static void waterMark(String inputFile, String outputFile, String waterMarkName) {
        try {
            // 读取需要添加水印的PDF文件
            PdfReader reader = new PdfReader(inputFile);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
            // 设置水印字体
            BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);

            Rectangle pageRect = null;
            PdfGState gs = new PdfGState();
            // 设置水印透明度
            gs.setFillOpacity(0.1f);
            gs.setStrokeOpacity(0.2f);
            // 计算PDF的页数
            int total = reader.getNumberOfPages() + 1;

            JLabel label = new JLabel();
            FontMetrics metrics;
            int textH = 0;
            int textW = 0;
            label.setText(waterMarkName);
            metrics = label.getFontMetrics(label.getFont());
            textH = metrics.getHeight();
            textW = metrics.stringWidth(label.getText());

            PdfContentByte under;
            // 給没一页加上水印,可以随便指定页
            for (int i = 1; i < total; i++) {
                pageRect = reader.getPageSizeWithRotation(i);
                under = stamper.getOverContent(i);
                under.saveState();
                under.setGState(gs);
                under.beginText();
                under.setFontAndSize(base, 40);
                
                under.showTextAligned(Element.ALIGN_LEFT, waterMarkName,  textW,  textH, 30);
                // 水印文字成30度角倾斜
                /*for (int height = interval + textH; height < pageRect.getHeight(); height = height + textH * 3) {
                    for (int width = interval + textW; width < pageRect.getWidth() + textW; width = width + textW * 2) {
                        under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW, height - textH, 30);
                    }
                }*/
                // 添加水印文字
                under.endText();
            }
            // 关闭流
            stamper.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量打包
     *
     * @param jsonString
     *            json格式字符串数据
     * @param fileSaveRootPath
     *            项目根目录
     * @return zip文件保存绝对路径
     */
    public static String createZipAndReturnPath(List<Map<String, String>> filePathList, 
            String fileSaveRootPath) {
        // 生成jsonArray列表
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMDD");
        String time = sdf.format(new Date());
        try {
            // 生成打包下载后的zip文件
            String papersZipName = "文件包"+time+".zip";

            // zip文件保存路径
            String zipPath = fileSaveRootPath + papersZipName;

            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));

            // 遍历jsonArray列表获取所有JSONObject对象
            for (int i = 0; i < filePathList.size(); i++) {
                Map<String, String> fileMap = filePathList.get(i);
                String fileName = fileMap.get("FILE_NAME");
                String relativePath = fileMap.get("FILE_PATH");

                // 获得下载文件完整路径
                String downloadPath = fileSaveRootPath + relativePath;

                FileInputStream fis = new FileInputStream(downloadPath);
                out.putNextEntry(new ZipEntry(fileName));

                // 写入压缩包
                int len;
                byte[] buffer = new byte[1024];
                while ((len = fis.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                out.closeEntry();
                fis.close();
            }
            out.close();
            out.flush();
            return zipPath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        waterMark("d:\12.pdf", "D:\456.pdf", "新化啊");
    }
}
DocumentUtils.java
原文地址:https://www.cnblogs.com/tangzeqi/p/12794510.html