激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - java實(shí)現(xiàn)PPT轉(zhuǎn)化為PDF

java實(shí)現(xiàn)PPT轉(zhuǎn)化為PDF

2021-05-08 10:26opzoonzhuzhengke Java教程

這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)PPT轉(zhuǎn)化為PDF的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

jacob的方法,足可以解決這個(gè)問題,但是我既然以前曾經(jīng)做過報(bào)表,就想嘗試不同的方法。

jacob是一座連接java和微軟的橋,所有的解析由微軟解析。poi是沒有微軟解析的那么原汁原味的,所以如果要求高的話,還是使用jacob。

大致思路很簡(jiǎn)單,將ppt先轉(zhuǎn)化為圖片,然后將圖片寫入pdf。轉(zhuǎn)化圖片是用poi,操作pdf使用itex。不過這個(gè)方法的bug就是轉(zhuǎn)化圖片的poi效果不是很好。

導(dǎo)入的包分別是:itextpdf-5.1.3.jar,poi-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar。

然后貼代碼了:

代碼沒有進(jìn)行參數(shù)統(tǒng)一,寫兩個(gè)方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.zzk.cn;
 
import java.awt.dimension;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.awt.color;
import java.awt.dimension;
import java.awt.graphics2d;
import java.awt.geom.rectangle2d;
import java.awt.image.bufferedimage;
import org.apache.poi.hslf.model.textrun;
import org.apache.poi.hslf.record.slide;
import org.apache.poi.hslf.usermodel.richtextrun;
import org.apache.poi.hslf.usermodel.slideshow;
 
public class ppttoimage {
  public static void main(string[] args) {
    // 讀入ppt文件
    file file = new file("d:/書本jvm總結(jié)7-9.ppt");
    doppttoimage(file);
  }
 
  public static boolean doppttoimage(file file) {
    boolean isppt = checkfile(file);
    if (!isppt) {
      system.out.println("你指定的文件不是ppt文檔!");
      return false;
    }
    try {
      fileinputstream is = new fileinputstream(file);
      slideshow ppt = new slideshow(is);
      is.close();
      dimension pgsize = ppt.getpagesize();
      org.apache.poi.hslf.model.slide[] slide = ppt.getslides();
      for (int i = 0; i < slide.length; i++) {
        system.out.print("第" + i + "頁。");
        if (slide[i].getnotessheet() != null
            && slide[i].getnotessheet().gettextruns() != null) {
          // 獲取第一個(gè)備注
          system.out.println("備注:"
              + slide[i].getnotessheet().gettextruns()[0]
                  .gettext());
        }
        textrun[] truns = slide[i].gettextruns();
        for (int k = 0; k < truns.length; k++) {
          richtextrun[] rtruns = truns[k].getrichtextruns();
          for (int l = 0; l < rtruns.length; l++) {
            rtruns[l].setfontindex(1);
            rtruns[l].setfontname("宋體");
            // 獲取文本列表
            system.out.println(rtruns[l].gettext());
          }
        }
        bufferedimage img = new bufferedimage(pgsize.width,
            pgsize.height, bufferedimage.type_int_rgb);
        graphics2d graphics = img.creategraphics();
        graphics.setpaint(color.white);
        graphics.fill(new rectangle2d.float(0, 0, pgsize.width,
            pgsize.height));
        slide[i].draw(graphics);
        // 這里設(shè)置圖片的存放路徑和圖片的格式(jpeg,png,bmp等等),注意生成文件路徑
        fileoutputstream out = new fileoutputstream("d:/testimage/pict_"
            + (i + 1) + ".jpeg");
        javax.imageio.imageio.write(img, "jpeg", out);
        out.close();
      }
      system.out.println("ok");
      return true;
    } catch (filenotfoundexception e) {
      system.out.println(e);
    } catch (ioexception e) {
      e.printstacktrace();
    }
    return false;
  }
 
  // function 檢查文件是否為ppt
  public static boolean checkfile(file file) {
    boolean isppt = false;
    string filename = file.getname();
    string suffixname = null;
    if (filename != null && filename.indexof(".") != -1) {
      suffixname = filename.substring(filename.indexof("."));
      if (suffixname.equals(".ppt")) {
        isppt = true;
      }
      return isppt;
    } else {
      return isppt;
    }
  }
}

第二段代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.zzk.cn;
 
import java.io.fileoutputstream;
import java.io.ioexception;
 
import com.itextpdf.text.document;
import com.itextpdf.text.documentexception;
import com.itextpdf.text.image;
import com.itextpdf.text.pdf.pdfwriter;
 
public class imagetopdf {
   
  public static void main(string[] args) {
     
    system.out.println("chapter 6 example 3: using a relative path for html");
     
    // step 1: creation of a document-object
    document document = new document();
     
    try {
       
      // step 2:
      // we create a writer that listens to the document
      // and directs a pdf-stream to a file
       
      pdfwriter.getinstance(document, new fileoutputstream("d:/測(cè)試圖片.pdf"));
     // htmlwriter writer = htmlwriter.getinstance(document, new fileoutputstream("chap0603.html"));
       
     // writer.setimagepath("../../images/kerstmis/");
       
      // step 3: we open the document
      document.open();
       
      for(int i=1;i<=7;i++) {
      // step 4: we add content
      image jpg = image.getinstance("d:/testimage/pict_"+i+".jpeg");
      jpg.scalepercent(50);
      document.add(jpg);
      }
       
    }
    catch(documentexception de) {
      system.err.println(de.getmessage());
    }
    catch(ioexception ioe) {
      system.err.println(ioe.getmessage());
    }
     
    // step 5: we close the document
    document.close();
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/opzoonzhuzhengke/article/details/7609833

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久最新视频 | 日本中文高清 | 激情网站在线观看 | 日本一级黄色大片 | 爽成人777777婷婷 | 性aaa| 国产精选在线 | 久久精品视频16 | 美女黄影院 | 斗罗破苍穹在线观看免费完整观看 | 日韩大片在线永久观看视频网站免费 | 羞羞羞网站 | 国产手机av在线 | 亚洲国产成人久久一区www妖精 | 成人在线免费视频播放 | 91快色| 国产精品视频免费在线观看 | 免费国产不卡午夜福在线 | 免费黄色在线 | 国产精品中文在线 | 久久久精品视频国产 | 国产91一区 | 蜜桃欧美性大片免费视频 | 黄色影院一级片 | 国产亚洲精品视频中文字幕 | 色综合久久久久综合99 | 久久超| 国产一区二区视频在线播放 | 欧美成年性h版影视中文字幕 | 国产亚洲精品成人 | 亚洲一级网站 | 中文字幕电影免费播放 | 91精品国产网站 | 亚洲一区二区三区精品在线观看 | 久久久国产精品电影 | 久久艹一区 | 日本高清电影在线播放 | 久草在线视频在线 | h视频在线免费观看 | 欧美男女爱爱视频 | 国产一区精品在线观看 |