Free Support Forum - aspose.com

Inserting bulleted list via HTML does not apply template formatting

I have a PowerPoint template file which defines styles and layouts that I must to use to create a PowerPoint presentation. The content to use for the presentation is in HTML format. This is working relatively well with tags such as <strong> and <u> but I’m having some trouble with bulleted lists.

Given the following HTML list:

<ul>
  <li>1</li>
    <ul>
      <li>1.1</li>
      <li>1.2</li>
      <ul>
        <li>1.2.1</li>
      </ul>
    </ul>
  <li>2</li>
  <li>3</li>
    <ul>
      <li>3.1</li>
    </ul>
</ul>

And using the ‘Title and Content’ layout from the template in the zip file at the end of the post, I would expect the result to be as follows:

expected-result.png (8.4 KB)

However the result is as follows:

actual-result.png (17.2 KB)

Two things in particular appear to be incorrect:

  1. The bullets are not the correct style. Look at the bullet style on the template – the first level should be a box, the second a diamond, and the third a tick.
  2. The bullets are one level indented further than they should be. This may not be obvious from the screenshot as the bullet styles are not correct but I’ve attached a zip file at the end of the post containing the generated PowerPoint file.

The code I am using to generate the PowerPoint is:

   public void createExamplePpt(final String templateFile, final String saveLocation) throws FileNotFoundException {
        License license = new License();
        license.setLicense(getClass().getClassLoader().getResourceAsStream("Aspose.Total.Java.lic"));

        FileInputStream fis = new FileInputStream(templateFile);
        Presentation presentation = createPresentationFromTemplate(fis);

        // Create a new slide using the 'Title and Content' layout
        final ILayoutSlide layoutSlide = presentation.getMasters().get_Item(1).getLayoutSlides().get_Item(1);
        final ISlide newSlide = presentation.getSlides().addEmptySlide(layoutSlide);

        final String bodyPlaceholderText = "Click to edit body text";
        final String html = "<ul>"
                + "  <li>1</li>"
                + "    <ul>"
                + "      <li>1.1</li>"
                + "      <li>1.2</li>"
                + "      <ul>"
                + "        <li>1.2.1</li>"
                + "      </ul>"
                + "    </ul>"
                + "  <li>2</li>"
                + "  <li>3</li>"
                + "    <ul>"
                + "      <li>3.1</li>"
                + "    </ul>"
                + "</ul>";

        // Find the placeholder on the new slide for the "body" text and replace it with the HTML content
        for (IShape shape : newSlide.getShapes()) {
            if (shape.getPlaceholder() != null) {
                final IAutoShape placeHolderShape = (IAutoShape)shape;
                final String placeholderText = placeHolderShape.getTextFrame().getText();
                if (placeholderText.startsWith(bodyPlaceholderText)) {
                    final IParagraphCollection paragraphs = placeHolderShape.getTextFrame().getParagraphs();
                    paragraphs.addFromHtml(html);
                    paragraphs.removeAt(0);
                }
            }
        }

        presentation.save(saveLocation, SaveFormat.Pptx);
    }

    private Presentation createPresentationFromTemplate(final InputStream templateStream) {
        Presentation template = new Presentation(templateStream);

        Presentation presentation = new Presentation();
        final Dimension2D templateSize = template.getSlideSize().getSize();
        presentation.getSlideSize().setSize((float)templateSize.getWidth(), (float)templateSize.getHeight(), SlideSizeScaleType.DoNotScale);
        presentation.getMasters().addClone(template.getMasters().get_Item(0));
        return presentation;
    }

powerpoint-from-html.zip (157.9 KB)

Is there a way to achieve what I want to do?

@sleylandcole

Please try using following on your end. Actually, Aspose.Slides used default characters for bullet during importing. After importing the HTML, we can change them.

public void bulletTest()
{
Presentation presentation = createPresentationFromTemplate(path+ "example-template.potx");

// Create a new slide using the 'Title and Content' layout
final ILayoutSlide layoutSlide = presentation.getMasters().get_Item(1).getLayoutSlides().get_Item(1);
final ISlide newSlide = presentation.getSlides().addEmptySlide(layoutSlide);

final String bodyPlaceholderText = "Click to edit body text";
final String html = "<ul>"
+ " <li>1</li>"
+ " <ul>"
+ " <li>1.1</li>"
+ " <li>1.2</li>"
+ " <ul>"
+ " <li>1.2.1</li>"
+ " </ul>"
+ " </ul>"
+ " <li>2</li>"
+ " <li>3</li>"
+ " <ul>"
+ " <li>3.1</li>"
+ " </ul>"
+ "</ul>";

// Find the placeholder on the new slide for the "body" text and replace it with the HTML content
for (IShape shape : newSlide.getShapes()) {
if (shape.getPlaceholder() != null) {
final IAutoShape placeHolderShape = (IAutoShape)shape;
final String placeholderText = placeHolderShape.getTextFrame().getText();
if (placeholderText.startsWith(bodyPlaceholderText)) {
final IParagraphCollection paragraphs = placeHolderShape.getTextFrame().getParagraphs();
paragraphs.addFromHtml(html);
paragraphs.removeAt(0);
findAndReplaceBullet(paragraphs);
}
}
}

presentation.save(path + "bullet.pptx", SaveFormat.Pptx);
}

private Presentation createPresentationFromTemplate(String templatePath) {
Presentation template = new Presentation(templatePath);

Presentation presentation = new Presentation();
final Dimension2D templateSize = template.getSlideSize().getSize();
presentation.getSlideSize().setSize((float)templateSize.getWidth(), (float)templateSize.getHeight(), SlideSizeScaleType.DoNotScale);
presentation.getMasters().addClone(template.getMasters().get_Item(0));
return presentation;
}


private static void findAndReplaceBullet(IParagraphCollection paragraphs)
{
char[] BulletChars = new char[] { (char) 252 , (char) 113, (char) 118 };

for (IParagraph paragraph : paragraphs)
{
paragraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
paragraph.getParagraphFormat().getBullet().setChar(BulletChars[paragraph.getParagraphFormat().getDepth() % 3]);
}
}

example-template.zip (23.6 KB)

@mudassir.fayyaz thank you very much for your response. It works to an extent, as in the bullet styles are correct however it doesn’t resolve the indentation or the font styling.

If you compare the file generated from your code to the expected file you will see that the bullets are all indented one level further than they should be. This means that the template applies the wrong formatting. To give a specific example, look at ‘1’: this is a first level bullet point so the font should be Comic Sans size 28 but is instead Courier New size 24 which is the style for a level 2 bullet.

The other problem is that the fix hardcodes the bullet point styles rather than getting them from the template. This code needs to work with a number of templates which have different styles, they must be taken from the template rather than code.

I’m not sure if there’s something I’m doing wrong or if this is a bug in Aspose.Slides. I would certainly expect paragraphs created from HTML to inherit the formatting from where they were inserted.

Thanks for any further help you can offer

@sleylandcole

It seems to be an issue and I have added a ticket with ID SLIDESJAVA-38313 in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be addressed.