Aspose.Slides for Xamarin

Overview

Xamarin is a framework used for mobile development in .NET C#. Xamarin has tools and libraries that extend the capabilities of the .NET platform. It allows developers to build applications for the Android operating system.

Aspose.Slides API works on the Xamarin platform. To achieve this, the Aspose.Slides .NET package adds a separate DLL for Xamarin. Aspose.Slides for Xamarin supports most of the features available in the .NET version:

  • converting and viewing presentations.
  • editing contents in presentations: text, shapes, charts, SmartArt, audio/video, fonts, etc.
  • handling/dealing with animation, 2D effects, WordArt, etc.
  • handling/dealing with metadata and document properties.
  • printing, cloning, merging, comparing, splitting, etc.

We provided a comparison of the full features in another section close to the bottom of this page.

In Aspose.Slides for Xamarin API, the classes, namespaces, logic, and behavior are as similar as possible to the .NET version. You can migrate your Aspose.Slides .NET applications to Xamarin with minimal costs.

Quick Example

You can use Aspose.Slides for Xamarin to build and utilize your C# application through Slides for Android.

We are providing an example of Android via Xamarin application that uses Aspose.Slides to display presentation slides and adds a new shape on the slide on touch. You can find the full source of the examples on GitHub.

Let’s start by creating a Xamarin Android App:

todo:image_alt_text

First, we create a content layout that will contain an image view, Prev, and Next buttons:

todo:image_alt_text

XML - content_main.xml - Create content layout

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation=    "vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:showIn="@layout/activity_main">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/linearLayout1">
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView"
            android:scaleType="fitCenter" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:id="@+id/linearLayout2">
        <Button
            android:text="Prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/buttonPrev" />
        <Button
            android:text="Next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/buttonNext"/>
    </LinearLayout>
</LinearLayout>

Here, we reference the “Aspose.Slides.Droid.dll” library that includes a sample presentation (“HelloWorld.pptx”) into Xamarin application Assets and adds its initialization to MainActivity:

C# - MainActivity.cs - Initialization

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    private Aspose.Slides.Presentation presentation;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);
    }

    protected override void OnResume()
    {
        if (presentation == null)
        {
            using (Stream input = Assets.Open("HelloWorld.pptx"))
            {
                presentation = new Aspose.Slides.Presentation(input);
            }
        }
    }

    protected override void OnPause()
    {
        if (presentation != null)
        {
            presentation.Dispose();
            presentation = null;
        }
    }
}

Let’s add the function to display the Prev and Next slides on the tapping of buttons:

C# - MainActivity.cs - Display slides on Prev and Next button click

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    private Button buttonNext;
    private Button buttonPrev;
    ImageView imageView;

    private Aspose.Slides.Presentation presentation;

    private int currentSlideNumber;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);
    }

    protected override void OnResume()
    {
        base.OnResume();
        LoadPresentation();
        currentSlideNumber = 0;
        if (buttonNext == null)
        {
            buttonNext = FindViewById<Button>(Resource.Id.buttonNext);
        }

        if (buttonPrev == null)
        {
            buttonPrev = FindViewById<Button>(Resource.Id.buttonPrev);
        }

        if(imageView == null)
        {
            imageView= FindViewById<ImageView>(Resource.Id.imageView);
        }

        buttonNext.Click += ButtonNext_Click;
        buttonPrev.Click += ButtonPrev_Click;
        RefreshButtonsStatus();
        ShowSlide(currentSlideNumber);
    }

    private void ButtonNext_Click(object sender, System.EventArgs e)
    {
        if (currentSlideNumber > (presentation.Slides.Count - 1))
        {
            return;
        }

        ShowSlide(++currentSlideNumber);
        RefreshButtonsStatus();
    }

    private void ButtonPrev_Click(object sender, System.EventArgs e)
    {
        if (currentSlideNumber == 0)
        {
            return;
        }

        ShowSlide(--currentSlideNumber);
        RefreshButtonsStatus();
    }

    protected override void OnPause()
    {
        base.OnPause();
        if (buttonNext != null)
        {
            buttonNext.Dispose();
            buttonNext = null;
        }

        if (buttonPrev != null)
        {
            buttonPrev.Dispose();
            buttonPrev = null;
        }

        if(imageView != null)
        {
            imageView.Dispose();
            imageView = null;
        }

        DisposePresentation();
    }

    private void RefreshButtonsStatus()
    {
        buttonNext.Enabled = currentSlideNumber < (presentation.Slides.Count - 1);
        buttonPrev.Enabled = currentSlideNumber > 0;
    }

    private void ShowSlide(int slideNumber)
    {
        Aspose.Slides.Drawing.Xamarin.Size size = presentation.SlideSize.Size.ToSize();
        Aspose.Slides.Drawing.Xamarin.Bitmap bitmap = presentation.Slides[slideNumber].GetThumbnail(size);
        imageView.SetImageBitmap(bitmap.ToNativeBitmap());
    }

    private void LoadPresentation()
    {
        if(presentation != null)
        {
            return;
        }

        using (Stream input = Assets.Open("HelloWorld.pptx"))
        {
            presentation = new Aspose.Slides.Presentation(input);
        }
    }

    private void DisposePresentation()
    {
        if(presentation == null)
        {
            return;
        }
        
        presentation.Dispose();
        presentation = null;
    }

}

