jacob转换PDF,pdf.js浏览

之前项目上做在线浏览的时候做的,不过后来因为某些原因,没用

Word、txt亲测无误,PPT有错没调试,Excel可以转换但是转换后格式有问题

用jacob转换PDF,用pdf.js在线浏览PDF

jacob,以及pdf.js下载地址(可以自行网上百度):

public static String pdf(String fileid)
  {
    String filename = null;
    String realfilename = null;
    String sourcepath = null;
    
    String inFilePath = sourcepath + realfilename;
    String[] name = filename.split("[.]");
    filename = name[0].replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "");
    filename = getFilePinyin(filename);

    System.out.println(inFilePath);

    String newfilename = filename + ".pdf";

    String outFilePath = sourcepath.replaceAll("\\\\", "/") + "pdf/" + 
      newfilename;

    File createPath = new File(sourcepath + "\\pdf");

    if (!createPath.isDirectory()) {
      createPath.mkdir();
    }

    File file = new File(outFilePath);
    if (!file.exists()) {
      System.out.println("pdf文件不存在");
      System.out.println("outFilePath:" + outFilePath);
      inFilePath = inFilePath.replaceAll("\\\\", "/");
      convert2PDF(inFilePath, outFilePath);
    }

    return newfilename;
  }
//获取文件类型
  public static String getFileSufix(String fileName)
  {
    int splitIndex = fileName.lastIndexOf(".");
    return fileName.substring(splitIndex + 1);
  }

//将文件名装换为拼音,因为pdf.js无法识别中文
  public static String getFilePinyin(String fileName)
  {
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    t3.setVCharType(HanyuPinyinVCharType.WITH_V);
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < fileName.length(); i++) {
      if (Character.toString(fileName.charAt(i)).matches(
        "[\\u4E00-\\u9FA5]+")) {
        try {
          sb.append(net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(fileName
            .charAt(i), t3)[
            0]);
        }
        catch (BadHanyuPinyinOutputFormatCombination e) {
          e.printStackTrace();
        }

      }
      else
      {
        sb.append(fileName.charAt(i));
      }
    }
    fileName = sb.toString();
    return fileName;
  }
  public static void convert2PDF(String inputFile, String pdfFile) //判断什么类型跳转不同的方法
  {
    String kind = getFileSufix(inputFile);
    File file = new File(inputFile);
    if (!file.exists()) {
      System.out.println("文件不存在");
    }
    if (kind.equals("pdf")) {
      File file1 = new File(inputFile);
      File file2 = new File(pdfFile);
      try {
        System.out.println("pdf");
        forChannel(file1, file2);
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
    if ((kind.equals("doc")) || (kind.equals("docx")) || (kind.equals("txt")) || 
      (kind.equals("wps"))) {
      System.out.println("doc");
      word2PDF(inputFile, pdfFile);
    } else if ((kind.equals("ppt")) || (kind.equals("pptx"))) {
      ppt2PDF(inputFile, pdfFile);
    } else if ((kind.equals("xls")) || (kind.equals("xlsx"))) {
      Ex2PDF(inputFile, pdfFile);
    }
  }

//Word装换pdf
  public static void word2PDF(String inFilePath, String outFilePath)
  {
    ActiveXComponent app = null;

    System.out.println("开始转换...");

    long start = System.currentTimeMillis();
    System.out.println("开始时间:" + start);
    try {
      System.out.println("进入try");

      System.out.println("开始加载组件");
      app = new ActiveXComponent("Word.Application");
      System.out.println("加载组件完毕");

      Dispatch documents = app.getProperty("Documents").toDispatch();
      System.out.println("打开文件: " + inFilePath);

      Dispatch document = Dispatch.call(documents, "Open", inFilePath, 
        Boolean.valueOf(false), Boolean.valueOf(true)).toDispatch();

      File target = new File(outFilePath);
      if (target.exists()) {
        target.delete();
      }
      System.out.println("另存为: " + outFilePath);

      Dispatch.call(document, "SaveAs", outFilePath, Integer.valueOf(17));

      Dispatch.call(document, "Close", Boolean.valueOf(false));

      long end = System.currentTimeMillis();
      System.out.println("转换成功,用时:" + (end - start) + "ms");
    } catch (Exception e) {
      System.out.println("转换失败" + e.getMessage());
    }
    finally {
      app.invoke("Quit", 0);
    }
  }

//将中文名的PDF另存为拼音名的pdf,因为pdf.js无法识别中文
  public static long forChannel(File f1, File f2)
    throws Exception
  {
    long time = new Date().getTime();
    int length = 2097152;
    System.out.println("length");
    FileInputStream in = new FileInputStream(f1);
    FileOutputStream out = new FileOutputStream(f2);
    FileChannel inC = in.getChannel();
    FileChannel outC = out.getChannel();
    ByteBuffer b = null;
    while (true) {
      if (inC.position() == inC.size()) {
        inC.close();
        outC.close();
        return new Date().getTime() - time;
      }
      if (inC.size() - inC.position() < length)
        length = (int)(inC.size() - inC.position());
      else
        length = 2097152;
      b = ByteBuffer.allocateDirect(length);
      inC.read(b);
      b.flip();
      outC.write(b);
      outC.force(false);
    }
  }

//Excel装换PDF,格式有问题,列太多打印不开会分到好几页
  public static String Ex2PDF(String inFilePath, String outFilePath)
  {
    ComThread.InitSTA(true);
    System.out.println("开始转换...");

    long start = System.currentTimeMillis();
    System.out.println(start);
    ActiveXComponent ax = new ActiveXComponent("Excel.Application");
    try {
      ax.setProperty("Visible", new Variant(false));
      ax.setProperty("AutomationSecurity", new Variant(3));
      Dispatch excels = ax.getProperty("Workbooks").toDispatch();

      Dispatch excel = Dispatch.invoke(
        excels, 
        "Open", 
        1, 
        new Object[] { inFilePath, new Variant(false), 
        new Variant(false) }, new int[9]).toDispatch();

      Dispatch.invoke(excel, "ExportAsFixedFormat", 1, 
        new Object[] { new Variant(0), 
        outFilePath, new Variant(0) }, 
        new int[1]);
      Dispatch.call(excel, "Close", new Variant(false));

      long end = System.currentTimeMillis();
      System.out.println(end);
      System.out.println("转换成功,用时:" + (end - start) + "ms");
      if (ax != null) {
        ax.invoke("Quit", new Variant[0]);
        ax = null;
      }
      ComThread.Release();
      return ""; } catch (Exception es) {
    }
    return es.toString();
  }

//ppt,报错可能缺少东西没有调试
  public static void ppt2PDF(String inputFile, String pdfFile)
  {
    ActiveXComponent app = null;
    Dispatch ppt = null;
    try {
      ComThread.InitSTA();
      app = new ActiveXComponent("PowerPoint.Application");

      Dispatch ppts = app.getProperty("Presentations").toDispatch();
      ppt = Dispatch.call(ppts, "Open", inputFile, Boolean.valueOf(true), 
        Boolean.valueOf(true), 
        Boolean.valueOf(false))
        .toDispatch();
      Dispatch.call(ppt, "SaveAs", pdfFile, Integer.valueOf(32));
    } catch (ComFailException e) {
      System.out.println(e.getMessage());
    } catch (Exception localException) {
    }
    finally {
      if (ppt != null) {
        Dispatch.call(ppt, "Close");
      }
      if (app != null) {
        app.invoke("Quit");
      }
      ComThread.Release();
    }
  }

猜你喜欢

转载自blog.csdn.net/lycoris_red/article/details/80944270