How to Replace a Text Portion in a Presentation with HTML Content in C#?

I am using a template and replace the dynamic text at runtime. In this I want to replace a portion content with html content (i.e base64 image, paragraph) but not getting any option to do that.

Any help would be much appreciated!

foreach (Paragraph para in tb[i].Paragraphs)
                {
                    foreach (Portion port in para.Portions)
                    {
                        var data = replaceText.FirstOrDefault(x => port.Text.Contains(x.Key));
                        if (data.Value != null)
                        {
                            string str = port.Text;

                            int idx = str.IndexOf(ss.Key);

                            string strStartText = str.Substring(0, idx);

                            string strEndText = str.Substring(idx + ss.Key.Length, str.Length - 1 - (idx + ss.Key.Length - 1));

                            port.Text = strStartText + ss.Value + strEndText;
                        }
                    }
                }

@vipinmishra,
Thank you for posting the query.

Unfortunately, I have not found such an ability you described. I logged the issue with ID SLIDESNET-42877 in our tracking system. Our development team will consider implementing this feature. You will be notified when it is done.

As a workaround, please try to use the next way:

// TextFrame object
textFrame.Paragraphs.AddFromHtml(htmlString);

Documents: Import HTML Text in Paragraphs
API Reference: IParagraphCollection Interface

If I understand correctly, your presentations contain text fields (<MY_TEXT>, for example) and you would like to replace them with HTML content. Could you please confirm?

Yes.
I also try your workaround it is working fine but not support for "<img " tag.

@vipinmishra,
Unfortunately, I was unable to insert an image into a paragraph by using PowerPoint. Could you please share a presentation file with the desired result?

I have attached one image of my ppt.2021-11-01_11-49-02.jpg (194.8 KB)

In this I want to render HTML in “<Background,<Execution”.

@vipinmishra,
Far as I know, PowerPoint presentation formats do not allow inserting images between text portions. You can try to use picture frames like this:

Bitmap bitmap = new Bitmap("picture.jpg");
IPPImage image = presentation.Images.AddImage(bitmap);

IPictureFrame pictureFrame = slide.Shapes.AddPictureFrame(
    ShapeType.Rectangle, 50, 60, image.Width, image.Height, image);

More examples: Picture Frame
API Reference: IPPImage Interface, IShapeCollection Interface, IPictureFrame Interface

Hi ,
I have tried to add html in text portion but not getting same format of html. I have attached two screenshots for you reference .
Please help me to get same format

code.png (88.8 KB)

PPT_2021-11-09_12-23-24.jpg (314.6 KB)
Web_2021-11-09_12-22-57.jpg (420.4 KB)

@vipinmishra,
Unfortunately, I cannot use your code example to check the problem (the replaceText is an unknown symbol). Please share the following:

  • input presentation file
  • comprehensive code example

Please find attached file. replaceText is a dictionary where we maintain key and value.

Code.txt.pdf (3.3 KB)
template.pptx.pdf (6.8 MB)

@vipinmishra,
I need to know the HTML strings you used for replacement. Please share the following:

  • dictionary with the HTML samples you passed in the replaceText parameter
  • your output presentation file

replaceText.pdf (5.8 KB)
templates.pptx.pdf (6.8 MB)

@vipinmishra,
Unfortunately, I was unable to use your code examples for reproducing the problem. Please try to isolate the problem and simplify the code example. Then share it again.

Hi @Andrey_Potapov,
We want set character limit in rendered HTML output content. Is there any option in aspose for that where we can handle it.

@vipinmishra,
Please share a presentation file and simple code example demonstrating the problem.

Hi,
Please check attached ppt and code sample.

code.jpg (82.5 KB)
test_11_18_2021.pptx (728.3 KB)
template.pptx (743.2 KB)

@vipinmishra,
Unfortunately, there is no possibility to limit rendered HTML content. As a workaround, after adding HTML content, you can check ashape.TextFrame.Text.Length property and remove text portions from paragraphs from the end of the text.

API Reference: ITextFrame Interface

Also, Shrink text on overflow option in PowerPoint may be useful for your purposes: shrink_text.png (307.8 KB).

@vipinmishra,
Our developers investigated the issue SLIDESNET-42877. PowerPoint does not support the text wrapping around objects. You can use the following ways to import HTML into presentations:

string wordFileName = path + "document.docx";
string substituteImageFileName = path + "oleImage.jpg";
string presentationFileName = path + "result.pptx";

// Create a presentation with the embedded Word document
using (Presentation pres = new Presentation())
{
    float slideWidth = pres.SlideSize.Size.Width;
    float slideHeight = pres.SlideSize.Size.Height;

    // Create a Word document with wrapped text
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    // Correct page properties
    builder.PageSetup.PageWidth = slideWidth;
    builder.PageSetup.PageHeight = slideHeight;
    builder.PageSetup.LeftMargin = 20;
    builder.PageSetup.TopMargin = 20;

    builder.InsertHtml("<p>Some text with an img tag! <img src=http://cdn.images.express.co.uk/img/dynamic/151/590x/secondary/red-galaxy-454959.jpg /></p>");

    doc.Save(substituteImageFileName, Aspose.Words.SaveFormat.Jpeg); // save substitution image for OleObjectFrame
    doc.Save(wordFileName, Aspose.Words.SaveFormat.Docx);

    // Create ole object frame
    byte[] fileBytes = File.ReadAllBytes(wordFileName);
    IOleEmbeddedDataInfo dataInfo = new OleEmbeddedDataInfo(fileBytes, "docx");

    IOleObjectFrame oleFrame = pres.Slides[0].Shapes.AddOleObjectFrame(0f, 0f, slideWidth, slideHeight, dataInfo);
    oleFrame.SubstitutePictureFormat.Picture.Image =
        pres.Images.AddImage(new FileStream(substituteImageFileName, FileMode.Open));

    pres.Save(presentationFileName, Aspose.Slides.Export.SaveFormat.Pptx);
}