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

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - java實(shí)現(xiàn)PPT轉(zhuǎn)PDF出現(xiàn)中文亂碼問題的解決方法

java實(shí)現(xiàn)PPT轉(zhuǎn)PDF出現(xiàn)中文亂碼問題的解決方法

2020-01-16 16:32lijiao JAVA教程

這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)PPT轉(zhuǎn)PDF出現(xiàn)中文亂碼問題的解決方法,進(jìn)行了詳細(xì)的問題分析,需要的朋友可以參考下

ppt轉(zhuǎn)成pdf,原理是ppt轉(zhuǎn)成圖片,再用圖片生產(chǎn)pdf,過程有個(gè)問題,不管是ppt還是pptx,都遇到中文亂碼,編程方框的問題,其中ppt后綴網(wǎng)上隨便找就有解決方案,就是設(shè)置字體為統(tǒng)一字體,pptx如果頁面是一種中文字體不會(huì)有問題,如果一個(gè)頁面有微軟雅黑和宋體,就會(huì)導(dǎo)致部分中文方框,懷疑是poi處理的時(shí)候,只讀取第一種字體,所以導(dǎo)致多個(gè)中文字體亂碼。

百度和谷歌都找了很久,有看到說apache官網(wǎng)有人說是bug,但他們回復(fù)說是字體問題,這個(gè)問題其實(shí)我覺得poi可能可以自己做,讀取原來字體設(shè)置成當(dāng)前字體,不過性能應(yīng)該會(huì)有很多消耗,反正我估計(jì)很多人跟我一樣花費(fèi)大量時(shí)間找解決方案,網(wǎng)上幾乎沒有現(xiàn)成的方案。自己也是一步步嘗試,最終找到解決辦法,ppt格式的就不說了網(wǎng)上找得到,pptx后綴的網(wǎng)上我是沒找到。

問題前的pptx轉(zhuǎn)成圖片:

java實(shí)現(xiàn)PPT轉(zhuǎn)PDF出現(xiàn)中文亂碼問題的解決方法

解決后的pptx轉(zhuǎn)成圖片:

java實(shí)現(xiàn)PPT轉(zhuǎn)PDF出現(xiàn)中文亂碼問題的解決方法

解決方法:
讀取每個(gè)shape,將文字轉(zhuǎn)成統(tǒng)一的字體,網(wǎng)上找到的那段代碼不可行,我自己改的方案如下:       

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for( XSLFShape shape : slide[i].getShapes() ){
  if ( shape instanceof XSLFTextShape ){
  XSLFTextShape txtshape = (XSLFTextShape)shape ;
  System.out.println("txtshape" + (i+1) + ":" + txtshape.getShapeName());
  System.out.println("text:" +txtshape.getText());
  
  for ( XSLFTextParagraph textPara : txtshape.getTextParagraphs() ){
  List<XSLFTextRun> textRunList = textPara.getTextRuns();
  for(XSLFTextRun textRun: textRunList) {
  textRun.setFontFamily("宋體");
  }
  }
  }
 }

完整代碼如下(除了以上自己的解決方案,大部分是stackoverflow上的代碼):

