Hello Aspose Team!
I’ve been under the assumption that when I call .getFormat().getEffective() on a Portion object, the resulting IPortionFormatEffective object was immutable and basically a ‘snapshot’ of the format style at the time of calling. However this doesn’t seem to always be the case as I’ve ran into an issue where an existing instance of IPortionFormatEffectiveData returns different objects after a subjective .getEffective() call on either the original portion or the paragraph parent for the portion, after the format of either changes.
I’m not sure if this is a bug or intended, and some clarification would be greatly appreciated as I’ve failed to reproduce the issue when instantiating a Table/AutoShape via code rather than the template provided.
Test template:
test-template.zip (1.4 MB)
Test code:
@Test
void iPortionFormatEffectiveData_doesNotChangeAfterGetEffectiveMethodCall() {
try {
presentation = new Presentation("test-template.pptx");
ISlide slide = presentation.getSlides().get_Item(0);
for (IShape shape : slide.getShapes()) {
if(shape instanceof ITable) {
testPortionFormatEffectiveDataDoesNotChange((ITable) shape);
}
}
} finally {
if (presentation != null) {
presentation.dispose();
}
}
}
private static void testPortionFormatEffectiveDataDoesNotChange(ITable table) {
// This cell triggered the issue originally, but happens with other cells too
ICell testCell = table.get_Item(2,2);
// Keeping the paragraph around for triggering the 'bug' later
IParagraph sourceParagraph = testCell.getTextFrame().getParagraphs().get_Item(0);
IPortion sourcePortion = sourceParagraph.getPortions().get_Item(0);
Color originalFillColor = sourcePortion.getPortionFormat().getFillFormat().getSolidFillColor().getColor();
Color testFillColor = new Color(34, 64, 128);
SoftAssertions softly = new SoftAssertions();
// Verify the original fill color is not the same as the test color
softly.assertThat(originalFillColor.getRed()).isNotEqualTo(testFillColor.getRed());
softly.assertThat(originalFillColor.getGreen()).isNotEqualTo(testFillColor.getGreen());
softly.assertThat(originalFillColor.getBlue()).isNotEqualTo(testFillColor.getBlue());
// Generate the original portions IPortionFormatEffective before changing anything
IPortionFormatEffectiveData sourcePortionFormatEffective =
sourcePortion.getPortionFormat().getEffective();
// Check fill color in IPortionFormatEffective is the expected original color
softly.assertThat(sourcePortionFormatEffective.getFillFormat().getSolidFillColor()).isEqualTo(originalFillColor);
// Change the portion fill Color
sourcePortion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
sourcePortion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(testFillColor);
// Check color in the source portion actually changed
softly.assertThat(sourcePortion.getPortionFormat().getFillFormat().getSolidFillColor().getColor())
.isEqualTo(testFillColor);
// Check fill color in instance of IPortionFormatEffective hasn't changed
softly.assertThat(sourcePortionFormatEffective.getFillFormat().getSolidFillColor()).isEqualTo(originalFillColor);
// 'Bug' happens here, generating either IParagraphFormatEffective from parent paragraph
// or IPortionFormatEffective from the source portion mutates existing instance of IPortionFormatEffective
// Calling either function below causes following test to fail.
// sourceParagraph.getParagraphFormat().getEffective();
sourcePortion.getPortionFormat().getEffective();
// Color of existing IEffectivePortionFormat should still be the original color
softly.assertThat(sourcePortionFormatEffective.getFillFormat().getSolidFillColor()).isEqualTo(originalFillColor);
softly.assertAll();
}
For clarification: I’ve been using IPortionFormatEffective as a way to capturing the text styling of keywords, and using said styling for different portions. Sometimes the original portion style changes, but I still need the original styling from the template to change/add other portions and apply that style. Do I have to make my own POJO to hold these values or is the use of IPortionFormatEffective correct?