This is a migrated thread and some comments may be shown as answers.

How to find all the images in the Document

5 Answers 269 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Bart
Top achievements
Rank 1
Bart asked on 29 Apr 2016, 12:43 PM

Our documents have a couple of images included (they are in rtf format, with some images in the header).

In Word, depending on the status (started, validated, ...), we take one image from the document and show it as a watermark. The other ones we hide.

I'm trying to do the same with the editor, but i can't really find api's to just get a list of images in the Document.

Is there a way to do this ?

 

 

5 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 29 Apr 2016, 01:57 PM
Hello Bart,

Thank you for writing.

RadDocument is the root element for RadRichTextEditor's content. It holds the collection of Sections defined for the RadRichTextEditor content. In order to count how many images you have in the document, you can iterate all sections and traverse them in depth to count the images. Here is demonstrated a sample approach:
int imageCount = 0;
 
private void radButton1_Click(object sender, EventArgs e)
{
    foreach (Section section in this.radRichTextEditor1.Document.Sections)
    {
        foreach (Block block in section.Blocks)
        {
            Paragraph p = block as Paragraph;
            if (p != null)
            {
                foreach (Inline inlineElement in p.Inlines)
                {
                    ImageInline image = inlineElement as ImageInline;
                    if (image != null)
                    {
                        imageCount++;
                    }
                }
            }
        }
    }
    Console.WriteLine(imageCount);
}

Note that it is just a approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.

I hope this information helps. Should you have further questions I would be glad to help.

 Regards,
Dess
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Bart
Top achievements
Rank 1
answered on 02 May 2016, 11:34 AM

Hi Dess,

Thanks for this. I've changed it to:

int imageCount = 0;
            foreach (Section section in viewer.Document.Sections)
            {
                foreach (Section sections in section.Headers.First.Body.Sections)
                {
                    foreach (Block block in sections.Blocks)
                    {
                        Paragraph p = block as Paragraph;
                        if (p != null)
                        {
                            foreach (Inline inlineElement in p.Inlines)
                            {
                                ImageInline image = inlineElement as ImageInline;
                                if (image != null)
                                {
                                    imageCount++;
                                }
                                FloatingImageBlock fib = inlineElement as FloatingImageBlock;
                                if (fib != null) {
                                    imageCount++;
                                }
                            }
                        }
                    }
                }

the annoying thing is that neither FloatingImageBlock and ImageInline have a name property, so I still don't know which images i'm dealing with

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 May 2016, 12:12 PM
Hello Bart,

Thank you for writing back. 

The ImageInline.Title property gives you the image title if you specify it when inserting the image:
Section section = new Section();
Paragraph paragraph = new Paragraph();
ImageInline image;
Telerik.WinControls.RichTextEditor.UI.Size size = new Telerik.WinControls.RichTextEditor.UI.Size(236, 50);
using (MemoryStream ms = new MemoryStream())
{
    Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg").Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    image = new ImageInline(ms, size, "png");
    image.Title = "Penguins";
}
 
paragraph.Inlines.Add(image);
section.Children.Add(paragraph);
this.radRichTextEditor1.Document.Sections.Add(section);

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Dess
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Bart
Top achievements
Rank 1
answered on 02 May 2016, 01:08 PM

Hi Dess,

We simply import an rtf we have already stored in the database.

regards,

Bart

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 May 2016, 06:11 AM | edited on 10 Feb 2022, 12:15 PM
Hello Bart,

Thank you for writing back. 

If you import an RTF file, the images won't have a title. Only the HtmlFormatProvider stores the image's title if it is explicitly specified.

I hope this information helps. If you have any additional questions, please let me know.
Regards,
Dess
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Divya
Top achievements
Rank 1
commented on 10 Feb 2022, 07:30 AM

The link is not working.
Dinko | Tech Support Engineer
Telerik team
commented on 10 Feb 2022, 12:16 PM

Thank you for letting us know. I have replaced the broken link with the correct one: Using HtmlFormatProvider
Tags
RichTextEditor
Asked by
Bart
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Bart
Top achievements
Rank 1
Share this question
or