|
| 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 | +} |
0 commit comments