textFrame.setText() not keeping original format/font


#1

Does anyone know how to maintain the original format/font/position when manipulating text form textFrame?

(java code below)

...

String orgText = textFrame.getText();

textFrame.setText(orgText+newText);

...

Text seems to be written correctly but the format/font/position is not same are original text.

thank you


#2

Sorry, getText() returns unformatted text and setText() resets all text
to default formatting (inherited from the master).
To maintain old formatting you should manually split the newText string
to lines, then add each line to textFrame’s Paragraphs collection one
by one. To make new paragraph to have same formatting as old one create
it using copy constructor and then change text using Portion’s setText
method.
To learn more about Aspose.Slides text representation see wiki


#3

nikolay,

that worked like a charm… but its very painful. seeems like a lot of work to set a simple text for an existing textframe.

am i taking a wrong approach?

the initial thought was to simply define a formated template ppt with text placeholders and replace with actual content at runtime. is there a better (a simple) approach in creating dynamic ppt?

thanks
joon


#4

The simpliest way to create template is using of master styles. You can
define formatting (including font properties, alignment, offsets and
bullets) for different textholder type (title, body etc) and then
simple use setText() method in your app. Without explicit formatting
PowerPoint will get formatting from the master. Of course, this method
doesn’t suite you if your template must have different formatting for
textboxes of same type.



The complex way isn’t so hard too. You can add something like:


    private static Pattern splitter = Pattern.compile("[\n\r]");
    static public void replaceText(Paragraphs paragraphs, String text)
    {
        try
        {
            while(paragraphs.size() > 1)
                paragraphs.remove(1);
            if(paragraphs.size() == 1)
            {
                Portions portions = paragraphs.get(0).getPortions();
                while(portions.size() > 1)
                    portions.remove(1);
                if(portions.size() == 0)
                    portions.add(new Portion());
            }
            else
            {
                paragraphs.add(new Paragraph());
                paragraphs.get(0).getPortions().add(new Portion());
            }
            
            Paragraph first = paragraphs.get(0);
            
            String[] newParagraphs = splitter.split(text, -1);
            
            first.getPortions().get(0).setText(newParagraphs.length > 0? newParagraphs[0] : "");
            for(int i = 1; i < newParagraphs.length; i++)
            {
                Paragraph newParagraph = new Paragraph(first);
                newParagraph.getPortions().get(0).setText(newParagraphs[ i ]);
                paragraphs.add(newParagraph);
            }
        }
        catch(PptEditException exp)
        {
            // handle exception
        }
    }

and then call replaceText(textFrame.getParagraphs(), newText) anywhere you need it.