Bug 45088

Summary: POI-HSLF changeTextInRichTextRun corrupts presentation
Product: POI Reporter: Des Kenny <des.kenny>
Component: HSLFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: major CC: sanmoy, ujjwalbsoni20032003
Priority: P2    
Version: 3.0-dev   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Attachments: test.ppt

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. ***