Free Support Forum - aspose.com

Table Cell Content Format Changing

I’m seeing an issue where the content in a table cell is changing format after I change the text. The text in the cell is original black and after I set the text it turns red. It is only happening for that one cell. I included a test PPT file and some code that replicates the issue.

public static void main(String[] args){
	Presentation ppt = new Presentation("test_delete_before.pptx");

	try {
		for (ISlide slide : ppt.getSlides()) {
			for (IShape shape : slide.getShapes()) {
				if (shape instanceof ITable) {
					ITable table = (ITable) shape;
					
					for (IRow row : table.getRows()) {

						for (int i = 0; i < table.getColumns().size(); i++) {
							row.get_Item(i).getTextFrame().setText("$0");
						}
						
					}
				}
			}
		}

		ppt.save("test_delete_after.pptx", com.aspose.slides.SaveFormat.Pptx);

	} finally {
		ppt.dispose();
	}
}

test_delete_before.zip (44.4 KB)

@jsaunders2011,

I have observed the code sample shared by you. Actually, you are trying to set the text on TextFrame level. When you do so, the formatting get lost. In order to maintain the text formatting, you need to set the text on TextFrame’s paragraph’s portion level. The following statement in your code need revision.

for (IRow row : table.getRows()) {

						for (int i = 0; i < table.getColumns().size(); i++) {
							row.get_Item(i).getTextFrame().setText("$0");
						}

Please try using following revised code.

row.get_Item(i).getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).setText("$0");

My actual use case is that I’m trying do a find-replace of a specific value within a paragraph. Using your approach, the value is broken up into multiple portions, so the find-replace isn’t working. Can I set the text at the paragraph level, rather than the portion level?

@jsaunders2011,

I have observed your requirements and like to share that if you want to retain the text formatting that has to be maintained on portion level. You may find text on paragraph level and may set text on portion level and removing remaining portions in paragraph.

If I do this:

paragraph.getPortions().get_Item(0).setText((paragraph.getText().replace("text to replace", "new text")));

and then delete the other portions, the new text still appears in red.

If I do this:

paragraph.getPortions().add(new Portion(paragraph.getText().replace("text to replace", "new text")));

and then delete the other portions, the next text is not red, but it is larger than the original text.

In neither case is it retaining the original formatting of the text.

Really frustrating…

@jsaunders2011,

I have observed the information shared by you and request you to please share the working sample project along with generated and desired output presentation. We will investigate the requirements on our end to help you further.