From 3920dc6c390cd9407ef74d805acb8c13ccbe34be Mon Sep 17 00:00:00 2001
From: Srihariharan <srihariharan.g@syncfusion.com>
Date: Mon, 16 Oct 2023 15:20:16 +0530
Subject: [PATCH] 851874_code_sample: add code sample for save document in
 google drive and dropbox.

---
 .../HelloWorldSample.sln                      | 25 ++++++++
 .../HelloWorldSample/HelloWorldSample.csproj  | 15 +++++
 .../HelloWorldSample/Program.cs               | 34 +++++++++++
 Save PDF files/To Google Drive/HelloWorld.sln | 25 ++++++++
 .../HelloWorld/HelloWorld.csproj              | 15 +++++
 .../To Google Drive/HelloWorld/Program.cs     | 58 +++++++++++++++++++
 6 files changed, 172 insertions(+)
 create mode 100644 Save PDF files/To Dropbox cloud file storage/HelloWorldSample.sln
 create mode 100644 Save PDF files/To Dropbox cloud file storage/HelloWorldSample/HelloWorldSample.csproj
 create mode 100644 Save PDF files/To Dropbox cloud file storage/HelloWorldSample/Program.cs
 create mode 100644 Save PDF files/To Google Drive/HelloWorld.sln
 create mode 100644 Save PDF files/To Google Drive/HelloWorld/HelloWorld.csproj
 create mode 100644 Save PDF files/To Google Drive/HelloWorld/Program.cs

