How to Center All Content when Converting a Presentation to HTML in Java?

When using Aspose.Slides (Java) to convert PPT(X) to HTML, how to center the content in HTML, now it stays left.
This is my code:

Presentation ppt = new Presentation(src);
ppt.save(dest, com.aspose.slides.SaveFormat.Html);

@serendipity.zhq,
Thank you for posting your question.

As far as I can see, Aspose.Slides does not provide a way to change the alignment of the output HTML content. I added a ticket with ID SLIDESJAVA-38761 to our issue tracking system. Our development team will consider implementing such an enhancement. You will be notified when the issue is resolved.

@serendipity.zhq,
Our development team investigated the issue. You can center all content when converting a presentation to HTML as shown below:

SimpleHtmlController controller = new SimpleHtmlController();

HtmlOptions htmlOptions = new HtmlOptions();
htmlOptions.setHtmlFormatter(HtmlFormatter.createCustomFormatter(controller));

Presentation ppt = new Presentation("test.pptx");
ppt.save("test_out.html", SaveFormat.Html, htmlOptions);
class SimpleHtmlController implements IHtmlFormattingController
{
    public SimpleHtmlController()
    {
    }

    public final void writeDocumentStart(IHtmlGenerator generator, IPresentation presentation)
    {
        generator.addHtml(Header);
    }

    public final void writeDocumentEnd(IHtmlGenerator generator, IPresentation presentation)
    {
        generator.addHtml(Footer);
    }

    public final void writeSlideStart(IHtmlGenerator generator, ISlide slide)
    {
        generator.addHtml(SlideHeader);
    }

    public final void writeSlideEnd(IHtmlGenerator generator, ISlide slide)
    {
        generator.addHtml(SlideFooter);
    }

    public final void writeShapeStart(IHtmlGenerator generator, IShape shape){ }

    public final void writeShapeEnd(IHtmlGenerator generator, IShape shape) { }

    private final String Header = "<!DOCTYPE html>\n <html>\n <head>\n" +
            "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
            "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=9\"></head>";

    private final String SlideHeader =
            "<div class=\"slideImage\" style=\"" +
                    "position: absolute; top: 0; bottom: 0; left: 0; right: 0; " +
                    "display: flex; flex-wrap: wrap; align-items: center; justify-content: center;\">\n";

    private final String SlideFooter = "</div>\n";

    private final String Footer = "</body></html>";
}

Documents: Convert Powerpoint to HTML
API Reference: HtmlOptions Class | HtmlFormattingController Interface

Thanks for your reply, I tested the code and it does center everything, but when I open the converted html, the content inside the html automatically runs to the last page.

@serendipity.zhq,
Please check your results using the latest version of Aspose.Slides if it is possible. If the issue persists, please share the following data:

  • input presentation file and your output HTML document
  • OS version on which the code was run
  • JDK version
  • Aspose.Slides version you used
  • My file (https://filedropper.com/d/s/n2QEai48U3fUB0Dz3T3qqFseQ63B4n)
  • win 10
  • jdk 1.8.0_261
  • Aspose.Slides version aspose-slides 22.2

@serendipity.zhq,
Thank you for the additional information. I’ve checked the problem and passed this data to our development team.

@serendipity.zhq,
Please replace the code part

private final String SlideHeader =
        "<div class=\"slideImage\" style=\"" + 
        "position: absolute; top: 0; bottom: 0; left: 0; right: 0; " +
        "display: flex; flex-wrap: wrap; align-items: center; justify-content: center;\">\n";

with the following:

private final String SlideHeader =
        "<div class=\"slideImage\" style=\"" +
        "position: relative; top: 0; bottom: 0; left: 0; right: 0; " +
        "display: flex; flex-wrap: wrap; align-items: center; justify-content: center;\">\n";

This fix should help.

Thanks!The problem has been solved perfectly.

@serendipity.zhq,
We are glad to know about it. Please feel free to ask any questions related to Aspose products.