Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

851874_code_sample: Add code sample for save document in google drive and dropbox #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Save PDF files/To Dropbox cloud file storage/HelloWorldSample.sln
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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);
}

25 changes: 25 additions & 0 deletions Save PDF files/To Google Drive/HelloWorld.sln
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions Save PDF files/To Google Drive/HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
@@ -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>
58 changes: 58 additions & 0 deletions Save PDF files/To Google Drive/HelloWorld/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
Loading