java简单操作word和pdf

所需依赖

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.doc.free</artifactId>
    <version>3.9.0</version>
</dependency>
<!--操作pdf -->
<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.pdf.free</artifactId>
    <version>3.9.0</version>
</dependency>
<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.pdf</artifactId>
    <version>3.9.6</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
  	<groupId>org.apache.poi</groupId>
  	 <artifactId>ooxml-schemas</artifactId>
  	 <version>1.3</version>
</dependency>

<!--仓库地址-->
 <repository>
    <id>com.e-iceblue</id>
     <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
 </repository>

PDF转word
去除首段水印

package com.example.daimademo.utils;

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class PdfUtils {
    public static void main(String[] args) throws  Exception{
// pdf转换word
//        pdf2Word("D:\\upload\\yy\\水土保持方案.pdf",
//                "D:\\upload\\yy\\ssm3.docx");
//word截去第一段水印
        RemoveTag("D:\\\\upload\\\\yy\\\\ssm3.docx","D:\\\\upload\\\\yy\\\\ssm31.docx");
    }

    /**
     * pdf to word
      * @param pdfPath
     * @param wordPath
     */
    public static void pdf2Word(String pdfPath,String wordPath){
        PdfDocument pdf = new PdfDocument();
        //Load a PDF file
        pdf.loadFromFile(pdfPath);
        //Save to .docx file
        pdf.saveToFile(wordPath, FileFormat.DOCX);
        pdf.close();
    }

    public static void RemoveTag(String wordPath,String wordPath2) throws Exception {
        String type = wordPath;
        InputStream inputStream=null;

        if (type.endsWith(".doc")) {
            inputStream = new FileInputStream(wordPath);
            HWPFDocument hwpfDocument = new HWPFDocument(inputStream);
            //以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告
            //hwpfDocument.delete() 该方法去掉文档指定长度的内容
            hwpfDocument.delete(0, 70);
            //输出word内容文件流,新输出路径位置
            OutputStream os = new FileOutputStream(wordPath2);
            try {
                hwpfDocument.write(os);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                os.close();
                inputStream.close();
            }
        } else if (type.endsWith(".docx")) {
            inputStream = new FileInputStream(wordPath);
            XWPFDocument old_document = new XWPFDocument(inputStream);
            //以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告
            old_document.removeBodyElement(0);
            //输出word内容文件流,新输出路径位置
            OutputStream os = new FileOutputStream(wordPath2);
            try {
                old_document.write(os);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                os.close();
                inputStream.close();
            }
        }
    }
    /**
     * ConvertPdfToExcel
     * @param pdfPath
     * @param excelPath
     */
    public void ConvertPdfToExcel (String pdfPath,String excelPath){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load a PDF file
        pdf.loadFromFile(pdfPath);
        //Save to .xlsx file
        pdf.saveToFile(excelPath, FileFormat.XLSX);
        pdf.close();
    }

    /**
     * ConvertPdfToImage
     * @param pdfPath
     * @param pngPath
     * @throws Exception
     */
    public void ConvertPdfToImage (String pdfPath,String pngPath) throws Exception{

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load a PDF file
        pdf.loadFromFile(pdfPath);

        //Declare a BufferedImage variable
        BufferedImage image;

        //Loop through the pages
        for (int i = 0; i < pdf.getPages().getCount(); i++) {
            //Save the specific page as image data
            image = pdf.saveAsImage(i);
            //Write image data to png file
            File file = new File(String.format(pngPath, i));
            ImageIO.write(image, "PNG", file);
        }
        pdf.close();
    }

    }

简单操作word
word转pdf

package com.iwhr.siyu.common.util.word;
import com.spire.doc.FileFormat;
import com.spire.doc.Document;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import lombok.SneakyThrows;
import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.*;

/**
 * word相关工具
 * @author ssm
 */
public class WordUtils {

    /**
     * apache poi实现
     *将word中某些标签替换成指定的值,并生成一个新的word文档。
     * @param templateFilePath word模板文件路径
     * @param outFilePath 填充后输出文件路径
     * @param map  key:word中的占位标签,value对应标签要替换的值。
     * @throws IOException
     */
    public static void  insertAndOutFile(String templateFilePath, String outFilePath, Map<String,String> map) throws IOException {
        //准备工作,生成docx对象
        String templatePath=templateFilePath;
        InputStream is=new FileInputStream(templatePath);
        XWPFDocument docx=new XWPFDocument(is);
        //获取该格子里所有的段
        List<XWPFParagraph> paragraphs=docx.getParagraphs();
        for(XWPFParagraph p:paragraphs) {
            //遍历该格子里的段
            List<XWPFRun> runs=p.getRuns();
            for(XWPFRun run:runs) {
                String text=run.getText(0);
                Set<String> keySet = map.keySet();
                for(String key:keySet){
                    if (text.indexOf(key) != -1) {
                        text = text.replace(key, map.get(key));
                        run.setText(text, 0);
                    }
                }
            }
        }
        OutputStream os=new FileOutputStream(outFilePath);
        docx.write(os);
        is.close();
        os.close();
    }

