Skip to content

Commit

Permalink
Merge pull request #64 from SyncfusionExamples/PPT-Google
Browse files Browse the repository at this point in the history
Add samples for opening and saving PowerPoint document in Google cloud storage
  • Loading branch information
MohanaselvamJothi authored Jul 5, 2024
2 parents af2150a + 60e860d commit c2ad70c
Show file tree
Hide file tree
Showing 161 changed files with 149,829 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@ public IActionResult Index()
}
public async Task<IActionResult> EditDocument()
{
//Your AWS Storage Account bucket name
string bucketName = "your-bucket-name";

//Name of the PowerPoint file you want to load from AWS S3
string key = "PowerPointTemplate.pptx";

try
{
//Retrieve the document from AWS S3
MemoryStream stream = await GetDocumentFromS3(bucketName, key);
MemoryStream stream = await GetDocumentFromS3();

//Set the position to the beginning of the MemoryStream
stream.Position = 0;
Expand All @@ -48,7 +42,7 @@ public async Task<IActionResult> EditDocument()
if (shape.TextBody.Text == "Company History")
shape.TextBody.Text = "Company Profile";

//Saving the PowerPoint document to a MemoryStream
//Saving the PowerPoint file to a MemoryStream
MemoryStream outputStream = new MemoryStream();
pptxDocument.Save(outputStream);

Expand All @@ -67,11 +61,15 @@ public async Task<IActionResult> EditDocument()
/// <summary>
/// Download file from AWS S3 cloud storage
/// </summary>
/// <param name="bucketName"></param>
/// <param name="key"></param>
/// <returns></returns>
public async Task<MemoryStream> GetDocumentFromS3(string bucketName, string key)
public async Task<MemoryStream> GetDocumentFromS3()
{
//Your AWS Storage Account bucket name
string bucketName = "your-bucket-name";

//Name of the PowerPoint file you want to load from AWS S3
string key = "PowerPointTemplate.pptx";

//Configure AWS credentials and region
var region = Amazon.RegionEndpoint.USEast1;
var credentials = new Amazon.Runtime.BasicAWSCredentials("your-access-key", "your-secret-key");
Expand All @@ -98,7 +96,6 @@ public async Task<MemoryStream> GetDocumentFromS3(string bucketName, string key)
await response.ResponseStream.CopyToAsync(stream);

return stream;

}
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.309.10" />
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="26.1.39" />
<PackageReference Include="AWSSDK.S3" Version="*" />
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,28 @@ public async Task<IActionResult> UploadDocument()
stampShape.Fill.FillType = FillType.None;
stampShape.TextBody.AddParagraph("IMN").HorizontalAlignment = HorizontalAlignmentType.Center;

//Saves the PowerPoint document to MemoryStream
//Saves the PowerPoint to MemoryStream
MemoryStream stream = new MemoryStream();
pptxDocument.Save(stream);

//Your AWS Storage Account bucket name
string bucketName = "your-bucket-name";

//Name of the PowerPoint file you want to upload
string key = "CreatePowerPoint.pptx";
pptxDocument.Save(stream);

//Upload the document to AWS S3
await UploadDocumentToS3(bucketName, key, stream);
await UploadDocumentToS3(stream);

return Ok("PowerPoint document uploaded to AWS S3 Storage.");
return Ok("PowerPoint uploaded to AWS S3 Storage.");
}
/// <summary>
/// Upload file to AWS S3 cloud storage
/// </summary>
/// <param name="bucketName"></param>
/// <param name="key"></param>
/// <param name="stream"></param>
/// <returns></returns>
public async Task<MemoryStream> UploadDocumentToS3(string bucketName, string key, MemoryStream stream)
public async Task<MemoryStream> UploadDocumentToS3(MemoryStream stream)
{
//Your AWS Storage Account bucket name
string bucketName = "your-bucket-name";

//Name of the PowerPoint file you want to upload
string key = "CreatePowerPoint.pptx";

//Configure AWS credentials and region
var region = Amazon.RegionEndpoint.USEast1;
var credentials = new Amazon.Runtime.BasicAWSCredentials("your-access-key", "your-secret-key");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.309.10" />
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="26.1.39" />
<PackageReference Include="AWSSDK.S3" Version="*" />
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,10 @@ public IActionResult Index()
}
public async Task<IActionResult> EditDocument()
{
//Your Azure Storage Account connection string
string connectionString = "Your_connection_string";

//Name of the Azure Blob Storage container
string containerName = "Your_container_name";

//Name of the Powerpoint file you want to load
string blobName = "PowerPointTemplate.pptx";

try
{
//Retrieve the document from Azure
MemoryStream stream = await GetDocumentFromAzure(connectionString, containerName, blobName);
MemoryStream stream = await GetDocumentFromAzure();

//Set the position to the beginning of the MemoryStream
stream.Position = 0;
Expand All @@ -52,7 +43,7 @@ public async Task<IActionResult> EditDocument()
if (shape.TextBody.Text == "Company History")
shape.TextBody.Text = "Company Profile";

//Saving the PowerPoint document to a MemoryStream
//Saving the PowerPoint file to a MemoryStream
MemoryStream outputStream = new MemoryStream();
pptxDocument.Save(outputStream);

Expand All @@ -61,7 +52,6 @@ public async Task<IActionResult> EditDocument()
fileStreamResult.FileDownloadName = "EditPowerPoint.pptx";
return fileStreamResult;
}

}
catch (Exception ex)
{
Expand All @@ -72,14 +62,21 @@ public async Task<IActionResult> EditDocument()
/// <summary>
/// Download file from Azure Blob cloud storage
/// </summary>
/// <param name="bucketName"></param>
/// <param name="key"></param>
/// <returns></returns>
public async Task<MemoryStream> GetDocumentFromAzure(string connectionString, string containerName, string blobName)
public async Task<MemoryStream> GetDocumentFromAzure()
{
try
{
//Download the PowerPoint document from Azure Blob Storage
//Your Azure Storage Account connection string
string connectionString = "Your_connection_string";

//Name of the Azure Blob Storage container
string containerName = "Your_container_name";

//Name of the Powerpoint file you want to load
string blobName = "PowerPointTemplate.pptx";

//Download the PowerPoint from Azure Blob Storage
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="26.1.40" />
<PackageReference Include="Azure.Storage.Blobs" Version="*" />
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,32 @@ public async Task<IActionResult> UploadDocument()

//Saves the PowerPoint document to MemoryStream
MemoryStream stream = new MemoryStream();
pptxDocument.Save(stream);

//Your Azure Storage Account connection string
string connectionString = "Your_connection_string";

//Name of the Azure Blob Storage container
string containerName = "Your_container_name";

//Name of the PowerPoint file you want to load
string blobName = "CreatePowerPoint.pptx";
pptxDocument.Save(stream);

//Upload the document to azure
await UploadDocumentToAzure(connectionString, containerName, blobName, stream);
await UploadDocumentToAzure(stream);

return Ok("PowerPoint document uploaded to Azure Blob Storage.");
return Ok("PowerPoint uploaded to Azure Blob Storage.");
}
/// <summary>
/// Upload file to Azure Blob cloud storage
/// </summary>
/// <param name="bucketName"></param>
/// <param name="key"></param>
/// <param name="stream"></param>
/// <returns></returns>
public async Task<MemoryStream> UploadDocumentToAzure(string connectionString, string containerName, string blobName, MemoryStream stream)
public async Task<MemoryStream> UploadDocumentToAzure(MemoryStream stream)
{
try
{
//Download the PowerPoint document from Azure Blob Storage
//Your Azure Storage Account connection string
string connectionString = "Your_connection_string";

//Name of the Azure Blob Storage container
string containerName = "Your_container_name";

//Name of the PowerPoint file you want to load
string blobName = "CreatePowerPoint.pptx";

//Download the PowerPoint from Azure Blob Storage
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="26.1.40" />
<PackageReference Include="Azure.Storage.Blobs" Version="*" />
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34928.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Open-PowerPoint-document", "Open-PowerPoint-document\Open-PowerPoint-document.csproj", "{32919A84-EEB2-44C9-90B0-6DE05C353817}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{32919A84-EEB2-44C9-90B0-6DE05C353817}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{32919A84-EEB2-44C9-90B0-6DE05C353817}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32919A84-EEB2-44C9-90B0-6DE05C353817}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32919A84-EEB2-44C9-90B0-6DE05C353817}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2F668BAA-6807-4025-AD13-15BD76F34901}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Storage.V1;
using Microsoft.AspNetCore.Mvc;
using Open_PowerPoint_document.Models;
using Syncfusion.Presentation;
using System.Diagnostics;
using System.IO;

namespace Open_PowerPoint_document.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
return View();
}
public async Task<IActionResult> EditDocument()
{
//Download the file from Google
MemoryStream memoryStream = await GetDocumentFromGoogle();

//Create an instance of PowerPoint Presentation file
using (IPresentation pptxDocument = Presentation.Open(memoryStream))
{
//Get the first slide from the PowerPoint presentation
ISlide slide = pptxDocument.Slides[0];

//Get the first shape of the slide
IShape shape = slide.Shapes[0] as IShape;

//Change the text of the shape
if (shape.TextBody.Text == "Company History")
shape.TextBody.Text = "Company Profile";

//Saving the PowerPoint to a MemoryStream
MemoryStream outputStream = new MemoryStream();
pptxDocument.Save(outputStream);

//Download the PowerPoint file in the browser
FileStreamResult fileStreamResult = new FileStreamResult(outputStream, "application/powerpoint");
fileStreamResult.FileDownloadName = "EditPowerPoint.pptx";
return fileStreamResult;
}
}
/// <summary>
/// Download file from Google
/// </summary>
/// <returns></returns>
public async Task<MemoryStream> GetDocumentFromGoogle()
{
try
{
//Your bucket name
string bucketName = "Your_bucket_name";

//Your service account key file path
string keyPath = "credentials.json";

//Name of the file to download from the Google Cloud Storage
string fileName = "PowerPointTemplate.pptx";

//Create Google Credential from the service account key file
GoogleCredential credential = GoogleCredential.FromFile(keyPath);

//Instantiates a storage client to interact with Google Cloud Storage
StorageClient storageClient = StorageClient.Create(credential);

//Download a file from Google Cloud Storage
MemoryStream memoryStream = new MemoryStream();
await storageClient.DownloadObjectAsync(bucketName, fileName, memoryStream);
memoryStream.Position = 0;

return memoryStream;
}
catch (Exception ex)
{
Console.WriteLine($"Error retrieving document from Google Cloud Storage: {ex.Message}");
throw; // or handle the exception as needed
}
}
public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Open_PowerPoint_document.Models
{
public class ErrorViewModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
Loading

0 comments on commit c2ad70c

Please sign in to comment.