java PPT 转成PDF 中文乱码解决 - Go语言中文社区

java PPT 转成PDF 中文乱码解决


ppt转成pdf,原理是ppt转成图片,再用图片生产pdf,过程有个问题,不管是ppt还是pptx,都遇到中文乱码,编程方框的问题,其中ppt后缀网上随便找就有解决方案,就是设置字体为统一字体,pptx如果页面是一种中文字体不会有问题,如果一个页面有微软雅黑和宋体,就会导致部分中文方框,怀疑是poi处理的时候,只读取第一种字体,所以导致多个中文字体乱码。

百度和谷歌上,看到apache官网上有人说是bug,但他们回复说是字体问题,这个问题其实poi可以自己做,读取原来字体设置成当前字体,不过性能应该会有很多消耗,估计很多人花费大量时间找解决方案,但网上几乎没有现成的方案。经过多次尝试,最终为大家提供以下解决方法。

问题前的pptx转成图片:

图片1

解决后的pptx转成图片:

图片2

解决方法:

图取每个shape,将文字转成统一的字体,方案如下:

   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上的代码)

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配置:

<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>


本文转自:https://www.oschina.net/blog

更多解决方案请点击查看原文

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/cocacola456/article/details/53925372
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-29 19:08:08
  • 阅读 ( 1503 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