How to find all the images in the Document

6 posts, 0 answers
  1. Bart
    Bart avatar
    7 posts
    Member since:
    Apr 2016

    Posted 29 Apr Link to this post

    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 ?

     

     

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 29 Apr Link to this post

    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 sample 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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Bart
    Bart avatar
    7 posts
    Member since:
    Apr 2016

    Posted 02 May in reply to Dess Link to this post

    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

     

  5. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 02 May Link to this post

    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.
  6. Bart
    Bart avatar
    7 posts
    Member since:
    Apr 2016

    Posted 02 May in reply to Dess Link to this post

    Hi Dess,

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

    regards,

    Bart

  7. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 03 May Link to this post

    Hello Bart,

    Thank you for writing back. 

    If you import a 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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready