Java PPT turned into PDF, Chinese garbled solution

Source: Internet
Author: User
Tags gettext

ppt into a PDF, the principle is ppt into a picture, and then use the picture to produce a PDF, the process has a problem, whether it is PPT or pptx, have encountered Chinese garbled, programming box problem, which ppt suffix on-line to find a solution, is to set the font for the unified font, pptx If the page is a Chinese font will not have a problem, if a page has Microsoft ya Black and Arial, will lead to some Chinese box, suspect is the POI processing time, only read the first font, so the number of Chinese characters garbled.

Baidu and Google have been looking for a long time, have seen that Apache official website was said to be a bug (https://bz.apache.org/bugzilla/show_bug.cgi?id=54880), but they replied that it is a font problem, In fact, I think the poi may be able to do their own, read the original font is set to the current font, but the performance should be a lot of consumption, anyway, I guess a lot of people like me to spend a lot of time to find solutions, online almost no ready-made solution. Oneself is also a step-by-step attempt, finally find a solution, PPT format will not say the online find, pptx suffix online I was not found.

Before the problem the pptx turns into a picture:

Resolved after the pptx turns into a picture:

Workaround:

Take each shape, the text into a unified font, the code found on the Internet is not line, I changed the program as follows:

             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 ()); &NBSP;&NBsp;                                                for  ( XSLFTextParagraph  Textpara : txtshape.gettextparagraphs ()  ) {                              list<xslftextrun> textrunlist = textpara.gettextruns ();                              for (xslftextrun textrun: textrunlist)  {                            &nbSp;     textrun.setfontfamily ("Song Body");                              }                         }                     }                 }

The

Complete code is as follows (except for the above-mentioned solutions, most of which are excerpt online):

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 ();                          &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RTRUNS[L]. Setfontindex (1); &NBSP;&NBSP;&NBSP;&NBsp;                           rtruns[l].setfontname ("Song Body");                                                }                       }                                                         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 ();             &nBsp;   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 ("Song Body");                             }                          }                     }                }                 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));    &Nbsp;            slide[i].draw (Graphics);                                  //                 fileoutputstream out = new  fileoutputstream ("Src/main/resources/test" +i+ ". jpg");  //                 javax.imageio.imageio.write (img,  "JPG",  Out);                                                            &nBsp;      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 Configuration:

<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 PPT turned into PDF, Chinese garbled solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.