WORD转HTML,PDF转图片小记

public class DocumentConvertTest {

    public static void main(String argv[]) {
        try {
//            docConvert2Html("E:/文档/gan.doc", "E:/文档/", "1.html", "UTF-8");
            pdfConvert2Img("E:/文档/", "zhy.pdf", 50);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void pdfConvert2Img(String filePath, String fileName, int dpi) throws IOException {
        PDDocument document = PDDocument.load(new FileInputStream(new File(filePath + fileName)));
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        String savePath = filePath + fileName.substring(0, fileName.indexOf(".")) + "_2_images/";
        File savePathDir = new File(savePath);
        if (!savePathDir.exists()) {
            savePathDir.mkdirs();
        }
        for (int page = 0; page < document.getNumberOfPages(); ++page) {
                BufferedImage bim = pdfRenderer.renderImageWithDPI(page, dpi, ImageType.RGB);
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                Iterator iter = ImageIO.getImageWritersBySuffix("jpg");
                ImageWriter writer = (ImageWriter) iter.next();
//                File outFile = new File(savePath + page + ".jpg");
                ImageOutputStream outImage = ImageIO.createImageOutputStream(bs);
                writer.setOutput(outImage);
                writer.write(new IIOImage(bim, null, null));
                writer.dispose();
                byte[] bArray = bs.toByteArray();
                InputStream is = new ByteArrayInputStream(bArray);
        }
        document.close();
    }

    public static void docConvert2Html(String fileName, final String outPutFilePath, final String outPutFileName, String encode)
            throws TransformerException, IOException,
                   ParserConfigurationException {
        HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));
        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
                DocumentBuilderFactory.newInstance().newDocumentBuilder()
                                      .newDocument());

        final String imgPath = outPutFilePath + outPutFileName.substring(0, outPutFileName.indexOf(".")) + "_images/";
        wordToHtmlConverter.setPicturesManager(new PicturesManager() {
            public String savePicture(byte[] content,
                                      PictureType pictureType, String suggestedName,
                                      float widthInches, float heightInches) {
                return imgPath + suggestedName;
            }
        });
        wordToHtmlConverter.processDocument(wordDocument);
        //save pictures
        List pics = wordDocument.getPicturesTable().getAllPictures();
        if (pics != null) {
            for (int i = 0; i < pics.size(); i++) {
                Picture pic = (Picture) pics.get(i);
                System.out.println(imgPath + pic.suggestFullFileName());
                File imgPathDir = new File(imgPath);
                if (!imgPathDir.exists()) {
                    imgPathDir.mkdirs();
                }
                pic.writeImageContent(new FileOutputStream(imgPath + pic.suggestFullFileName()));
            }
        }
        Document htmlDocument = wordToHtmlConverter.getDocument();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DOMSource domSource = new DOMSource(htmlDocument);
        StreamResult streamResult = new StreamResult(out);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, encode);
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.METHOD, "html");
        serializer.transform(domSource, streamResult);
        out.close();
        writeFile(new String(out.toByteArray()), outPutFilePath + outPutFileName, encode);
    }

    public static void writeFile(String content, String name, String encode) {
        FileOutputStream fos = null;
        BufferedWriter bw = null;
        try {
            File file = new File(name);
            fos = new FileOutputStream(file);
            bw = new BufferedWriter(new OutputStreamWriter(fos, encode));
            bw.write(content);
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (bw != null)
                    bw.close();
                if (fos != null)
                    fos.close();
            } catch (IOException ie) {
            }
        }
    }
}


maven依赖:PDFbox,POI的HWPFDocument

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.13</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.13</version>
		</dependency>
	
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml-schemas</artifactId>
			<version>3.13</version>
		</dependency>

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-scratchpad</artifactId>
			<version>3.13</version>
		</dependency>

		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>pdfbox</artifactId>
			<version>2.0.0-RC3</version>
		</dependency>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值