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

How can I insert an image from a Xamarin Resource into a RadFlowDocument

3 Answers 662 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 2
Veteran
Chris asked on 08 Mar 2021, 04:37 PM

I am using RadFlowDocument to create a PDF that will be shared by the user. This is a Xamarin Forms app.

I am finding little documentation specific to using RadFlowDocument in the Xamarin context.

The RadFlowDocumentEditor.InsertImageInline method takes a Stream but I don't know how to get a Stream from a Xamarin Forms resource.

3 Answers, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 08 Mar 2021, 04:51 PM

Hello Chris,

The Telerik forums are not a good place to get help for this as the underlying problem is "how to get a stream from an embedded resource". This is unrelated to Telerik UI for Xamarin (even though you're using the stream in the document).

I did a quick search and found an answer on StackOverflow https://stackoverflow.com/a/41728728/1406210 

Regards,
Lance | Manager Technical Support
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Chris
Top achievements
Rank 2
Veteran
answered on 08 Mar 2021, 06:06 PM

Thanks Mark.

Yes, sorry, I found the same resources as you, I was kinda hopping I would get a "no need to get a stream, you can just use to the Xamarin Resource/ImageSource directly in your RadFlowDocument like this...".

 

0
Lance | Manager Technical Support
Telerik team
answered on 08 Mar 2021, 07:29 PM

Hello Chris,

Ah yeah. The Telerik Document Processing Libraries (which is what UI for Xamarin is bundled with) doesn't have platform-specific features like that. Instead, it is intentionally compatible with all platforms and an encoded .NET Stream or byte[] is the best way to do that.

Extension and-or Helper Method

If you have a lot of images as embedded resources, you can just write a little helper method to make it easier.

For example:

public static class MyExtensionMethods
{
    public static Stream GetEmbeddedResourceStream(string resourceFileName)
    {
        var imagePath = $"ProjectNamespace.ImagesFolderName.{resourceFileName}";
        
        return typeof(MyExtensionMethods).GetTypeInfo().Assembly.GetManifestResourceStream(imagePath);
    }
}

 

You could also write an extension method for the RadFlowDocumentEditor (even or FlowDocument itself) and insert a Floating Image or an Inline Image.

However, this is only really helpful if you're using the same image settings every time.  For example, like a repetitive Watermark placement

public static void AddWatermarkImage(this RadFlowDocumentEditor editor, string resourceFileName)
{
    var imagePath = $"MyApp.Portable.Images.{resourceFileName}";
    var imageFileExtension = Path.GetExtension(imagePath);

    using (var imgStream =typeof(MyExtensionMethods).GetTypeInfo().Assembly.GetManifestResourceStream(imagePath))
    {
        editor.InsertFloatingImage(imgStream, imageFileExtension, new Telerik.Documents.Primitives.Size(118, 28));
    }
}

then in your main logic, you just call it like this:

// somewhere  in the app
editor.AddWatermarkImage("watermark.png");

 

Tip 1 - Avoid using Xamarin.Forms.ImageSource

The problem with trying to use the ImageSource type is that it's already too late by the time you have that object. It's just an abstraction (i.e. you wont find a byte[] or Stream or pixel buffer). So it's easier and more reliable to write your code around stream or byte array.

If you'd like to see a nice custom way to handle a platform abstraction and load the native project image, take a look at the way our developers handle this in the official demos. https://github.com/telerik/telerik-xamarin-forms-samples/blob/01598d71dea101ab565a9d43076c3244936bfb4d/QSF/QSF/Examples/PdfProcessingControl/FirstLookExample/FirstLookViewModel.cs#L144-L154 

Tip 2 - Doc Processing Team

I normally don't like to bring up Support Tickets when in the context of forums because it's 'salesy' and we treat every case with the same amount of dedication, whether you have paid support or not. The only difference with paid support is a high-priority SLA (<24hr, phone assistance, remote web assistance, etc).

That being said, if you have any use-specific questions about Document Processing, I recommend opening a Support ticket here and we can change the product type to the internal Document Processing development team.

Regards,
Lance | Manager Technical Support
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
General Discussions
Asked by
Chris
Top achievements
Rank 2
Veteran
Answers by
Lance | Manager Technical Support
Telerik team
Chris
Top achievements
Rank 2
Veteran
Share this question
or