You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by elennick <ev...@accenture.com> on 2009/03/23 18:45:59 UTC

HSLF - HTML to TextBox

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
-- 
View this message in context: http://www.nabble.com/HSLF---HTML-to-TextBox-tp22664650p22664650.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: HSLF - HTML to TextBox

Posted by RakeshYadav <ra...@dbydx.com>.

Hi Yegor,

I am working on Tables Cell formatting. I want to use bold, italic  text
chunks in table cell. Also I want to add lists with text as in a TextBox.
can it be possible in Table Cell?

I have done TextBox's formatting on chunk basis as read from your post. but
cannot apply that on Table Cell.

please suggest

Rakesh Yadav





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


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: HSLF - HTML to TextBox

Posted by elennick <ev...@accenture.com>.
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: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
> 
> 
> 

-- 
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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: HSLF - HTML to TextBox

Posted by rohit_sh1 <ro...@yahoo.ca>.
Hi Yegor,

I was wondering if text formatting this way to content of a TextShape as
well?

Please advise.

Really in need to find an answer.

Thanks

Rohit

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/HSLF-HTML-to-TextBox-tp2306132p5710136.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: HSLF - HTML to TextBox

Posted by Yegor Kozlov <ye...@dinom.ru>.
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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org