You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Sylvain Berthouzoz <sb...@hotmail.com> on 2008/05/08 17:02:44 UTC
Powerpoint: modifying text causes error: Powerpoint can't read this
text.
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
Re: Powerpoint: modifying text causes error: Powerpoint can't read
this text.
Posted by patrickolee <de...@globoforce.com>.
Oh dear, discovered a problem with the fix I think...
If we do something like...
richText[k].setRawText(richText[k].getRawText().replace("[SYSDATE2]",
"REPLACED_DATE2"));
We'll get a string out of bounds exception generated from within the
setRawText method. eg.
Problem with slide show String index out of range: 51
java.lang.StringIndexOutOfBoundsException: String index out of range: 51
at java.lang.String.substring(String.java:1765)
at
org.apache.poi.hslf.usermodel.RichTextRun.getRawText(RichTextRun.java:158)
at
org.apache.poi.hslf.model.TextRun.changeTextInRichTextRun(TextRun.java:435)
at
org.apache.poi.hslf.usermodel.RichTextRun.setRawText(RichTextRun.java:174)
at com.globoforce.scheduled.App.main(App.java:34)
Changing the calls to use getText and setText avoids the string out of
bounds, but loses all formatting so the text, when replaced, is centre
justified and no bullet points.
--
View this message in context: http://www.nabble.com/Powerpoint%3A-modifying-text-causes-error%3A-Powerpoint-can%27t-read-this-text.-tp17130113p17401765.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: Powerpoint: modifying text causes error: Powerpoint can't read
this text.
Posted by patrickolee <de...@globoforce.com>.
Hi Yegor
Yes, the 'poi-3.1-beta2' which you provided the link for fixed the problem I
was seeing.
Thanks :-)
-patrick
--
View this message in context: http://www.nabble.com/Powerpoint%3A-modifying-text-causes-error%3A-Powerpoint-can%27t-read-this-text.-tp17130113p17399578.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: Powerpoint: modifying text causes error: Powerpoint can't read
this text.
Posted by patrickolee <de...@globoforce.com>.
Just noticed bug #44985, issue has been resolved and will be 'Fixed in
r656252'
cheers
--
View this message in context: http://www.nabble.com/Powerpoint%3A-modifying-text-causes-error%3A-Powerpoint-can%27t-read-this-text.-tp17130113p17364297.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: Powerpoint: modifying text causes error: Powerpoint can't read
this text.
Posted by patrickolee <de...@globoforce.com>.
I've tested with Open Office, it can read the file... tested with Earlier
version Office office (2003 I think) and it does not work. Have attached a
test spreadsheet.. and here's the code to reproduce the problem...
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
public class App {
public static void main(String[] args) {
try {
SlideShow ppt = new SlideShow(new
HSLFSlideShow("./src/main/resources/test.ppt"));
Slide[] slides = ppt.getSlides();
for (int i = 0; i < slides.length; i++) {
TextRun[] text = slides[i].getTextRuns();
for (int j = 0; j < text.length; j++) {
if (text[j].getText().contains("[SYSDATE]")) {
text[j].setText("REPLACED_DATE");
System.out.println("replaced date!" + text[j].getText());
}
}
}
writeFile(ppt);
} catch (Exception e) {
System.out.println("Problem with slide show " + e.getMessage());
}
}
private static void writeFile(SlideShow slideshow) throws
FileNotFoundException, IOException {
FileOutputStream fos = null;
try {
File outputFile = new File("output.ppt");
fos = new FileOutputStream(outputFile);
slideshow.write(fos);
} finally {
if (fos != null) {
fos.close();
fos = null;
}
}
}
}
--
View this message in context: http://www.nabble.com/Powerpoint%3A-modifying-text-causes-error%3A-Powerpoint-can%27t-read-this-text.-tp17130113p17364267.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: Powerpoint: modifying text causes error: Powerpoint can't read
this text.
Posted by patrickolee <de...@globoforce.com>.
If I replace, the text, with the same length of text everything is fine...
i.e. if I replace SYSDATE with SSSDATE all works fine, but if I replace
SYSDATE with SYSDATES, when the result is opened with Powerpoint 2007 it
says some of the slides have been corrupted and displays the slide as blank.
patrickolee wrote:
>
> I'm seeing something similar, except I'm using
> poi-scratchpad-3.0.2-FINAL-20080204.
>
> I'm also using Windows XP with Powerpoint 2007, unfortunately.
>
>
>
> Sylvain Berthouzoz wrote:
>>
>> 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
>>
>
>
--
View this message in context: http://www.nabble.com/Powerpoint%3A-modifying-text-causes-error%3A-Powerpoint-can%27t-read-this-text.-tp17130113p17364234.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[2]: Powerpoint: modifying text causes error: Powerpoint can't read this text.
Posted by Yegor Kozlov <ye...@dinom.ru>.
Try the forthcoming poi-3.1-beta2:
http://people.apache.org/~yegor/POI-3.1-BETA2/
The problem should be fixed in it.
Yegor
> I'm seeing something similar, except I'm using
> poi-scratchpad-3.0.2-FINAL-20080204.
> I'm also using Windows XP with Powerpoint 2007, unfortunately.
> Sylvain Berthouzoz wrote:
>>
>> 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 des maintenant
>> gratuitement a la nouvelle generation des services Windows Live!
>> http://get.live.com
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: Powerpoint: modifying text causes error: Powerpoint can't read
this text.
Posted by patrickolee <de...@globoforce.com>.
I'm seeing something similar, except I'm using
poi-scratchpad-3.0.2-FINAL-20080204.
I'm also using Windows XP with Powerpoint 2007, unfortunately.
Sylvain Berthouzoz wrote:
>
> 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
>
--
View this message in context: http://www.nabble.com/Powerpoint%3A-modifying-text-causes-error%3A-Powerpoint-can%27t-read-this-text.-tp17130113p17364229.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: Powerpoint: modifying text causes error: Powerpoint can't read this text.
Posted by Yegor Kozlov <ye...@dinom.ru>.
It might be a bug. Can you create a bug in Bugzilla and attach to it
a ppt template and sample code to reproduce the problem.
Yegor
> 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
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: Powerpoint: modifying text causes error: Powerpoint can't read
this text.
Posted by carsamba55 <bl...@live.de>.
Hi,
I still have the problem, Sylvian described. I use Apache POI v3.7-20101029.
I can't open the PPT-File (MS Office 2003 and 2007).
I tried with the same code, Patrickolee posted:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
public class App {
public static void main(String[] args) {
try {
SlideShow ppt = new SlideShow(new
HSLFSlideShow("./src/main/resources/test.ppt"));
Slide[] slides = ppt.getSlides();
for (int i = 0; i < slides.length; i++) {
TextRun[] text = slides[i].getTextRuns();
for (int j = 0; j < text.length; j++) {
if (text[j].getText().contains("[SYSDATE]")) {
text[j].setText("REPLACED_DATE");
System.out.println("replaced date!" +
text[j].getText());
}
}
}
writeFile(ppt);
} catch (Exception e) {
System.out.println("Problem with slide show " + e.getMessage());
}
}
private static void writeFile(SlideShow slideshow) throws
FileNotFoundException, IOException {
FileOutputStream fos = null;
try {
File outputFile = new File("output.ppt");
fos = new FileOutputStream(outputFile);
slideshow.write(fos);
} finally {
if (fos != null) {
fos.close();
fos = null;
}
}
}
}
And also, if text length is the same it works fine, but if not, it doesn't
work.
What's the problem? I thought the bug is fixed since v3.2
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Powerpoint-modifying-text-causes-error-Powerpoint-can-t-read-this-text-tp2298821p4288724.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