Add New Last Slide with Text to a Presentation Using the insertFromHtml Method

For an existing presentation I want to add an extra slide with some additional free text. I am using

IMasterLayoutSlideCollection layoutSlides = emptyMaster.getLayoutSlides();
blankLayout = layoutSlides.getByType(SlideLayoutType.Blank);
if (blankLayout == null) {
    blankLayout = layoutSlides.add(SlideLayoutType.Blank, null);
}

ISlide emptySlide = ppt.getSlides().addEmptySlide(blankLayout);
ISlide[] notesSlides = slides.insertFromHtml(slides.size(), "Some text .... ");
ppt.getSlides().remove(emptySlide);

Depending on the presentation I opened the text on the new slide is sometimes centerd and sometimes left aligned. Additionally the text “Click to edit Master title style” occurs sometimes. So far I failed to modify the master or the layout to avoid these effects. Can you please provide a suggestion so that the text on the new slide is just left aligned?

@Tim49

Cause

The issue you’re experiencing with text alignment and the appearance of “Click to edit Master title style” is likely due to the default settings of the slide layout and the text frame properties. When you insert text using insertFromHtml, it may inherit formatting from the master slide or the layout slide, which can lead to inconsistent text alignment.

Solution

To ensure that the text on your new slide is left-aligned and does not show the default title style, you can explicitly set the text frame properties after inserting the text. Here’s how you can do that:

  1. After adding the new slide, access the text frame of the shape where you want to insert the text.
  2. Set the text alignment to left.
  3. Clear any default title text that might be present.

Code

Here’s a code snippet that demonstrates how to achieve this:

// Add a new slide with a blank layout
IMasterLayoutSlideCollection layoutSlides = emptyMaster.getLayoutSlides();
ISlide emptySlide = ppt.getSlides().addEmptySlide(blankLayout);

// Insert text from HTML
ISlide[] notesSlides = slides.insertFromHtml(slides.size(), "Some text .... ");

// Access the newly added slide's shape
IAutoShape shape = (IAutoShape) emptySlide.getShapes().get_Item(0);
ITextFrame textFrame = shape.getTextFrame();

// Set text alignment to left
textFrame.getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
textFrame.getParagraphs().get_Item(0).getParagraphFormat().setAlignment(TextAlignment.Left);

// Clear any default title text
textFrame.setText("Some text .... "); // Set your desired text here

// Optionally, remove the empty slide if not needed
ppt.getSlides().remove(emptySlide);

Next Steps

  1. Test the above code to see if it resolves the alignment issue.
  2. If you still encounter the “Click to edit Master title style” text, ensure that you are not using a layout that has a title placeholder.
  3. Adjust any other text frame properties as needed to fit your design requirements.

Please implement these changes and let me know if it resolves your issue!

@Tim49,

I tested your scenario using the latest version of Aspose.Slides for Java 25.6. Using a basic PowerPoint presentation file, everything functioned correctly and as expected. I set the text alignment to left for the shapes on the new slides. Here is my sample code.
e.g.,
Sample code:

        // Load the existing presentation
        Presentation pres = new Presentation("d:\\files\\Brush Skript MT.pptx");

        // Get a blank layout
        IMasterLayoutSlideCollection layoutSlides = pres.getMasters().get_Item(0).getLayoutSlides();
        ILayoutSlide blankLayout = layoutSlides.getByType(SlideLayoutType.Blank);

        if (blankLayout == null) {
            blankLayout = layoutSlides.add(SlideLayoutType.Blank, null);
        }

        // Add an empty slide based on the truly blank layout
        ISlide slide = pres.getSlides().addEmptySlide(blankLayout);

        // Add a text frame manually instead of insertFromHtml
        float x = 50;
        float y = 50;
        float width = 600;
        float height = 100;
        IAutoShape textShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, x, y, width, height);

        textShape.getTextFrame().setText("Your free text goes here.");

        // Set font and left alignment
        IPortion portion = textShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
        portion.getPortionFormat().setFontHeight(14);

        textShape.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().setAlignment(TextAlignment.Left);
        
        
        textShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, x , y+200, width, height);

        textShape.getTextFrame().setText("This is new text.");

        // Set font and left alignment
        portion = textShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
        portion.getPortionFormat().setFontHeight(14);

        textShape.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().setAlignment(TextAlignment.Left);

        textShape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
        
        // Save result
        pres.save("d:\\files\\output1.pptx", com.aspose.slides.SaveFormat.Pptx);

Please find attached the zipped archive having input and output PowerPoint presentation files for your reference.
output1.zip (30.6 KB)

In case you still find any issue, kindly do provide sample (runnable) code and PowerPoint presentation files. We will check your issue soon.

Hi amjad.sahi,
thanks very much for the additional feedback which I did not expect.

But based on the suggestion of Professionalize.Discourse I found the following solution in the meantime:

String notes = "<!DOCTYPE html> <html> <body> <h2>Text Header</h2>" + "<p>" + "Some text line .."
        + "</p>" + "<p>" + "second text line .." + "</p>" + "</body> </html>";

// Add a new blank layout
IMasterLayoutSlideCollection layoutSlides = ppt.getMasters().get_Item(0).getLayoutSlides();
blankLayout = layoutSlides.getByType(SlideLayoutType.Blank);
if (blankLayout == null) {
    blankLayout = layoutSlides.add(SlideLayoutType.Blank, null);
}

ISlide[] notesSlides = slides.insertFromHtml(slides.size(), notes);

for (ISlide slide : notesSlides) {
    slide.setShowMasterShapes(false);
    veryLigthBackground(slide);

    for (IShape iShape : slide.getShapes()) {
        if (iShape instanceof IAutoShape autoShape) {
            ITextFrame textFrame = autoShape.getTextFrame();
            leftAlignText(textFrame);
            clearDefaultTitles(autoShape, textFrame);
        }
    }
}

private void veryLigthBackground(ISlide slide) {
    slide.getBackground().setType(BackgroundType.OwnBackground);
    slide.getBackground().getFillFormat().setFillType(FillType.Solid);
    slide.getBackground().getFillFormat().getSolidFillColor().setColor(new Color(248, 248, 248));
}

private void clearDefaultTitles(IAutoShape autoShape, ITextFrame textFrame) {
    if (autoShape.getPlaceholder() != null) {
        byte type = autoShape.getPlaceholder().getType();

        switch (type) {
            case PlaceholderType.Title:
            case PlaceholderType.CenteredTitle:
            case PlaceholderType.Subtitle:
                textFrame.setText("");
                break;
        }
    }
}

private void leftAlignText(ITextFrame textFrame) {
    textFrame.getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
    for (IParagraph paragraph : textFrame.getParagraphs()) {
        paragraph.getParagraphFormat().setAlignment(TextAlignment.Left);
    }
}

I prefer to use insertFromHtml() as the text can be very long and might be placed on several new slides.
Thanks anyway!

@Tim49,

Alright, if the suggested approach using the insertFromHtml API meets your requirements, please feel free to proceed with it. Should you have any additional questions or feedback, please do not hesitate to reach out to us.