Bug 45776 - [PATCH] Fix corrupt file problem using TextRun.setText
Summary: [PATCH] Fix corrupt file problem using TextRun.setText
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSLF (show other bugs)
Version: unspecified
Hardware: PC Mac OS X 10.4
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-09-10 14:58 UTC by Don Boulia
Modified: 2013-12-29 22:18 UTC (History)
0 users



Attachments
input test case powerpoint file (121.50 KB, application/vnd.ms-powerpoint)
2008-09-10 14:58 UTC, Don Boulia
Details
patch for TextRun.setRawText (1.04 KB, patch)
2008-09-10 14:59 UTC, Don Boulia
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Don Boulia 2008-09-10 14:58:54 UTC
Created attachment 22552 [details]
input test case powerpoint file

See the attached file testcase1in.ppt as the input file to the following test code which tries to substitute the string $$DATE$$ with the current date/time:

package pptMaker;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.model.*;

/**
 * Test file to take an input file, change its contents based on a predefined template
 * string, and write the output
 */

public class ModifyDate {
	
	public static final String _inputPPTFile = "/Users/djboulia/Downloads/testcase1in.ppt";
	public static final String _outputPPTFile = "/Users/djboulia/Downloads/testcase1out.ppt";

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		 SlideShow ppt = new SlideShow(new HSLFSlideShow(_inputPPTFile));

		 //get slides 
		  Slide[] slide = ppt.getSlides();
		  for (int i = 0; i < slide.length; i++){
			System.out.println( "Slide " + i);
			
		    Shape[] sh = slide[i].getShapes();
		    for (int j = 0; j < sh.length; j++){
		      //name of the shape
		      String name = sh[j].getShapeName();

		      //shapes's anchor which defines the position of this shape in the slide
		      java.awt.Rectangle anchor = sh[j].getAnchor();

		      if (sh[j] instanceof Line){
		        Line line = (Line)sh[j];
		        //work with Line
		        System.out.println( "Found a Line!");
		      } else if (sh[j] instanceof AutoShape){
		        AutoShape shape = (AutoShape)sh[j];
		        //work with AutoShape
		        System.out.println( "Found an AutoShape!");
		      } else if (sh[j] instanceof TextBox){
	                TextBox shape = (TextBox)sh[j];
	                //work with TextBox
	                String str = shape.getText();
	                               
	                System.out.println( "Found a TextBox!");
	                System.out.println( str );
	                
	                if (str.indexOf("$$DATE$$") >=0 ) {
	                    System.out.println( "Found $$DATE$$!!" );
	                    java.util.Date now = new java.util.Date();
	                    str = str.replaceAll("[$][$]DATE[$][$]", now.toString());
	 
				        if (str.compareTo(shape.getText())!=0) {
					        shape.setText( str );
				        }
	                    System.out.println( "Replacement string is " + str ); 
	                }
	                		        
		      } else if (sh[j] instanceof Picture){
		        Picture shape = (Picture)sh[j];
		        //work with Picture
		        System.out.println( "Found a Picture!");
		        
		      } else {
		    	  System.out.println("Found unknown shape: "+name);
		      }
		    }
		  }
		  
	    //save changes in a new file
	    FileOutputStream out = new FileOutputStream(_outputPPTFile);
	    ppt.write(out);
	    out.close();

	}

}

Running this on poi-3.1 results in a corrupt PowerPoint file.  (Loading it in Office 2004 on the Mac results in a blank page.)

After some investigation, it appears that there are cases where TextRun.changeRichTextRun incorrectly calculates the new length of the paragraph properties when called via setRawText.

The proposed patch resets the size of the paragraph and character properties to the correct length when setRawText is called which avoids the problem in changeRichTextRun.
Comment 1 Don Boulia 2008-09-10 14:59:50 UTC
Created attachment 22553 [details]
patch for TextRun.setRawText
Comment 2 Andreas Beeker 2013-12-29 22:18:29 UTC
Thank you for the patch.

Applied with SVN ver r1554077