    /**
     * spire.doc.free 实现word转pdf
     * @param wordPath
     * @param pdfPath
     */
    @SneakyThrows
    public static void word2Pdf(String wordPath ,String pdfPath){
        Document doc = new Document(wordPath);
        doc.saveToFile(pdfPath, FileFormat.PDF);
    }
    /**
     * 替换Word文件中 ${} 标识的特殊字符
     * @param templateFilePath
     * @param outFilePath
     * @param map
     */
    @SneakyThrows
    public static void replaceSpecialWord(String templateFilePath,String outFilePath, Map<String, String> map) {
        Document doc = new Document();
        FileFormat type;
        if("doc".equals(templateFilePath.substring(templateFilePath.length()-3))){
            type = FileFormat.Doc;
        }else{
            type=FileFormat.Docx;
        }
        doc.loadFromFile(templateFilePath, type);
        // 正则表达式,匹配所有的占位符 ${}
        Pattern pattern = compile("\\$\\{.*?}");
        // 根据正则表达式获取所有文本
        TextSelection[] allPattern = doc.findAllPattern(pattern);
        // 逐个替换占位符
        for (TextSelection textSelection : allPattern) {
            String tmp = map.get(textSelection.getSelectedText());
            if(tmp!=null){
                System.out.print(textSelection.getSelectedText());
                int res = doc.replace(textSelection.getSelectedText(), tmp, true, true);
                System.out.println(": " + res);
            }
        }
        doc.saveToFile(outFilePath,type);
    }

    /**
     * 读取本地文件
     * 替换word中的图片类型的占位符
     * @param templateFilePath
     * @param outFilePath
     * @param picMap
     */
    public static void insertImages(String templateFilePath,String outFilePath, Map<String,String> picMap){
        FileFormat type;
        if("doc".equals(templateFilePath.substring(templateFilePath.length()-3))){
            type = FileFormat.Doc;
        }else{
            type=FileFormat.Docx;
        }
        Document doc = new Document();
        doc.loadFromFile(templateFilePath, type);
        try {
            Set<String> keySet = picMap.keySet();
            for(String key:keySet){
                File file = new File(picMap.get(key));
                InputStream in = new FileInputStream(file);
                replaceTextToImage(doc, key, in);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        doc.saveToFile(outFilePath,type);
    }

    /**
     * 读取网络图片
     */
    @SneakyThrows
    public static void insertImages2(String templateFilePath, String outFilePath, Map<String,String> picMap){
        FileFormat type;
        if("doc".equals(templateFilePath.substring(templateFilePath.length()-3))){
            type = FileFormat.Doc;
        }else{
            type=FileFormat.Docx;
        }
        Document doc = new Document();
        doc.loadFromFile(templateFilePath, type);
        URL url = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis=null;
        try {
            Set<String> keySet = picMap.keySet();
            for(String key:keySet){
                url = new URL(picMap.get(key));
                httpUrl = (HttpURLConnection) url.openConnection();
                httpUrl.connect();
                bis = new BufferedInputStream(httpUrl.getInputStream());
                replaceTextToImage(doc, key, bis);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            bis.close();
        }
        doc.saveToFile(outFilePath,type);
    }
    /**
     * 按照横纵比缩放图片
     * @param pic: Spire 图片对象
     * @param rate: 缩放比例 0.1<= rate <=10,
     * @author He Peng
     * @date 2021/10/23 9:28
     */
    public static void zoomImage(DocPicture pic, float rate) {
        if (rate < 0.1 || rate > 10) {
            throw new IllegalArgumentException("缩放比例参数的数值范围为:0.1<= rate <=10,不能超过此范围");
        }
        float originalWidth = pic.getWidth();
        float originalHeight = pic.getHeight();
        pic.setWidth(originalWidth * rate);
        pic.setHeight(originalHeight * rate);
    }
    /**
     * 替换Word文档中的同一占位符,为多张不同的图片,每一个占位符对应一张图片,
     * 如果占位符比图片数量多则该占位符会被写为空字符串
     * @param doc: Spire Document
     * @param matchString: 占位符
     * @param picInput: 图片输入流
     * @date 2021/10/22 20:09
     */
    public static void replaceTextToImage(Document doc, String matchString, InputStream picInput) {
        TextSelection[] selections = doc.findAllString(matchString, true, true);
        for (TextSelection selection : selections) {
            if (picInput != null) {
                DocPicture pic = new DocPicture(doc);
                pic.loadImage(picInput);
                zoomImage(pic,0.8f);
                TextRange range = selection.getAsOneRange();
                int index = range.getOwnerParagraph().getChildObjects().indexOf(range);
                range.getOwnerParagraph().getChildObjects().insert(index, pic);
                range.getOwnerParagraph().getChildObjects().remove(range);
            } else {
                // 如果已近没有了图片则将所有占位符替换为空
                doc.replace(selection.getSelectedText(), "", true, true);
                break;
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值