Finally, let’s implement a function to add an ellipse shape on a touch on the slide:

C# - MainActivity.cs - Add ellipse by slide click

 private void ImageView_Touch(object sender, Android.Views.View.TouchEventArgs e)
{
    int[] location = new int[2];
    imageView.GetLocationOnScreen(location);
    int x = (int)e.Event.GetX();
    int y = (int)e.Event.GetY();
    int posX = x - location[0];
    int posY = y - location[0];
    
    Aspose.Slides.Drawing.Xamarin.Size presSize = presentation.SlideSize.Size.ToSize();

    float coeffX = (float)presSize.Width / imageView.Width;
    float coeffY = (float)presSize.Height / imageView.Height;
    int presPosX = (int)(posX * coeffX);
    int presPosY = (int)(posY * coeffY);
    int width = presSize.Width / 50;

    int height = width;
    Aspose.Slides.IAutoShape ellipse = presentation.Slides[currentSlideNumber].Shapes.AddAutoShape(Aspose.Slides.ShapeType.Ellipse, presPosX, presPosY, width, height);
    ellipse.FillFormat.FillType = Aspose.Slides.FillType.Solid;

    Random random = new Random();
    Aspose.Slides.Drawing.Xamarin.Color slidesColor = Aspose.Slides.Drawing.Xamarin.Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
    ellipse.FillFormat.SolidFillColor.Color = slidesColor;
    ShowSlide(currentSlideNumber);
}

Each click on the presentation slide causes a random colored ellipse to be added:

todo:image_alt_text

Supported Features

FEATURES  Aspose.Slides for .NET   Aspose.Slides for Xamarin
Presentation features    
Create new presentations  tick tick
PowerPoint 97 - 2003 formats open/save  tick tick
PowerPoint 2007 formats open/save  tick tick
PowerPoint 2010 extensions support  tick tick
PowerPoint 2013 extensions support  tick tick
PowerPoint 2016 features support  restricted restricted
PowerPoint 2019 features support  restricted  restricted
PPT 2 PPTX conversion  tick tick
PPTX 2 PPT conversion  tick tick
PPTX in PPT  restricted restricted
Themes processing  tick tick
Macros processing  tick tick
Document properties processing  tick tick
Password protection  tick tick
Fast text extraction  tick tick
Embedding fonts  tick tick
Comments rendering  tick   tick
Interrupting of long-running tasks  tick tick  
Export formats:     
PDF  tick tick
XPS  tick tick
HTML  tick tick
TIFF  tick circle
ODP  restricted  restricted
SWF  restricted restricted
SVG  tick tick
Import formats:     
HTML  restricted restricted
ODP  tick tick
THMX  tick tick
Master slides features:     
Accessing all existing master slide  tick tick
Creating/removing master slides  tick tick
Cloning master slides  tick tick
Layout slides features:     
Accessing all existing layout slides  tick tick
Creating/removing layout slides  tick tick
Cloning layout slides  tick tick
Slide features:     
Accessing all existing slides  tick tick
Creating/removing slides  tick tick
Cloning slides  tick tick
Exporting slides to images  tick tick
Creating/editing/removing slide sections  tick tick
Notes slides features    
Accessing all existing notes slides  tick tick
Shape features:     
Accessing all slide shapes  tick tick
Adding new shapes  tick tick
Cloning shapes  tick tick
Exporting separate shapes to images  tick tick
Supported shape types:     
All predefined shape types  tick tick
Picture frames  tick tick
Tables  tick tick
Charts  tick tick
SmartArt  tick tick
Legacy diagram  tick tick
WordArt  tick tick
OLE, ActiveX objects  restricted restricted
Video frames  tick tick
Audio frames  tick tick
Connectors  tick tick
Group shape features:     
Accessing group shapes  tick tick
Creating group shapes  tick tick
Ungrouping existing group shapes  tick tick
Shape effects features:     
2D effects  restricted restricted
3D effects  circle circle
Text features:     
Paragraphs formatting  tick tick
Portions formatting  tick tick
Animation Features:     
Export animation to SWF  circle circle
Export animation to HTML  circle circle