Hello everyone,

I have a ppt file as template and I want to replace some parts of text (tagged 
between < and >)  of the text in it. Therefore I loop over each slide, get the 
TextRuns and RichTextRuns, and call richTextRun.setText(text) for updating the 
presentation and keeping the styles.

At the end, I write the SlideShow to a new file. But when I open the output 
with PowerPoint, I get an error...
I use POI 3.1-beta1 and PowerPoint2003 SP3 on WindowsXP.

Am I doing something wrong? Is there another method to setText and keep the 
existing styles ?



Here is my java code for creating the new file:

try {
            SlideShow ppt = POIUtil.openSlideShow(TEMPLATE);
            //get the slides
            Slide[] slides = ppt.getSlides();
            if (slides == null || slides.length == 0) {
                System.out.println(TEMPLATE + " doesn't contains any slide.");
                return;
            }
            Pattern pattern = Pattern.compile(REGEXP);
            final Map<String, Object> map = TestData.getTestData();
            Matcher matcher = null;
            StringBuilder sb = new StringBuilder();
            //iterate all slides
            for (Slide slide : slides) {
                TextRun[] textRuns = slide.getTextRuns();
                if (textRuns == null || textRuns.length == 0) {
                    //no text runs. go to next slide.
                    continue;
                }
                System.out.println("slide number:" + slide.getSlideNumber());
                for (TextRun run : textRuns) {
                    RichTextRun[] richTextRuns = run.getRichTextRuns();
                    if (richTextRuns == null || richTextRuns.length == 0) {
                        //no rich text run. go to next text run.
                        continue;
                    }
                    for (RichTextRun richTextRun : richTextRuns) {
                        //empty the string builder.
                        sb.delete(0, sb.length());
                        //set the text into the stringbuilder
                        sb.append(richTextRun.getText());
                        POIUtil.printTextWithLineBreaks("oldText: " + 
richTextRun.getText());
                        matcher = pattern.matcher(sb.toString());
                        //get all replacements parameter.
                        boolean change = false;
                        while (matcher.find()) {
                            change = true;
                            String param = matcher.group();
                            System.out.println("match found: " + param);
                            //replace match in text.
                            String replacement = POIUtil.getValue(map, param);
                            System.out.println(param + " replaced by " + 
replacement);
                            int start = sb.indexOf(param);
                            int end = start + param.length();
                            sb.replace(start, end, replacement);
                        }
                        if (change) {
                            POIUtil.printTextWithLineBreaks("new text: " + 
sb.toString());
                        }                        
                        if (change) {                        
                            System.out.println("text changed");
                            richTextRun.setText(sb.toString());
                        }                        
                    }
                }
                System.out.println();
            }
            POIUtil.writeSlideShow(ppt, OUTPUT);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


Sylvain

_________________________________________________________________
Il est temps de rejoindre la famille - Mettez-vous dès maintenant gratuitement 
à la nouvelle génération des services Windows Live!
http://get.live.com

Reply via email to