?
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
public static void convertPPTToPDF(String sourcepath, String destinationPath, String fileType) throws Exception {
 FileInputStream inputStream = new FileInputStream(sourcepath);
 double zoom = 2;
 AffineTransform at = new AffineTransform();
 at.setToScale(zoom, zoom);
 Document pdfDocument = new Document();
 PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(destinationPath));
 PdfPTable table = new PdfPTable(1);
 pdfWriter.open();
 pdfDocument.open();
 Dimension pgsize = null;
 Image slideImage = null;
 BufferedImage img = null;
 if (fileType.equalsIgnoreCase(".ppt")) {
 SlideShow ppt = new SlideShow(inputStream);
 inputStream.close();
 pgsize = ppt.getPageSize();
 Slide slide[] = ppt.getSlides();
 pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
 pdfWriter.open();
 pdfDocument.open();
 for (int i = 0; i < slide.length; i++) {
  
 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++){
//  int index = rtruns[l].getFontIndex();
//  String name = rtruns[l].getFontName();
  rtruns[l].setFontIndex(1);
  rtruns[l].setFontName("宋體"); 
  }
 }
  
  
 img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
 Graphics2D graphics = img.createGraphics();
 graphics.setTransform(at);
 
 graphics.setPaint(Color.white);
 graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
 slide[i].draw(graphics);
 graphics.getPaint();
 slideImage = Image.getInstance(img, null);
 table.addCell(new PdfPCell(slideImage, true));
 }
 }
 if (fileType.equalsIgnoreCase(".pptx")) {
 XMLSlideShow ppt = new XMLSlideShow(inputStream);
 pgsize = ppt.getPageSize();
 XSLFSlide slide[] = ppt.getSlides();
 pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
 pdfWriter.open();
 pdfDocument.open();
 
 
 for (int i = 0; i < slide.length; i++) {
 for( XSLFShape shape : slide[i].getShapes() ){
  if ( shape instanceof XSLFTextShape ){
  XSLFTextShape txtshape = (XSLFTextShape)shape ;
  // System.out.println("txtshape" + (i+1) + ":" + txtshape.getShapeName());
  //System.out.println("text:" +txtshape.getText());
  
  for ( XSLFTextParagraph textPara : txtshape.getTextParagraphs() ){
  List<XSLFTextRun> textRunList = textPara.getTextRuns();
  for(XSLFTextRun textRun: textRunList) {
  textRun.setFontFamily("宋體");
  }
  }
  }
 }
 img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
 Graphics2D graphics = img.createGraphics();
 graphics.setTransform(at);
 graphics.setPaint(Color.white);
 graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
 slide[i].draw(graphics);
  
  
// FileOutputStream out = new FileOutputStream("src/main/resources/test"+i+".jpg");
// javax.imageio.ImageIO.write(img, "jpg", out);
  
  
  
 graphics.getPaint();
 slideImage = Image.getInstance(img, null);
 table.addCell(new PdfPCell(slideImage, true));
 }
 }
 pdfDocument.add(table);
 pdfDocument.close();
 pdfWriter.close();
 System.out.println("Powerpoint file converted to PDF successfully");
 }

maven配置:

?
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
<dependency>
 <groupId>org.apache.poi</groupId>
 <artifactId>poi</artifactId>
 <!-- <version>3.13</version> -->
 <version>3.9</version>
 </dependency>
 <dependency>
 <groupId>org.apache.poi</groupId>
 <artifactId>poi-ooxml</artifactId>
 <!-- <version>3.10-FINAL</version> -->
 <version>3.9</version>
 </dependency>
 
 <dependency>
 <groupId>com.itextpdf</groupId>
 <artifactId>itextpdf</artifactId>
 <version>5.5.7</version>
 </dependency>
 
 <dependency>
 <groupId>com.itextpdf.tool</groupId>
 <artifactId>xmlworker</artifactId>
 <version>5.5.7</version>
 </dependency>
 <dependency>
 <groupId>org.apache.poi</groupId>
 <artifactId>poi-scratchpad</artifactId>
 <!-- <version>3.12</version> -->
 <version>3.9</version>
 </dependency>

上面就是為大家分享的java實(shí)現(xiàn)PPT轉(zhuǎn)PDF出現(xiàn)中文亂碼問題的解決方法,希望對(duì)大家的學(xué)習(xí)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产98色在线| 免费看成年人网站 | 污污网站入口 | 欧美一级做a | 女人a级毛片 | 在线视频a | 久久精品久 | 斗罗破苍穹在线观看免费完整观看 | www噜噜偷拍在线视频 | 欧美性生交xxxxx免费观看 | 午夜啪视频 | 精品在线观看一区 | 亚洲成a人在线 | 欧美精品欧美极品欧美激情 | 久久九九热re6这里有精品 | 一级毛片a级| 欧美一区二区黄 | 一二区成人影院电影网 | 亚洲一区二区三区在线看 | ⅴideo裸体秀hd | 欧美精品一区二区三区四区 | 成人欧美在线观看 | 国产成人av免费观看 | 色婷婷久久久 | 新久草在线视频 | 一级黄色免费观看 | 亚洲精品欧美二区三区中文字幕 | 男女无套免费视频 | 精品国产91久久久久久浪潮蜜月 | 国产精品免费看 | 成人三级视频在线观看 | 亚洲精品久久久久久久久久久 | 日韩中字在线 | 中国毛片在线观看 | 久久精品一区二区三区不卡牛牛 | 国产羞羞视频在线免费观看 | 日韩av在线网址 | aa国产视频一区二区 | www.99热视频| 嫩呦国产一区二区三区av | 成人免费午夜视频 |