This is just what I was looking for. Thanks Yegor!

-Evan


Yegor Kozlov wrote:
> 
> Hi Evan,
> 
> We don't have a nice usermodel API to create multiple rich text runs,
> however it is possible to create such text by 
> manipulating low-level ppt records. An example is below.
> 
> package org.apache.poi.hslf.scratchpad;
> 
> import org.apache.poi.hslf.record.StyleTextPropAtom;
> import org.apache.poi.hslf.record.TextCharsAtom;
> import org.apache.poi.hslf.record.Record;
> import org.apache.poi.hslf.model.*;
> import org.apache.poi.hslf.model.textproperties.TextPropCollection;
> import org.apache.poi.hslf.usermodel.SlideShow;
> import org.apache.poi.hslf.usermodel.RichTextRun;
> 
> import java.awt.*;
> import java.io.*;
> 
> public class TextChunks {
> 
>      public static void main(String[] args) throws Exception {
> 
>          SlideShow ppt = new SlideShow();
>          Slide slide = ppt.createSlide();
>          TextShape shape = new TextBox();
>          shape.setSheet(slide);
> 
>          TextRun run = shape.getTextRun();
>          StyleTextPropAtom styleAtom = null;
>          TextCharsAtom textAtom = null;
>          for(Record r : run.getRecords()){
>              if(r instanceof StyleTextPropAtom) styleAtom =
> (StyleTextPropAtom)r;
>              else if(r instanceof TextCharsAtom) textAtom =
> (TextCharsAtom)r;
>          }
> 
>          styleAtom.getParagraphStyles().clear();
>          styleAtom.getCharacterStyles().clear();
> 
>          StringBuffer text = new StringBuffer();
>          TextPropCollection prProps, chProps;
>          RichTextRun rt;
>          String chunk;
> 
>          //begin building rich text runs
>          chunk = "Apache";
>          text.append(chunk);
>          prProps =
> styleAtom.addParagraphTextPropCollection(chunk.length());
>          chProps =
> styleAtom.addCharacterTextPropCollection(chunk.length());
>          rt = new RichTextRun(shape.getTextRun(), text.length(),
> chunk.length(), null, chProps, false, false);
>          rt.supplySlideShow(ppt);
>          rt.setFontColor(Color.red);
>          rt.setUnderlined(true);
>          rt.setFontSize(10);
> 
>          chunk = " POI";
>          text.append(chunk);
>          prProps =
> styleAtom.addParagraphTextPropCollection(chunk.length());
>          chProps =
> styleAtom.addCharacterTextPropCollection(chunk.length());
>          rt = new RichTextRun(shape.getTextRun(), text.length(),
> chunk.length(), null, chProps, false, false);
>          rt.supplySlideShow(ppt);
>          rt.setFontColor(Color.green);
>          rt.setItalic(true);
>          rt.setFontSize(24);
> 
>          chunk = " is cool";
>          text.append(chunk);
>          prProps =
> styleAtom.addParagraphTextPropCollection(chunk.length());
>          chProps =
> styleAtom.addCharacterTextPropCollection(chunk.length());
>          rt = new RichTextRun(shape.getTextRun(), text.length(),
> chunk.length(), null, chProps, false, false);
>          rt.supplySlideShow(ppt);
>          PPFont font = new PPFont();
>          font.setFontName("Times New Roman");
>          int fontIndex = ppt.addFont(font);
>          rt.setFontIndex(fontIndex);
>          rt.setBold(true);
>          rt.setFontSize(24);
> 
>          chunk = "really!";
>          text.append(chunk);
>          prProps =
> styleAtom.addParagraphTextPropCollection(chunk.length());
>          chProps =
> styleAtom.addCharacterTextPropCollection(chunk.length());
>          rt = new RichTextRun(shape.getTextRun(), text.length(),
> chunk.length(), null, chProps, false, false);
>          rt.supplySlideShow(ppt);
>          rt.setFontIndex(fontIndex);
>          rt.setSuperscript(100);
>          rt.setFontSize(20);
> 
>          //sum of chunk lengths must be text.length+1, add a dummy char to
> the end
>          styleAtom.addParagraphTextPropCollection(1);
>          styleAtom.addCharacterTextPropCollection(1);
> 
>          String txt = text.toString();
>          textAtom.setText(txt);
>         
> shape.getTextRun().buildRichTextRuns(styleAtom.getParagraphStyles(),
> styleAtom.getCharacterStyles(), txt);
>          //end building rich text runs
> 
>          shape.setAnchor(new Rectangle(100, 100, 300, 50));
>          slide.addShape(shape);
> 
>          FileOutputStream out = new FileOutputStream("test.ppt");
>          ppt.write(out);
>          out.close();
> 
>      }
> 
> }
> 
> Regards,
> Yegor
> 
>> I have spent the past few days searching the web, searching this forum
>> and
>> playing around with the Apache POI-HSLF API in an attempt to convert HTML
>> from a WYSIWYG editor into something that looks comparable inside of a
>> PPT
>> TextBox. While I am able to pretty easily convert stuff like line breaks,
>> tabs and bullets to plain text, I am kind of stumped as to how I can
>> display
>> text of different sizes/colors/bolding/underlining/etc inside the same
>> TextBox. I spotted another thread on here that said the addition of
>> multiple
>> RichTextRun objects to a single TextBox was not yet supported in the API
>> which makes me think this might not even be possible.
>> 
>> Does anyone have any examples of something like this or any suggestions
>> as
>> to how it could be implemented? Do I pretty much have to create multiple
>> TextBox objects, each with their own RichTextRun in order to show
>> differently formatted text when creating slides from scratch?
>> 
>> Any input would be appreciated. Thanks!
>> 
>> -Evan
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/HSLF---HTML-to-TextBox-tp22664650p22667432.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to