Bug 45088 - POI-HSLF changeTextInRichTextRun corrupts presentation
Summary: POI-HSLF changeTextInRichTextRun corrupts presentation
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSLF (show other bugs)
Version: 3.0-dev
Hardware: PC Windows XP
: P2 major (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
: 47494 51800 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-05-28 06:47 UTC by Des Kenny
Modified: 2015-11-01 19:54 UTC (History)
2 users (show)



Attachments
test.ppt (99.50 KB, application/vnd.ms-powerpoint)
2008-05-28 06:47 UTC, Des Kenny
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Des Kenny 2008-05-28 06:47:36 UTC
Created attachment 22024 [details]
test.ppt

Am using a pre-release version of scratchpad (although I think it also happens with previous versions)

Using version poi-scratchpad-3.1-beta2-20080526

Calling changeTextInRichTextRun renders the output presentation corrupt. Have tried to open with Microsoft Office Powerpoint 2003 and 2007. The reason I'm trying to use rich text runs, is to prevent the formating of the text being erased, which is happening when I call setText on the TextRun. Here's the code...

    public static void main(String[] args) {
	doPowerpoint();
    }

    private static void doPowerpoint() {
	try {
	    SlideShow ppt = new SlideShow(new HSLFSlideShow("./src/main/resources/test.ppt"));
	    Slide[] slides = ppt.getSlides();
	    for (int i = 0; i < slides.length; i++) {
		System.out.println("checking slide " + i);
		TextRun[] text = slides[i].getTextRuns();

		outer: for (int j = 0; j < text.length; j++) {
		    System.out.println(" checking text " + j);
		    RichTextRun[] richText = text[j].getRichTextRuns();
		    for (int k = 0; k < richText.length; k++) {
			System.out.println("  checking rich text " + k);
			String sText = richText[k].getText();
			if (sText != null && sText.contains("[SYSDATE]")) {
			    String replacedText = sText.replace("[SYSDATE]", "REPLACED_DATE_WITH_A_LONG_ONE");
			    text[j].changeTextInRichTextRun(richText[k], replacedText);
			    text[j].ensureStyleAtomPresent();
			    System.out.println("replaced date!" + richText[k].getRawText());
			    continue outer;
			}
		    }
		}
	    }
	    writeFile(ppt);

	} catch (Exception e) {
	    e.printStackTrace();
	    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;
	    }
	}
    }
Comment 1 Yegor Kozlov 2008-06-03 23:12:42 UTC
From poi-user:

Check setting style when '\n' needs to be the last
character in the first RichTextRun. It seems to be a related bug.

        SlideShow ppt = new SlideShow();
                        
        Slide s = ppt.createSlide();
        TextBox tb = new TextBox();
        TextRun tr = tb.createTextRun();
                        
        tb.setAnchor(new Rectangle(10,10,500,20));
        
        tr.getRichTextRuns()[0].setText("hello\n");
        tr.getRichTextRuns()[0].setFontName("Calibri");
        tr.getRichTextRuns()[0].setFontSize(12);
        
        RichTextRun rtr = tr.appendText("world");
        rtr.setFontName("Times New Roman");
        rtr.setFontSize(12);
                
        s.addShape(tb);
        
        FileOutputStream out = new FileOutputStream("report/slideshow.ppt");
        ppt.write(out);
        out.close();
Comment 2 Andreas Beeker 2015-11-01 19:42:58 UTC
Nothing to be changed ... anymore.
I've added a testcase documenting the new handling via r1711845
Comment 3 Andreas Beeker 2015-11-01 19:44:54 UTC
*** Bug 47494 has been marked as a duplicate of this bug. ***
Comment 4 Andreas Beeker 2015-11-01 19:54:46 UTC
*** Bug 51800 has been marked as a duplicate of this bug. ***