Bug 53355

Summary: When changing text content, formatting is lost.
Product: POI Reporter: Mark <mark.horninger>
Component: HSLFAssignee: POI Developers List <dev>
Status: RESOLVED INVALID    
Severity: minor    
Priority: P2    
Version: 3.8-FINAL   
Target Milestone: ---   
Hardware: All   
OS: All   

Description Mark 2012-06-04 17:26:17 UTC
When changing text on a slide, formatting is lost. See example code below:
	private boolean replaceTextInPowerPoint(final File destFile,
			final String assessmentId) {
		logger.log(Level.TRACE,
				"Entering WordDocGenerator.replaceVariablesInWordDoc(File, String)");
		boolean result = Boolean.FALSE;
		if (destFile.canWrite()) {
			FileInputStream is;
			final AnnexVarDao dao = new AnnexVarDao();
			final Map<String, String> variableRs = dao
					.loadVarReplacementMapByAssessmentId(assessmentId);
			final Map<String, String> summaryVars = dao
					.loadSummaryValuesByAssessment(assessmentId);
			final Map<String, String> qnAVars = dao
					.loadQuestionAndAnswerVarsByAssessment(assessmentId);
			if (summaryVars != null) {
				variableRs.putAll(summaryVars);
			}

			// 04/03/2012 [MJH] Change in regards to BUG: 72
			// Added Q&A section to the document.
			// For more detail, see Bugzilla at:
			// http://tlma.dimensiondata.com/bugs/show_bug.cgi?id=72
			if ((qnAVars != null) && (0 < qnAVars.size())) {
				variableRs.putAll(qnAVars);
			}
			final StrLookupDefaultReplace<String> strLookup = new StrLookupDefaultReplace<String>(
					variableRs);
			final StrSubstitutor subtitution = new StrSubstitutor(strLookup,
					StrSubstitutor.DEFAULT_PREFIX,
					StrSubstitutor.DEFAULT_SUFFIX,
					StrSubstitutor.DEFAULT_ESCAPE);
			try {
				if (0 < variableRs.size()) {
					is = new FileInputStream(destFile);
					final XMLSlideShow pptx = new XMLSlideShow(is);
					
					for(final XSLFSlide slide : pptx.getSlides()) {
						for (final XSLFTextShape textShape : slide
								.getPlaceholders()) {
							String shapeText = textShape.getText();
							logger.log(Level.DEBUG, String.format(
									"Got shape text%s%s",
									Configuration.SYSTEM_NEWLINE_STRING,
									shapeText));
							
							// 05/31/2012 [MJH] Change in regards to BUG: 89
							// Reducing the number of replacements that are done
							// where possible.
							// For more detail, see Bugzilla at:
							// http://tlma.dimensiondata.com/bugs/show_bug.cgi?id=89
							if (StringUtils.contains(shapeText, "${")) {
								shapeText = subtitution.replace(shapeText);
								textShape.clearText();
								logger.log(Level.DEBUG, String.format(
										"Replacing shape text on slide: %s",
										slide.getTitle()));
								textShape.setText(shapeText);
							}
							logger.log(Level.DEBUG, String.format(
									"New shape text%s%s",
									Configuration.SYSTEM_NEWLINE_STRING,
									shapeText));
						}
					}
					final FileOutputStream os = new FileOutputStream(destFile);
					pptx.write(os);
					result = Boolean.TRUE;
				}
			} catch (final FileNotFoundException e) {
				logger.log(Level.ERROR, e.getMessage(), e);
			} catch (final IOException e) {
				logger.log(Level.ERROR, e.getMessage(), e);
			}

		}
		logger.log(Level.TRACE,
				"Exiting WordDocGenerator.replaceVariablesInWordDoc(File, String)");
		return result;
	}
Comment 1 Mark 2012-06-04 17:29:16 UTC
I know that the trace log entries reference the wrong method name.  I assure you that it is receiving a powerpoint file.
Comment 2 Andreas Beeker 2015-11-09 22:17:04 UTC
The text handling has been changed a lot and now the formatting/styles will be 
kept for every paragraph and text run and in the end it's combined when the 
file is saved - the older method was to change the formatting in-place which 
might have lead to a formatting loss.
Please have a look into the documentation of 
org.apache.poi.sl.usermodel.TextShape for the new handling, this describes when 
the formatting is lost now, e.g. when calling setText()

If you have problems with the new handling, feel free to reopen this bug with a 
Short, Self Contained, Correct (Compilable), Example (see http://sscce.org), 
i.e. I'm not trying to figure out a problem based on some code ripped out of 
report generation source.