Skip to content

Commit 7adba4c

Browse files
Merge pull request #193 from SyncfusionExamples/Word-Google
Add samples for opening and saving Word document in Google cloud storage
2 parents abe3a97 + a5f693f commit 7adba4c

File tree

164 files changed

+149999
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+149999
-58
lines changed

Read-and-Save-document/Open-and-save-Word-document/AWS-S3-Bucket/Open-Word-document/Open-Word-document/Controllers/HomeController.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,10 @@ public IActionResult Index()
2121
}
2222
public async Task<IActionResult> EditDocument()
2323
{
24-
//Your AWS Storage Account bucket name
25-
string bucketName = "your-bucket-name";
26-
27-
//Name of the Word file you want to load from AWS S3
28-
string key = "WordTemplate.docx";
29-
3024
try
3125
{
3226
//Retrieve the document from AWS S3
33-
MemoryStream stream = await GetDocumentFromS3(bucketName, key);
27+
MemoryStream stream = await GetDocumentFromS3();
3428

3529
//Set the position to the beginning of the MemoryStream
3630
stream.Position = 0;
@@ -69,11 +63,15 @@ public async Task<IActionResult> EditDocument()
6963
/// <summary>
7064
/// Download file from AWS S3 cloud storage
7165
/// </summary>
72-
/// <param name="bucketName"></param>
73-
/// <param name="key"></param>
7466
/// <returns></returns>
75-
public async Task<MemoryStream> GetDocumentFromS3(string bucketName, string key)
67+
public async Task<MemoryStream> GetDocumentFromS3()
7668
{
69+
//Your AWS Storage Account bucket name
70+
string bucketName = "your-bucket-name";
71+
72+
//Name of the Word file you want to load from AWS S3
73+
string key = "WordTemplate.docx";
74+
7775
//Configure AWS credentials and region
7876
var region = Amazon.RegionEndpoint.USEast1;
7977
var credentials = new Amazon.Runtime.BasicAWSCredentials("your-access-key", "your-secret-key");
@@ -100,7 +98,6 @@ public async Task<MemoryStream> GetDocumentFromS3(string bucketName, string key)
10098
await response.ResponseStream.CopyToAsync(stream);
10199

102100
return stream;
103-
104101
}
105102
}
106103
catch (Exception ex)

Read-and-Save-document/Open-and-save-Word-document/AWS-S3-Bucket/Open-Word-document/Open-Word-document/Open-Word-document.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="AWSSDK.S3" Version="3.7.309.10" />
12-
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="26.1.39" />
11+
<PackageReference Include="AWSSDK.S3" Version="*" />
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
1313
</ItemGroup>
1414

1515
</Project>

Read-and-Save-document/Open-and-save-Word-document/AWS-S3-Bucket/Save-Word-document/Save-Word-document/Controllers/HomeController.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,28 +254,26 @@ public async Task<IActionResult> UploadDocument()
254254

255255
//Saves the Word document to MemoryStream
256256
MemoryStream stream = new MemoryStream();
257-
document.Save(stream, FormatType.Docx);
258-
259-
//Your AWS Storage Account bucket name
260-
string bucketName = "your-bucket-name";
261-
262-
//Name of the Word file you want to upload
263-
string key = "CreateWord.docx";
257+
document.Save(stream, FormatType.Docx);
264258

265259
//Upload the document to AWS S3
266-
await UploadDocumentToS3(bucketName, key, stream);
260+
await UploadDocumentToS3(stream);
267261

268262
return Ok("Word document uploaded to AWS S3 Storage.");
269263
}
270264
/// <summary>
271265
/// Upload file to AWS S3 cloud storage
272266
/// </summary>
273-
/// <param name="bucketName"></param>
274-
/// <param name="key"></param>
275267
/// <param name="stream"></param>
276268
/// <returns></returns>
277-
public async Task<MemoryStream> UploadDocumentToS3(string bucketName, string key, MemoryStream stream)
269+
public async Task<MemoryStream> UploadDocumentToS3(MemoryStream stream)
278270
{
271+
//Your AWS Storage Account bucket name
272+
string bucketName = "your-bucket-name";
273+
274+
//Name of the Word file you want to upload
275+
string key = "CreateWord.docx";
276+
279277
//Configure AWS credentials and region
280278
var region = Amazon.RegionEndpoint.USEast1;
281279
var credentials = new Amazon.Runtime.BasicAWSCredentials("your-access-key", "your-secret-key");

Read-and-Save-document/Open-and-save-Word-document/AWS-S3-Bucket/Save-Word-document/Save-Word-document/Save-Word-document.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="AWSSDK.S3" Version="3.7.309.10" />
12-
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="26.1.39" />
11+
<PackageReference Include="AWSSDK.S3" Version="*" />
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
1313
</ItemGroup>
1414

1515
</Project>

Read-and-Save-document/Open-and-save-Word-document/Azure-Blob-Storage/Open-Word-document/Open-Word-document/Controllers/HomeController.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,10 @@ public IActionResult Index()
2626
}
2727
public async Task<IActionResult> EditDocument()
2828
{
29-
//Your Azure Storage Account connection string
30-
string connectionString = "Your_connection_string";
31-
32-
//Name of the Azure Blob Storage container
33-
string containerName = "Your_container_name";
34-
35-
//Name of the Word file you want to load
36-
string blobName = "WordTemplate.docx";
37-
3829
try
3930
{
4031
//Retrieve the document from Azure
41-
MemoryStream stream = await GetDocumentFromAzure(connectionString, containerName, blobName);
32+
MemoryStream stream = await GetDocumentFromAzure();
4233

4334
//Set the position to the beginning of the MemoryStream
4435
stream.Position = 0;
@@ -67,7 +58,6 @@ public async Task<IActionResult> EditDocument()
6758
fileStreamResult.FileDownloadName = "EditWord.docx";
6859
return fileStreamResult;
6960
}
70-
7161
}
7262
catch (Exception ex)
7363
{
@@ -78,13 +68,20 @@ public async Task<IActionResult> EditDocument()
7868
/// <summary>
7969
/// Download file from Azure Blob cloud storage
8070
/// </summary>
81-
/// <param name="bucketName"></param>
82-
/// <param name="key"></param>
8371
/// <returns></returns>
84-
public async Task<MemoryStream> GetDocumentFromAzure(string connectionString, string containerName, string blobName)
72+
public async Task<MemoryStream> GetDocumentFromAzure()
8573
{
8674
try
8775
{
76+
//Your Azure Storage Account connection string
77+
string connectionString = "Your_connection_string";
78+
79+
//Name of the Azure Blob Storage container
80+
string containerName = "Your_container_name";
81+
82+
//Name of the Word file you want to load
83+
string blobName = "WordTemplate.docx";
84+
8885
//Download the Word document from Azure Blob Storage
8986
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
9087
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

Read-and-Save-document/Open-and-save-Word-document/Azure-Blob-Storage/Open-Word-document/Open-Word-document/Open-Word-document.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
12-
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="26.1.40" />
11+
<PackageReference Include="Azure.Storage.Blobs" Version="*" />
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
1313
</ItemGroup>
1414

1515
</Project>

Read-and-Save-document/Open-and-save-Word-document/Azure-Blob-Storage/Save-Word-document/Save-Word-document/Controllers/HomeController.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -252,32 +252,31 @@ public async Task<IActionResult> UploadDocument()
252252

253253
//Saves the Word document to MemoryStream
254254
MemoryStream stream = new MemoryStream();
255-
document.Save(stream, FormatType.Docx);
256-
257-
//Your Azure Storage Account connection string
258-
string connectionString = "Your_connection_string";
259-
260-
//Name of the Azure Blob Storage container
261-
string containerName = "Your_container_name";
262-
263-
//Name of the Word file you want to load
264-
string blobName = "CreateWord.docx";
255+
document.Save(stream, FormatType.Docx);
265256

266257
//Upload the document to azure
267-
await UploadDocumentToAzure(connectionString, containerName, blobName, stream);
258+
await UploadDocumentToAzure(stream);
268259

269260
return Ok("Word document uploaded to Azure Blob Storage.");
270261
}
271262
/// <summary>
272263
/// Upload file to Azure Blob cloud storage
273264
/// </summary>
274-
/// <param name="bucketName"></param>
275-
/// <param name="key"></param>
265+
/// <param name="stream"></param>
276266
/// <returns></returns>
277-
public async Task<MemoryStream> UploadDocumentToAzure(string connectionString, string containerName, string blobName, MemoryStream stream)
267+
public async Task<MemoryStream> UploadDocumentToAzure(MemoryStream stream)
278268
{
279269
try
280270
{
271+
//Your Azure Storage Account connection string
272+
string connectionString = "Your_connection_string";
273+
274+
//Name of the Azure Blob Storage container
275+
string containerName = "Your_container_name";
276+
277+
//Name of the Word file you want to load
278+
string blobName = "CreateWord.docx";
279+
281280
//Download the Word document from Azure Blob Storage
282281
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
283282
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

Read-and-Save-document/Open-and-save-Word-document/Azure-Blob-Storage/Save-Word-document/Save-Word-document/Save-Word-document.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
12-
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="26.1.40" />
11+
<PackageReference Include="Azure.Storage.Blobs" Version="*" />
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
1313
</ItemGroup>
1414

1515
</Project>
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.10.34928.147
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Open-Word-document", "Open-Word-document\Open-Word-document.csproj", "{E732C351-53B8-4986-971E-50D074486ACB}"
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+
{E732C351-53B8-4986-971E-50D074486ACB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E732C351-53B8-4986-971E-50D074486ACB}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E732C351-53B8-4986-971E-50D074486ACB}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E732C351-53B8-4986-971E-50D074486ACB}.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 = {518AB72D-48F2-48F0-998F-73D48821A204}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using Google.Apis.Auth.OAuth2;
2+
using Google.Cloud.Storage.V1;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Open_Word_document.Models;
5+
using Syncfusion.DocIO.DLS;
6+
using System.Diagnostics;
7+
8+
namespace Open_Word_document.Controllers
9+
{
10+
public class HomeController : Controller
11+
{
12+
private readonly ILogger<HomeController> _logger;
13+
14+
public HomeController(ILogger<HomeController> logger)
15+
{
16+
_logger = logger;
17+
}
18+
19+
public IActionResult Index()
20+
{
21+
return View();
22+
}
23+
public async Task<IActionResult> EditDocument()
24+
{
25+
//Download the file from Google
26+
MemoryStream memoryStream = await GetDocumentFromGoogle();
27+
28+
//Create an instance of WordDocument
29+
using (WordDocument wordDocument = new WordDocument(memoryStream, Syncfusion.DocIO.FormatType.Docx))
30+
{
31+
//Access the section in a Word document
32+
IWSection section = wordDocument.Sections[0];
33+
34+
//Add new paragraph to the section
35+
IWParagraph paragraph = section.AddParagraph();
36+
paragraph.ParagraphFormat.FirstLineIndent = 36;
37+
paragraph.BreakCharacterFormat.FontSize = 12f;
38+
39+
//Add new text to the paragraph
40+
IWTextRange textRange = paragraph.AppendText("In 2000, AdventureWorks Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the AdventureWorks Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.") as IWTextRange;
41+
textRange.CharacterFormat.FontSize = 12f;
42+
43+
//Saving the Word document to a MemoryStream
44+
MemoryStream outputStream = new MemoryStream();
45+
wordDocument.Save(outputStream, Syncfusion.DocIO.FormatType.Docx);
46+
47+
//Download the Word file in the browser
48+
FileStreamResult fileStreamResult = new FileStreamResult(outputStream, "application/msword");
49+
fileStreamResult.FileDownloadName = "EditWord.docx";
50+
return fileStreamResult;
51+
}
52+
}
53+
/// <summary>
54+
/// Download file from Google
55+
/// </summary>
56+
/// <returns></returns>
57+
public async Task<MemoryStream> GetDocumentFromGoogle()
58+
{
59+
try
60+
{
61+
//Your bucket name
62+
string bucketName = "Your_bucket_name";
63+
64+
//Your service account key file path
65+
string keyPath = "credentials.json";
66+
67+
//Name of the file to download from the Google Cloud Storage
68+
string fileName = "WordTemplate.docx";
69+
70+
//Create Google Credential from the service account key file
71+
GoogleCredential credential = GoogleCredential.FromFile(keyPath);
72+
73+
//Instantiates a storage client to interact with Google Cloud Storage
74+
StorageClient storageClient = StorageClient.Create(credential);
75+
76+
//Download a file from Google Cloud Storage
77+
MemoryStream memoryStream = new MemoryStream();
78+
await storageClient.DownloadObjectAsync(bucketName, fileName, memoryStream);
79+
memoryStream.Position = 0;
80+
81+
return memoryStream;
82+
}
83+
catch (Exception ex)
84+
{
85+
Console.WriteLine($"Error retrieving document from Google Cloud Storage: {ex.Message}");
86+
throw; // or handle the exception as needed
87+
}
88+
}
89+
public IActionResult Privacy()
90+
{
91+
return View();
92+
}
93+
94+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
95+
public IActionResult Error()
96+
{
97+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)