Skip to content

Commit 02d0175

Browse files
894415-Handled dispose and added XML comments for Google Drive cloud samples
1 parent f5aa797 commit 02d0175

File tree

2 files changed

+32
-34
lines changed
  • Read-and-save-PowerPoint-presentation/Open-and-save-PowerPoint/Google-Drive
    • Open-PowerPoint-document/Open-PowerPoint-document
    • Save-PowerPoint-document/Save-PowerPoint-document

2 files changed

+32
-34
lines changed

Read-and-save-PowerPoint-presentation/Open-and-save-PowerPoint/Google-Drive/Open-PowerPoint-document/Open-PowerPoint-document/Program.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,28 @@ static void Main(string[] args)
2424
new FileDataStore(credPath, true)).Result;
2525
}
2626

27-
// Step 2: Create Drive API service
27+
// Step 2: Create Drive API service.
2828
var service = new DriveService(new BaseClientService.Initializer()
2929
{
3030
HttpClientInitializer = credential,
3131
ApplicationName = ApplicationName,
3232
});
3333

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
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.
3636

37-
// Step 4: Download the PowerPoint presentation from Google Drive
37+
// Step 4: Download the PowerPoint presentation from Google Drive.
3838
var request = service.Files.Get(fileId);
3939
var stream = new MemoryStream();
4040
request.Download(stream);
4141

42-
// Step 5: Save the PowerPoint presentation locally
42+
// Step 5: Save the PowerPoint presentation locally.
4343
using (FileStream fileStream = new FileStream("Output.pptx", FileMode.Create, FileAccess.Write))
4444
{
4545
stream.WriteTo(fileStream);
4646
}
47-
47+
//Dispose the memory stream.
48+
stream.Dispose();
4849
}
4950
}
5051
}

Read-and-save-PowerPoint-presentation/Open-and-save-PowerPoint/Google-Drive/Save-PowerPoint-document/Save-PowerPoint-document/Program.cs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,99 +11,96 @@ internal class Program
1111
{
1212
static void Main(string[] args)
1313
{
14-
//Create a new instance of PowerPoint Presentation file
14+
//Create a new instance of PowerPoint Presentation file.
1515
IPresentation pptxDocument = Presentation.Create();
1616

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

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

28-
//Add description content to the slide by adding a new TextBox
27+
//Add description content to the slide by adding a new TextBox.
2928
IShape descriptionShape = slide.AddTextBox(53.22, 141.73, 874.19, 77.70);
3029
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.";
3130

32-
//Add bullet points to the slide
31+
//Add bullet points to the slide.
3332
IShape bulletPointsShape = slide.AddTextBox(53.22, 270, 437.90, 116.32);
3433

35-
//Add a paragraph for a bullet point
34+
//Add a paragraph for a bullet point.
3635
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
36+
//Format how the bullets should be displayed.
3937
firstPara.ListFormat.Type = ListType.Bulleted;
4038
firstPara.LeftIndent = 35;
4139
firstPara.FirstLineIndent = -35;
4240

43-
//Add another paragraph for the next bullet point
41+
//Add another paragraph for the next bullet point.
4442
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
43+
//Format how the bullets should be displayed.
4744
secondPara.ListFormat.Type = ListType.Bulleted;
4845
secondPara.LeftIndent = 35;
4946
secondPara.FirstLineIndent = -35;
5047

51-
//Gets a picture as stream
52-
FileStream pictureStream = new FileStream(Path.GetFullPath("../../../Output/Image.jpg"), FileMode.Open);
53-
48+
//Gets a picture as stream.
49+
FileStream pictureStream = new FileStream(Path.GetFullPath("../../../Data/Image.jpg"), FileMode.Open);
5450
//Adds the picture to a slide by specifying its size and position.
5551
slide.Shapes.AddPicture(pictureStream, 499.79, 238.59, 364.54, 192.16);
5652

57-
//Add an auto-shape to the slide
53+
//Add an auto-shape to the slide.
5854
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
55+
//Format the auto-shape color by setting the fill type and text.
6156
stampShape.Fill.FillType = FillType.None;
6257
stampShape.TextBody.AddParagraph("IMN").HorizontalAlignment = HorizontalAlignmentType.Center;
6358

64-
//Saves the PowerPoint to MemoryStream
59+
//Saves the PowerPoint to MemoryStream.
6560
MemoryStream stream = new MemoryStream();
6661
pptxDocument.Save(stream);
6762

68-
// Load Google Drive API credentials from a file
63+
// Load Google Drive API credentials from a file.
6964
UserCredential credential;
7065
string[] Scopes = { DriveService.Scope.Drive };
7166
string ApplicationName = "YourAppName";
7267

7368
using (var stream1 = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))//Replace with your actual credentials.json
7469
{
7570
string credPath = "token.json";
76-
// Authorize the Google Drive API access
71+
// Authorize the Google Drive API access.
7772
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
7873
GoogleClientSecrets.Load(stream1).Secrets,
7974
Scopes,
8075
"user",
8176
CancellationToken.None,
8277
new FileDataStore(credPath, true)).Result;
8378
}
84-
// Create a new instance of Google Drive service
79+
// Create a new instance of Google Drive service.
8580
var service = new DriveService(new BaseClientService.Initializer()
8681
{
8782
HttpClientInitializer = credential,
8883
ApplicationName = ApplicationName,
8984
});
9085

91-
// Create metadata for the file to be uploaded
86+
// Create metadata for the file to be uploaded.
9287
var fileMetadata = new File()
9388
{
94-
Name = "Output.pptx", // Name of the file in Google Drive
89+
Name = "Output.pptx", // Name of the file in Google Drive.
9590
MimeType = "application/powerpoint",
9691
};
9792
FilesResource.CreateMediaUpload request;
98-
9993
// Create a memory stream from the PowerPoint presentation.
10094
using (var fs = new MemoryStream(stream.ToArray()))
10195
{
102-
// Create an upload request for Google Drive
96+
// Create an upload request for Google Drive.
10397
request = service.Files.Create(fileMetadata, fs, "application/powerpoint");
104-
// Upload the file
98+
// Upload the file.
10599
request.Upload();
106100
}
101+
//Dispose the memory stream.
102+
stream.Dispose();
103+
pptxDocument.Close();
107104
}
108105
}
109106
}

0 commit comments

Comments
 (0)