Skip to content

Commit 244e9c0

Browse files
Merge pull request #86 from DharanitharanA/master
894415-Added opening and saving PowerPoint presentation in Google Drive cloud storage samples
2 parents 39f06e6 + 02d0175 commit 244e9c0

File tree

7 files changed

+236
-0
lines changed

7 files changed

+236
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34205.153
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Open-PowerPoint-document", "Open-PowerPoint-document\Open-PowerPoint-document.csproj", "{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {024A6C01-116E-4483-9B82-338F6A791749}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Open_PowerPoint_document</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Google.Apis.Drive.v3" Version="1.62.0.3155" />
13+
</ItemGroup>
14+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Google.Apis.Auth.OAuth2;
2+
using Google.Apis.Drive.v3;
3+
using Google.Apis.Services;
4+
using Google.Apis.Util.Store;
5+
6+
namespace Open_PowerPoint_document
7+
{
8+
internal class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
UserCredential credential;
13+
string[] Scopes = { DriveService.Scope.DriveReadonly };
14+
string ApplicationName = "YourAppName";
15+
// Step 1: Open Google Drive with credentials.
16+
using (var cretendialStream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
17+
{
18+
string credPath = "token.json";
19+
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
20+
GoogleClientSecrets.Load(cretendialStream).Secrets,
21+
Scopes,
22+
"user",
23+
CancellationToken.None,
24+
new FileDataStore(credPath, true)).Result;
25+
}
26+
27+
// Step 2: Create Drive API service.
28+
var service = new DriveService(new BaseClientService.Initializer()
29+
{
30+
HttpClientInitializer = credential,
31+
ApplicationName = ApplicationName,
32+
});
33+
34+
// Step 3: Specify the file ID of the PowerPoint presentation you want to open.
35+
string fileId = "YOUR_FILE_ID"; // Replace with the actual file ID YOUR_FILE_ID.
36+
37+
// Step 4: Download the PowerPoint presentation from Google Drive.
38+
var request = service.Files.Get(fileId);
39+
var stream = new MemoryStream();
40+
request.Download(stream);
41+
42+
// Step 5: Save the PowerPoint presentation locally.
43+
using (FileStream fileStream = new FileStream("Output.pptx", FileMode.Create, FileAccess.Write))
44+
{
45+
stream.WriteTo(fileStream);
46+
}
47+
//Dispose the memory stream.
48+
stream.Dispose();
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34205.153
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Save-PowerPoint-document", "Save-PowerPoint-document\Save-PowerPoint-document.csproj", "{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {024A6C01-116E-4483-9B82-338F6A791749}
24+
EndGlobalSection
25+
EndGlobal
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using Google.Apis.Auth.OAuth2;
2+
using Google.Apis.Drive.v3;
3+
using Google.Apis.Services;
4+
using Google.Apis.Util.Store;
5+
using File = Google.Apis.Drive.v3.Data.File;
6+
using Syncfusion.Presentation;
7+
8+
namespace Save_PowerPoint_document
9+
{
10+
internal class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
//Create a new instance of PowerPoint Presentation file.
15+
IPresentation pptxDocument = Presentation.Create();
16+
17+
//Add a new slide to file and apply background color.
18+
ISlide slide = pptxDocument.Slides.Add(SlideLayoutType.TitleOnly);
19+
//Specify the fill type and fill color for the slide background.
20+
slide.Background.Fill.FillType = FillType.Solid;
21+
slide.Background.Fill.SolidFill.Color = ColorObject.FromArgb(232, 241, 229);
22+
23+
//Add title content to the slide by accessing the title placeholder of the TitleOnly layout-slide.
24+
IShape titleShape = slide.Shapes[0] as IShape;
25+
titleShape.TextBody.AddParagraph("Company History").HorizontalAlignment = HorizontalAlignmentType.Center;
26+
27+
//Add description content to the slide by adding a new TextBox.
28+
IShape descriptionShape = slide.AddTextBox(53.22, 141.73, 874.19, 77.70);
29+
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.";
30+
31+
//Add bullet points to the slide.
32+
IShape bulletPointsShape = slide.AddTextBox(53.22, 270, 437.90, 116.32);
33+
34+
//Add a paragraph for a bullet point.
35+
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.");
36+
//Format how the bullets should be displayed.
37+
firstPara.ListFormat.Type = ListType.Bulleted;
38+
firstPara.LeftIndent = 35;
39+
firstPara.FirstLineIndent = -35;
40+
41+
//Add another paragraph for the next bullet point.
42+
IParagraph secondPara = bulletPointsShape.TextBody.AddParagraph("The company is participating in top open source projects in automation industry.");
43+
//Format how the bullets should be displayed.
44+
secondPara.ListFormat.Type = ListType.Bulleted;
45+
secondPara.LeftIndent = 35;
46+
secondPara.FirstLineIndent = -35;
47+
48+
//Gets a picture as stream.
49+
FileStream pictureStream = new FileStream(Path.GetFullPath("../../../Data/Image.jpg"), FileMode.Open);
50+
//Adds the picture to a slide by specifying its size and position.
51+
slide.Shapes.AddPicture(pictureStream, 499.79, 238.59, 364.54, 192.16);
52+
53+
//Add an auto-shape to the slide.
54+
IShape stampShape = slide.Shapes.AddShape(AutoShapeType.Explosion1, 48.93, 430.71, 104.13, 80.54);
55+
//Format the auto-shape color by setting the fill type and text.
56+
stampShape.Fill.FillType = FillType.None;
57+
stampShape.TextBody.AddParagraph("IMN").HorizontalAlignment = HorizontalAlignmentType.Center;
58+
59+
//Saves the PowerPoint to MemoryStream.
60+
MemoryStream stream = new MemoryStream();
61+
pptxDocument.Save(stream);
62+
63+
// Load Google Drive API credentials from a file.
64+
UserCredential credential;
65+
string[] Scopes = { DriveService.Scope.Drive };
66+
string ApplicationName = "YourAppName";
67+
68+
using (var stream1 = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))//Replace with your actual credentials.json
69+
{
70+
string credPath = "token.json";
71+
// Authorize the Google Drive API access.
72+
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
73+
GoogleClientSecrets.Load(stream1).Secrets,
74+
Scopes,
75+
"user",
76+
CancellationToken.None,
77+
new FileDataStore(credPath, true)).Result;
78+
}
79+
// Create a new instance of Google Drive service.
80+
var service = new DriveService(new BaseClientService.Initializer()
81+
{
82+
HttpClientInitializer = credential,
83+
ApplicationName = ApplicationName,
84+
});
85+
86+
// Create metadata for the file to be uploaded.
87+
var fileMetadata = new File()
88+
{
89+
Name = "Output.pptx", // Name of the file in Google Drive.
90+
MimeType = "application/powerpoint",
91+
};
92+
FilesResource.CreateMediaUpload request;
93+
// Create a memory stream from the PowerPoint presentation.
94+
using (var fs = new MemoryStream(stream.ToArray()))
95+
{
96+
// Create an upload request for Google Drive.
97+
request = service.Files.Create(fileMetadata, fs, "application/powerpoint");
98+
// Upload the file.
99+
request.Upload();
100+
}
101+
//Dispose the memory stream.
102+
stream.Dispose();
103+
pptxDocument.Close();
104+
}
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Save_PowerPoint_document</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Google.Apis.Drive.v3" Version="1.62.0.3155" />
13+
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="*" />
14+
</ItemGroup>
15+
</Project>

0 commit comments

Comments
 (0)