Skip to content

Commit

Permalink
Merge pull request #86 from DharanitharanA/master
Browse files Browse the repository at this point in the history
894415-Added opening and saving PowerPoint presentation in Google Drive cloud storage samples
  • Loading branch information
Ramaraj-Marimuthu authored Sep 23, 2024
2 parents 39f06e6 + 02d0175 commit 244e9c0
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 0 deletions.
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.8.34205.153
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Open-PowerPoint-document", "Open-PowerPoint-document\Open-PowerPoint-document.csproj", "{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {024A6C01-116E-4483-9B82-338F6A791749}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Open_PowerPoint_document</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.Drive.v3" Version="1.62.0.3155" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;

namespace Open_PowerPoint_document
{
internal class Program
{
static void Main(string[] args)
{
UserCredential credential;
string[] Scopes = { DriveService.Scope.DriveReadonly };
string ApplicationName = "YourAppName";
// Step 1: Open Google Drive with credentials.
using (var cretendialStream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(cretendialStream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}

// Step 2: Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});

// Step 3: Specify the file ID of the PowerPoint presentation you want to open.
string fileId = "YOUR_FILE_ID"; // Replace with the actual file ID YOUR_FILE_ID.

// Step 4: Download the PowerPoint presentation from Google Drive.
var request = service.Files.Get(fileId);
var stream = new MemoryStream();
request.Download(stream);

// Step 5: Save the PowerPoint presentation locally.
using (FileStream fileStream = new FileStream("Output.pptx", FileMode.Create, FileAccess.Write))
{
stream.WriteTo(fileStream);
}
//Dispose the memory stream.
stream.Dispose();
}
}
}
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.8.34205.153
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Save-PowerPoint-document", "Save-PowerPoint-document\Save-PowerPoint-document.csproj", "{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {024A6C01-116E-4483-9B82-338F6A791749}
EndGlobalSection
EndGlobal
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using File = Google.Apis.Drive.v3.Data.File;
using Syncfusion.Presentation;

namespace Save_PowerPoint_document
{
internal class Program
{
static void Main(string[] args)
{
//Create a new instance of PowerPoint Presentation file.
IPresentation pptxDocument = Presentation.Create();

//Add a new slide to file and apply background color.
ISlide slide = pptxDocument.Slides.Add(SlideLayoutType.TitleOnly);
//Specify the fill type and fill color for the slide background.
slide.Background.Fill.FillType = FillType.Solid;
slide.Background.Fill.SolidFill.Color = ColorObject.FromArgb(232, 241, 229);

//Add title content to the slide by accessing the title placeholder of the TitleOnly layout-slide.
IShape titleShape = slide.Shapes[0] as IShape;
titleShape.TextBody.AddParagraph("Company History").HorizontalAlignment = HorizontalAlignmentType.Center;

//Add description content to the slide by adding a new TextBox.
IShape descriptionShape = slide.AddTextBox(53.22, 141.73, 874.19, 77.70);
descriptionShape.TextBody.Text = "IMN Solutions PVT LTD is the software company, established in 1987, by George Milton. The company has been listed as the trusted partner for many high-profile organizations since 1988 and got awards for quality products from reputed organizations.";

//Add bullet points to the slide.
IShape bulletPointsShape = slide.AddTextBox(53.22, 270, 437.90, 116.32);

//Add a paragraph for a bullet point.
IParagraph firstPara = bulletPointsShape.TextBody.AddParagraph("The company acquired the MCY corporation for 20 billion dollars and became the top revenue maker for the year 2015.");
//Format how the bullets should be displayed.
firstPara.ListFormat.Type = ListType.Bulleted;
firstPara.LeftIndent = 35;
firstPara.FirstLineIndent = -35;

//Add another paragraph for the next bullet point.
IParagraph secondPara = bulletPointsShape.TextBody.AddParagraph("The company is participating in top open source projects in automation industry.");
//Format how the bullets should be displayed.
secondPara.ListFormat.Type = ListType.Bulleted;
secondPara.LeftIndent = 35;
secondPara.FirstLineIndent = -35;

//Gets a picture as stream.
FileStream pictureStream = new FileStream(Path.GetFullPath("../../../Data/Image.jpg"), FileMode.Open);
//Adds the picture to a slide by specifying its size and position.
slide.Shapes.AddPicture(pictureStream, 499.79, 238.59, 364.54, 192.16);

//Add an auto-shape to the slide.
IShape stampShape = slide.Shapes.AddShape(AutoShapeType.Explosion1, 48.93, 430.71, 104.13, 80.54);
//Format the auto-shape color by setting the fill type and text.
stampShape.Fill.FillType = FillType.None;
stampShape.TextBody.AddParagraph("IMN").HorizontalAlignment = HorizontalAlignmentType.Center;

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

// Load Google Drive API credentials from a file.
UserCredential credential;
string[] Scopes = { DriveService.Scope.Drive };
string ApplicationName = "YourAppName";

using (var stream1 = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))//Replace with your actual credentials.json
{
string credPath = "token.json";
// Authorize the Google Drive API access.
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream1).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create a new instance of Google Drive service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});

// Create metadata for the file to be uploaded.
var fileMetadata = new File()
{
Name = "Output.pptx", // Name of the file in Google Drive.
MimeType = "application/powerpoint",
};
FilesResource.CreateMediaUpload request;
// Create a memory stream from the PowerPoint presentation.
using (var fs = new MemoryStream(stream.ToArray()))
{
// Create an upload request for Google Drive.
request = service.Files.Create(fileMetadata, fs, "application/powerpoint");
// Upload the file.
request.Upload();
}
//Dispose the memory stream.
stream.Dispose();
pptxDocument.Close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Save_PowerPoint_document</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.Drive.v3" Version="1.62.0.3155" />
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="*" />
</ItemGroup>
</Project>

0 comments on commit 244e9c0

Please sign in to comment.