1. 程式人生 > >PDF轉圖片以及轉html

PDF轉圖片以及轉html

type 電子書 技術分享 tor 實現 img image() lose bag

嘿,筆者就是本著有意思的心態。其實實現的原理與ppt轉html的一致。先轉成圖片,然後把圖片呈現在html上。直接貼代碼嘍。

        package com.css.java.learning.massbag;

        import java.awt.image.BufferedImage;
        import java.io.File;
        import java.io.FileOutputStream;
        import java.util.ArrayList;
        import java.util.Iterator;
        import java.util.List;
        import javax.imageio.IIOImage;
        import javax.imageio.ImageIO;
        import javax.imageio.ImageWriter;
        import javax.imageio.stream.ImageOutputStream;
        import org.apache.pdfbox.pdmodel.PDDocument;
        import org.apache.pdfbox.pdmodel.PDPage;

        /**
         *PDF 轉html 
         * @author Red_Ant
         * 20180925
         */
        public class PdfToHtml {
            @SuppressWarnings("deprecation")
            private static void pdfToHtml(String sourcePath, String outPath){
                List<String> imgList = new ArrayList<String>();
                try {
                    PDDocument doc = PDDocument.load(sourcePath);
                    int pageCount = doc.getPageCount(); 
                    System.out.println("總共多少頁?" + pageCount); 
                    List pages = doc.getDocumentCatalog().getAllPages(); 
                    for(int i=0;i<pages.size();i++){
                            PDPage page = (PDPage)pages.get(i); 
                            BufferedImage image = page.convertToImage(); 
                            Iterator iter = ImageIO.getImageWritersBySuffix("jpg"); 
                            ImageWriter writer = (ImageWriter)iter.next(); 
                            String imgName = File.separator + "20180925_pdf" + File.separator +i+".jpg";
                            File folder = new File(outPath + File.separator + "20180925_pdf");  //先創建文件夾
                            folder.mkdirs();
                            File outFile = new File(outPath + imgName); //再創建文件
                            imgList.add("20180925_pdf" + File.separator +i+".jpg");
                            outFile.createNewFile();
                            FileOutputStream out = new FileOutputStream(outFile); 
                            ImageOutputStream outImage = ImageIO.createImageOutputStream(out); 
                            writer.setOutput(outImage); 
                            writer.write(new IIOImage(image,null,null)); 
                    }
                    doc.close();
                } catch (Exception e) {
                    e.printStackTrace();
                } 
                PptToHtml.createPPTHtml(outPath, imgList, sourcePath);
            }

            public static void main(String[] args) {
                pdfToHtml("D:\\red_ant_file\\20180925\\20180925_pdf\\西點烘焙百科全書電子書.pdf", 
                        "D:\\red_ant_file\\20180925\\20180925_pdf");
            }
        }

實現效果

技術分享圖片

技術分享圖片

技術分享圖片

PDF轉圖片以及轉html