diff --git a/Save PDF files/To Dropbox cloud file storage/HelloWorldSample.sln b/Save PDF files/To Dropbox cloud file storage/HelloWorldSample.sln
new file mode 100644
index 00000000..2d7c2df3
--- /dev/null
+++ b/Save PDF files/To Dropbox cloud file storage/HelloWorldSample.sln	
@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.6.33829.357
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorldSample", "HelloWorldSample\HelloWorldSample.csproj", "{385C3CBF-55AC-4E74-80A6-E1711CDD30D4}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{385C3CBF-55AC-4E74-80A6-E1711CDD30D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{385C3CBF-55AC-4E74-80A6-E1711CDD30D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{385C3CBF-55AC-4E74-80A6-E1711CDD30D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{385C3CBF-55AC-4E74-80A6-E1711CDD30D4}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {23BAF787-34A6-4A66-B756-CA8D19FECEAD}
+	EndGlobalSection
+EndGlobal
diff --git a/Save PDF files/To Dropbox cloud file storage/HelloWorldSample/HelloWorldSample.csproj b/Save PDF files/To Dropbox cloud file storage/HelloWorldSample/HelloWorldSample.csproj
new file mode 100644
index 00000000..c55703be
--- /dev/null
+++ b/Save PDF files/To Dropbox cloud file storage/HelloWorldSample/HelloWorldSample.csproj	
@@ -0,0 +1,15 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net7.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Dropbox.Api" Version="6.37.0" />
+    <PackageReference Include="Syncfusion.Pdf.Net.Core" Version="23.1.40" />
+  </ItemGroup>
+
+</Project>
diff --git a/Save PDF files/To Dropbox cloud file storage/HelloWorldSample/Program.cs b/Save PDF files/To Dropbox cloud file storage/HelloWorldSample/Program.cs
new file mode 100644
index 00000000..616c66e0
--- /dev/null
+++ b/Save PDF files/To Dropbox cloud file storage/HelloWorldSample/Program.cs	
@@ -0,0 +1,34 @@
+using Syncfusion.Pdf.Graphics;
+using Syncfusion.Pdf;
+using Dropbox.Api;
+using Dropbox.Api.Files;
+using Syncfusion.Drawing;
+
+
+PdfDocument doc = new PdfDocument();
+PdfPage page = doc.Pages.Add();
+
+PdfGraphics graphics = page.Graphics;
+PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
+
+graphics.DrawString("Hello, World!", font, PdfBrushes.Black, new PointF(10, 10));
+
+// Add more content if needed...
+
+// Save the PDF to a memory stream.
+MemoryStream stream = new MemoryStream();
+doc.Save(stream);
+doc.Close(true);
+
+var accessToken = "YOUR_ACCESS_TOKEN";// Replace with your actual access token
+
+using (var dbx = new DropboxClient(accessToken))
+{
+    var uploadResult = await dbx.Files.UploadAsync(
+        "/path/to/save/yourfile.pdf",
+        WriteMode.Overwrite.Instance,
+        body: new MemoryStream(stream.ToArray()));
+
+    Console.WriteLine("Saved to Dropbox as " + uploadResult.PathDisplay);
+}
+
diff --git a/Save PDF files/To Google Drive/HelloWorld.sln b/Save PDF files/To Google Drive/HelloWorld.sln
new file mode 100644
index 00000000..25800391
--- /dev/null
+++ b/Save PDF files/To Google Drive/HelloWorld.sln	
@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.6.33829.357
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld", "HelloWorld\HelloWorld.csproj", "{40A97E2C-B946-468D-B767-145E8DCC3E17}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{40A97E2C-B946-468D-B767-145E8DCC3E17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{40A97E2C-B946-468D-B767-145E8DCC3E17}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{40A97E2C-B946-468D-B767-145E8DCC3E17}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{40A97E2C-B946-468D-B767-145E8DCC3E17}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {73580920-6987-4F50-8854-86A7EDA86A8D}
+	EndGlobalSection
+EndGlobal
diff --git a/Save PDF files/To Google Drive/HelloWorld/HelloWorld.csproj b/Save PDF files/To Google Drive/HelloWorld/HelloWorld.csproj
new file mode 100644
index 00000000..c0b52fa9
--- /dev/null
+++ b/Save PDF files/To Google Drive/HelloWorld/HelloWorld.csproj	
@@ -0,0 +1,15 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net7.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Google.Apis.Drive.v3" Version="1.62.0.3155" />
+    <PackageReference Include="Syncfusion.Pdf.Net.Core" Version="23.1.40" />
+  </ItemGroup>
+
+</Project>
diff --git a/Save PDF files/To Google Drive/HelloWorld/Program.cs b/Save PDF files/To Google Drive/HelloWorld/Program.cs
new file mode 100644
index 00000000..8a9a999c
--- /dev/null
+++ b/Save PDF files/To Google Drive/HelloWorld/Program.cs	
@@ -0,0 +1,58 @@
+using Syncfusion.Pdf;
+using Syncfusion.Pdf.Graphics;
+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.Drawing;
+
+PdfDocument document = new PdfDocument();
+PdfPage page = document.Pages.Add();
+PdfGraphics graphics = page.Graphics;
+graphics.DrawString("Hello, World!", new PdfStandardFont(PdfFontFamily.Helvetica, 12), PdfBrushes.Black, new PointF(10, 10));
+
+// Step 2: Save the PDF to a MemoryStream
+MemoryStream stream = new MemoryStream();
+document.Save(stream);
+document.Close(true);
+
+// Step 3: Authenticate with Google Drive
+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";
+    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
+        GoogleClientSecrets.Load(stream1).Secrets,
+        Scopes,
+        "user",
+        CancellationToken.None,
+        new FileDataStore(credPath, true)).Result;
+}
+// Step 4: Create Drive API service
+var service = new DriveService(new BaseClientService.Initializer()
+{
+    HttpClientInitializer = credential,
+    ApplicationName = ApplicationName,
+});
+
+// Step 5: Upload the PDF to Google Drive
+
+var fileMetadata = new File()
+{
+    Name = "Sample.pdf", // Name of the file in Google Drive
+    MimeType = "application/pdf",
+};
+FilesResource.CreateMediaUpload request;
+using (var fs = new MemoryStream(stream.ToArray()))
+{
+    request = service.Files.Create(fileMetadata, fs, "application/pdf");
+    request.Upload();
+}
+
+File file = request.ResponseBody;
+
+Console.WriteLine("File ID: " + file.Id);