How to convert a PowerPoint file to PDF in Xamarin?
The Syncfusion® PowerPoint library do not have direct support for converting the PowerPoint file to PDF document in Xamarin platform. But this can be achieved by converting the PowerPoint file to PDF using a web service and get the converted PDF document in Xamarin application. The following code example explains how to create and use the web service in Xamarin platform.
Web Service code:
public class PowerPointController : ApiController
{
/// <summary>
/// Converts the PowerPoint to PDF document.
/// </summary>
[AcceptVerbs("Post")]
public HttpResponseMessage ConvertToPdf()
{
HttpResponseMessage httpResponseMessage;
using (Stream stream = Request.Content.ReadAsStreamAsync().Result)
{
try
{
//Opens the PowerPoint from stream
using (IPresentation pptxDoc = Presentation.Open(stream))
{
//Initializes the ChartToImageConverter for converting charts during PowerPoint to PDF conversion
pptxDoc.ChartToImageConverter = new ChartToImageConverter
{
ScalingMode = Syncfusion.OfficeChart.ScalingMode.Best
};
//Creates an instance of the PresentationToPdfConverterSettings
PresentationToPdfConverterSettings settings = new PresentationToPdfConverterSettings
{
ShowHiddenSlides = true
};
//Converts PowerPoint into PDF document
PdfDocument pdfDocument = PresentationToPdfConverter.Convert(pptxDoc, settings);
//Saves the PDF document to response stream
MemoryStream memoryStream = new MemoryStream();
pdfDocument.Save(memoryStream);
memoryStream.Position = 0;
httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(memoryStream)
};
pdfDocument.Dispose();
}
}
catch
{
httpResponseMessage = new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
}
}
return httpResponseMessage;
}
}
The below code snippet explains how to use the above web service to the Xamarin applications to display the PowerPoint file as PDF document in Xamarin application.
private async void PptxToPdfViewer(object sender, EventArgs e)
{
string resourcePath = "XamarinFormsPDFConversion.Data.Template.pptx";
Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream(resourcePath);
//Open a PowerPoint
IPresentation pptxDoc = Syncfusion.Presentation.Presentation.Open(stream);
//Save the Presentation to stream
MemoryStream inputStream = new MemoryStream();
pptxDoc.Save(inputStream);
pptxDoc.Close();
stream.Dispose();
inputStream.Position = 0;
// Creates new instance of HttpClient to access service.
HttpClient client = new HttpClient();
// Gets Uri
string requestUri = "http://js.syncfusion.com/demos/ioservices/api/powerpoint/converttopdf";
// Posts input PowerPoint document to service and gets resultant PDF as content of HttpResponseMessage
HttpResponseMessage response = await client.PostAsync(requestUri, new StreamContent(inputStream));
//Dispose the input stream and client instances.
inputStream.Dispose();
client.Dispose();
MemoryStream outputStream = null;
// Gets PDF from content stream if service got success.
if (response.IsSuccessStatusCode)
{
var responseHeaders = response.Headers;
outputStream = new MemoryStream(await response.Content.ReadAsByteArrayAsync());
// Dispose the response instance.
response.Dispose();
}
outputStream.Position = 0;
if (Device.RuntimePlatform == Device.UWP)
Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("ConvertedPDF.pdf", "application/pdf", outputStream);
else
Xamarin.Forms.DependencyService.Get<ISave>().Save("ConvertedPDF.pdf", "application/pdf", outputStream);
}
The below URL is the Syncfusion® demo hosted web service (instead of local web service) from which we can convert the PowerPoint file to PDF document with “Essential Presentation” watermark. To avoid this watermark, please run the below provided web service locally and replace the below mentioned Syncfusion® demo URL with your local host URL.
Syncfusion demo URL: string demoURL = “http://js.syncfusion.com/demos/ioservices/api/powerpoint/converttopdf”;
Xamarin Forms PDF conversion sample:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/XamarinFormsPDFConversion-656665052.zip
ASP.NET Web API (service) application:
https://www.syncfusion.com/downloads/support/directtrac/221483/ze/WebServices-2046617801
Take a moment to peruse the documentation, where you can find more details with code examples. Refer here to explore the rich set of Syncfusion® Essential® PowerPoint features.