package com.ag.edi;

import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.*;
import java.util.List;

/**
 * Created by shaoqiang on 16/6/20.
 */
public class EdiBean implements  java.io.Serializable{

    public List logerror=null;
    public InputStream datain=null;

    public List getLogerror() {
        return logerror;
    }
    public void setLogerror(List data) {
        this.logerror = data;
    }

    public InputStream getDatain() {
        return datain;
    }

    public void setDatain(InputStream datain) {
        this.datain = datain;
    }


    /**
     * 读取pdf文件内容
     * @return
     * @throws IOException
     */
    public static void main(String args[]) throws IOException {
        String result = null;
        FileInputStream is = null;
        PDDocument document = null;
        try {
           is = new FileInputStream("/Users/running/work/work2019/威海ocr识别/DB_aabfejjffabc0x01FA.pdf");
       //     is = new FileInputStream("/Users/shaoqiang/work/work2019/威海ocr识别/COSU 6201279260 BC.PDF");
            PDFParser parser = new PDFParser(new RandomAccessBuffer(is));
            parser.parse();
            document = parser.getPDDocument();
            PDFTextStripper stripper = new PDFTextStripper();
            result = stripper.getText(document);
            System.out.println(result);
           // pdfToWordOfText();
        } finally {
            if (is != null) {
                is.close();
            }
            if (document != null) {
                document.close();
            }
        }
        //return result;
    }



    public static  void pdfToWordOfText(){
        try {
            //pdf文件路径
            String filePath = "/Users/shaoqiang/work/work2019/威海ocr识别/HL-83014252 MXZLO BC 1ST UPDATE.PDF";
            File fdf = new File(filePath);
            //生成的word的文件路径
            String wordPath = "/Users/shaoqiang/work/work2019/威海ocr识别/ap.docx";
            //通过文件名加载文档
            PDDocument doc = PDDocument.load(fdf);
            //获取文档的页数
            int pageNumber = doc.getNumberOfPages();
            //剥离器（读取pdf文件）
            PDFTextStripper  stripper = new PDFTextStripper();
            //排序
            stripper.setSortByPosition(true);
            //设置要读取的起始页码
            stripper.setStartPage(1);
            //设置要读取的结束页码
            stripper.setEndPage(pageNumber);
            File word = new File(wordPath);
            if(!word.exists()){
                word.createNewFile();
            }
            //文件输出流
            FileOutputStream fos = new FileOutputStream(word);
            //
            Writer  writer = new OutputStreamWriter(fos, "utf-8");
            stripper.writeText(doc, writer);
            writer.close();
            fos.close();
            System.out.println("转码完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
