网站开发模式,全国企业信用公示查询服务平台,潍坊网页制作案例,超市网站设计java实现pdf转word 前言pom文件启动入口过滤器对象ConvertPdfToWordWithFlowableStructure转换实现类 前言
1.java实现pdf转word。 2.纯免费开源。 3.pdf解析完会生成word文件和图片文件夹。 4.无页码限制#xff0c;文本类型生成到word中#xff0c;图片生成到图片文件夹中… java实现pdf转word 前言pom文件启动入口过滤器对象ConvertPdfToWordWithFlowableStructure转换实现类 前言
1.java实现pdf转word。 2.纯免费开源。 3.pdf解析完会生成word文件和图片文件夹。 4.无页码限制文本类型生成到word中图片生成到图片文件夹中。 5.弊端需手动将图片与文本整合成一个word文件。 仅提供一个pdf转word的实现方案代码粗糙老铁轻喷。 jar包地址https://download.csdn.net/download/wyazyf/88917191
pom文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdwy/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.apache.poi/groupIdartifactIdpoi/artifactIdversion4.1.1/version/dependency!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --dependencygroupIdorg.apache.poi/groupIdartifactIdpoi-ooxml/artifactIdversion4.1.1/version/dependency!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --dependencygroupIdcom.itextpdf/groupIdartifactIditextpdf/artifactIdversion5.5.13.1/version/dependency!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian --dependencygroupIdcom.itextpdf/groupIdartifactIditext-asian/artifactIdversion5.2.0/version/dependencydependencygroupIdorg.apache.pdfbox/groupIdartifactIdpdfbox/artifactIdversion2.0.0/version/dependencydependencygroupIdorg.apache.pdfbox/groupIdartifactIdpdfbox-tools/artifactIdversion2.0.0/version/dependencydependencygroupIdorg.apache.pdfbox/groupIdartifactIdjbig2-imageio/artifactIdversion3.0.2/version/dependencydependencygroupIdcom.intellij/groupIdartifactIdforms_rt/artifactIdversion7.0.3/version/dependency/dependenciesrepositoriesrepositoryidalimaven/idnamealiyun maven/nameurlhttp://maven.aliyun.com/nexus/content/groups/public//url/repository/repositories
/project启动入口
import com.sun.deploy.util.StringUtils;
import org.apache.poi.util.StringUtil;import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;/*** author wy* create 2024-02-20 15:57*/
public class test {private JPanel Panel;private JLabel JLabel;private JButton button;private JButton selectButton;private JFileChooser jf ;public static void main(String[] args) {JFrame frame new JFrame(test);JPanel panel new test().Panel;panel.setSize(500,300);frame.setContentPane(panel);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.pack();frame.setSize(400, 200);// frame.setLocation(3);frame.setVisible(true);}public test() {selectButton.addActionListener(new ActionListener() {Overridepublic void actionPerformed(ActionEvent e) {FileFilterTest fileFilternew FileFilterTest (); //创建过滤器对象jfnew JFileChooser();jf.setFileFilter(fileFilter); //对JFileChooser设置过滤器jf.showOpenDialog(null);}});button.addActionListener(new ActionListener() {Overridepublic void actionPerformed(ActionEvent e) {if(jfnull){JOptionPane.showMessageDialog(Panel, 请选择一个pdf文件, 标题,JOptionPane.WARNING_MESSAGE);}File selectedFile jf.getSelectedFile(); // 获取选择的文件String fielPath selectedFile.getPath();if(fielPathnull||(fielPath!null fielPath)){JOptionPane.showMessageDialog(Panel, 请选择一个pdf文件, 标题,JOptionPane.WARNING_MESSAGE);}try {ConvertPdfToWordWithFlowableStructure pdfToWordnew ConvertPdfToWordWithFlowableStructure();pdfToWord.pdfToWordOrPhoto(fielPath);} catch (IOException ioException) {ioException.printStackTrace();}}});}}
过滤器对象
/*** author wy* create 2024-02-20 16:24*/
public class FileFilterTest extends javax.swing.filechooser.FileFilter{public boolean accept(java.io.File f) {if (f.isDirectory())return true;return f.getName().endsWith(.pdf); //设置为选择以.pdf为后缀的文件}public String getDescription(){return .pdf;}
}
ConvertPdfToWordWithFlowableStructure转换实现类
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;import java.awt.image.BufferedImage;
import java.io.*;public class ConvertPdfToWordWithFlowableStructure {public void pdfToWordOrPhoto(String pdfFile) throws IOException {try{// String pdfFile C:\\Users\\Administrator\\Desktop\\1.pdf;PDDocument doc PDDocument.load(new File(pdfFile));int pagenumber doc.getNumberOfPages();pdfFile pdfFile.substring(0, pdfFile.lastIndexOf(.));String fileName pdfFile .doc;File file new File(fileName);if (!file.exists()){file.createNewFile();}FileOutputStream fos new FileOutputStream(fileName);Writer writer new OutputStreamWriter(fos, UTF-8);PDFTextStripper stripper new PDFTextStripper();stripper.setSortByPosition(true);// 排序stripper.setStartPage(1);// 设置转换的开始页stripper.setEndPage(pagenumber);// 设置转换的结束页stripper.writeText(doc, writer);writer.close();System.out.println(pdf文字转换word成功);//开始下载图片PDFRenderer pdfRenderer new PDFRenderer(doc);for(int i 0;ipagenumber;i){// 设置页数首页从0开始、每英寸点数、图片类型BufferedImage bufferedImage pdfRenderer.renderImageWithDPI(i, 300, ImageType.RGB);String currentCoverPath pdfFile / i . png;// 创建图片文件对象File file1 new File(currentCoverPath);if (!file1.getParentFile().exists()) {file1.getParentFile().mkdirs();}if (!file1.exists()) {file1.createNewFile();}else{file1.delete();file1.createNewFile();}// 将图片写入到图片对象中ImageIOUtil.writeImage(bufferedImage, currentCoverPath, 300);}doc.close();System.out.println(pdf图片下载成功);}catch (IOException e){e.printStackTrace();}}
}
文章转载自: http://www.morning.nlhcb.cn.gov.cn.nlhcb.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.lsmgl.cn.gov.cn.lsmgl.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.rkxqh.cn.gov.cn.rkxqh.cn http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.tymwx.cn.gov.cn.tymwx.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.kqpsj.cn.gov.cn.kqpsj.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.bzgpj.cn.gov.cn.bzgpj.cn http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.pnmtk.cn.gov.cn.pnmtk.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.qctsd.cn.gov.cn.qctsd.cn http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.qjzgj.cn.gov.cn.qjzgj.cn http://www.morning.wqkfm.cn.gov.cn.wqkfm.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.ctwwq.cn.gov.cn.ctwwq.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.jopebe.cn.gov.cn.jopebe.cn http://www.morning.bcdqf.cn.gov.cn.bcdqf.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.jczjf.cn.gov.cn.jczjf.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.rbmnq.cn.gov.cn.rbmnq.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.flchj.cn.gov.cn.flchj.cn http://www.morning.bpmft.cn.gov.cn.bpmft.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.ghslr.cn.gov.cn.ghslr.cn http://www.morning.mgkb.cn.gov.cn.mgkb.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.phxdc.cn.gov.cn.phxdc.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.yfnjk.cn.gov.cn.yfnjk.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn http://www.morning.bgzgq.cn.gov.cn.bgzgq.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.spfh.cn.gov.cn.spfh.cn http://www.morning.pluimers.cn.gov.cn.pluimers.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.pmhln.cn.gov.cn.pmhln.cn http://www.morning.kqbwr.cn.gov.cn.kqbwr.cn