Skip to content

Commit e202827

Browse files
894415-Added opening and saving PowerPoint presentation in Google Drive cloud storage samples
1 parent 8a703a9 commit e202827

File tree

7 files changed

+238
-0
lines changed

7 files changed

+238
-0
lines changed
Lines changed: 25 additions & 0 deletions
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
Lines changed: 14 additions & 0 deletions
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>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
48+
}
49+
}
50+
}
Lines changed: 25 additions & 0 deletions
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
21 KB
Loading
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
20+
//Specify the fill type and fill color for the slide background
21+
slide.Background.Fill.FillType = FillType.Solid;
22+
slide.Background.Fill.SolidFill.Color = ColorObject.FromArgb(232, 241, 229);
23+
24+
//Add title content to the slide by accessing the title placeholder of the TitleOnly layout-slide
25+
IShape titleShape = slide.Shapes[0] as IShape;
26+
titleShape.TextBody.AddParagraph("Company History").HorizontalAlignment = HorizontalAlignmentType.Center;
27+
28+
//Add description content to the slide by adding a new TextBox
29+
IShape descriptionShape = slide.AddTextBox(53.22, 141.73, 874.19, 77.70);
30+
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.";
31+
32+
//Add bullet points to the slide
33+
IShape bulletPointsShape = slide.AddTextBox(53.22, 270, 437.90, 116.32);
34+
35+
//Add a paragraph for a bullet point
36+
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.");
37+
38+
//Format how the bullets should be displayed
39+
firstPara.ListFormat.Type = ListType.Bulleted;
40+
firstPara.LeftIndent = 35;
41+
firstPara.FirstLineIndent = -35;
42+
43+
//Add another paragraph for the next bullet point
44+
IParagraph secondPara = bulletPointsShape.TextBody.AddParagraph("The company is participating in top open source projects in automation industry.");
45+
46+
//Format how the bullets should be displayed
47+
secondPara.ListFormat.Type = ListType.Bulleted;
48+
secondPara.LeftIndent = 35;
49+
secondPara.FirstLineIndent = -35;
50+
51+
//Gets a picture as stream
52+
FileStream pictureStream = new FileStream(Path.GetFullPath("../../../Output/Image.jpg"), FileMode.Open);
53+
54+
//Adds the picture to a slide by specifying its size and position.
55+
slide.Shapes.AddPicture(pictureStream, 499.79, 238.59, 364.54, 192.16);
56+
57+
//Add an auto-shape to the slide
58+
IShape stampShape = slide.Shapes.AddShape(AutoShapeType.Explosion1, 48.93, 430.71, 104.13, 80.54);
59+
60+
//Format the auto-shape color by setting the fill type and text
61+
stampShape.Fill.FillType = FillType.None;
62+
stampShape.TextBody.AddParagraph("IMN").HorizontalAlignment = HorizontalAlignmentType.Center;
63+
64+
//Saves the PowerPoint to MemoryStream
65+
MemoryStream stream = new MemoryStream();
66+
pptxDocument.Save(stream);
67+
68+
// Load Google Drive API credentials from a file
69+
UserCredential credential;
70+
string[] Scopes = { DriveService.Scope.Drive };
71+
string ApplicationName = "YourAppName";
72+
73+
using (var stream1 = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))//Replace with your actual credentials.json
74+
{
75+
string credPath = "token.json";
76+
// Authorize the Google Drive API access
77+
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
78+
GoogleClientSecrets.Load(stream1).Secrets,
79+
Scopes,
80+
"user",
81+
CancellationToken.None,
82+
new FileDataStore(credPath, true)).Result;
83+
}
84+
// Create a new instance of Google Drive service
85+
var service = new DriveService(new BaseClientService.Initializer()
86+
{
87+
HttpClientInitializer = credential,
88+
ApplicationName = ApplicationName,
89+
});
90+
91+
// Create metadata for the file to be uploaded
92+
var fileMetadata = new File()
93+
{
94+
Name = "Output.pptx", // Name of the file in Google Drive
95+
MimeType = "application/powerpoint",
96+
};
97+
FilesResource.CreateMediaUpload request;
98+
99+
// Create a memory stream from the PowerPoint presentation.
100+
using (var fs = new MemoryStream(stream.ToArray()))
101+
{
102+
// Create an upload request for Google Drive
103+
request = service.Files.Create(fileMetadata, fs, "application/powerpoint");
104+
// Upload the file
105+
request.Upload();
106+
}
107+
}
108+
}
109+
}
Lines changed: 15 additions & 0 deletions
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)