diff --git a/Document-Processing/Word/Word-Library/NET/Create-Word-document-in-Blazor.md b/Document-Processing/Word/Word-Library/NET/Create-Word-document-in-Blazor.md index 101e2d396..e1b9630f7 100644 --- a/Document-Processing/Word/Word-Library/NET/Create-Word-document-in-Blazor.md +++ b/Document-Processing/Word/Word-Library/NET/Create-Word-document-in-Blazor.md @@ -1,20 +1,21 @@ --- +layout: post title: Create Word document in Blazor | DocIO | Syncfusion -description: Create Word document without Microsoft Word or interop dependencies in Blazor application using Syncfusion® .NET Word (DocIO) library. +description: Create Word documents without Microsoft Word or interop dependencies in Blazor applications using Syncfusion® .NET Word (DocIO) library. platform: document-processing control: DocIO documentation: UG --- -# Create Word document in Blazor +# Create Word Document in Blazor Syncfusion® Essential® DocIO is a [.NET Core Word library](https://www.syncfusion.com/document-processing/word-framework/net-core/word-library) used to create, read, and edit **Word** documents programmatically without **Microsoft Word** or interop dependencies. Using this library, you can **create a Word document in Blazor**. -To quickly get started with creating a Word document in Blazor, check this video: +To quickly get started with creating a Word document in Blazor, watch this video: {% youtube "https://www.youtube.com/watch?v=yVfDlpewbpU" %} -## Server app +## Server Application {% tabcontents %} @@ -22,20 +23,23 @@ To quickly get started with creating a Word document in Blazor, check this video **Prerequisites:** -* Visual Studio 2022. -* Install [.NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) or later. +* Visual Studio 2022. +* Install [.NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) or later. -Step 1: Create a new C# Blazor Server app project. Select Blazor Server App from the template and click the Next button. +Step 1: Create a new C# Blazor Server app project. +Select "Blazor Server App" from the template and click **Next**. ![Create Blazor Server application in Visual Studio](Blazor_Images/Blazor_Create.png) -Step 2: To **create a Word document in Blazor Server app**, install [Syncfusion.DocIO.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core) to the Blazor project. +Step 2: Install the `Syncfusion.DocIO.Net.Core` NuGet package. +To **create a Word document in a Blazor Server app**, install [Syncfusion.DocIO.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core) into your Blazor project. ![Install DocIO.NET Core NuGet Package](Blazor_Images/Install_Nuget.png) -N> Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add "Syncfusion.Licensing" assembly reference and include a license key in your projects. Please refer to this [link](https://help.syncfusion.com/common/essential-studio/licensing/overview) to know about registering Syncfusion® license key in your application to use our components. +N> Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you must also add the "Syncfusion.Licensing" assembly reference and include a license key in your projects. Refer to this [link](https://help.syncfusion.com/common/essential-studio/licensing/overview) for details on registering Syncfusion® license key in your application to use our components. -Step 3: Create a razor file with name as **DocIO** under **Pages** folder and include the following namespaces in the file. +Step 3: Create a Razor file named `DocIO` in the `Pages` folder. +Include the following namespaces in the file: {% tabs %} {% highlight c# tabtitle="C#" %} @@ -47,24 +51,26 @@ Step 3: Create a razor file with name as **DocIO** under **Pages** folder and in {% endhighlight %} {% endtabs %} -Step 4: Add the following code in **DocIO.razor** file to create a new button. +Step 4: Add a button to `DocIO.razor`. +Include the following code to create a new button that triggers document creation: {% tabs %} {% highlight CSHTML %} -

Syncfusion DocIO library (Essential DocIO)

-

Syncfusion DocIO library (Essential DocIO) is a Blazor DocIO library used to create, read, edit, and convert Word files in your applications without Microsoft Office dependencies.

+

Syncfusion DocIO Library (Essential DocIO)

+

The Syncfusion DocIO library (Essential DocIO) is a Blazor DocIO library used to create, read, edit, and convert Word files in your applications without Microsoft Office dependencies.

{% endhighlight %} {% endtabs %} -Step 5: Add the following code in **DocIO.razor** file to create and download the **Word document**. +Step 5: Implement `CreateWord` method in `DocIO.razor`. +Add the following code to create and download the Word document: {% tabs %} {% highlight c# tabtitle="C#" %} @code { MemoryStream documentStream; /// - /// Create and download the Word document + /// Creates and downloads the Word document. /// protected async void CreateWord() { @@ -75,7 +81,8 @@ Step 5: Add the following code in **DocIO.razor** file to create and download th {% endhighlight %} {% endtabs %} -Step 6: Create a new cs file with name as **WordService** under Data folder and include the following namespaces in the file. +Step 6: Create `WordService.cs` in the `Data` folder. +Include the following namespaces in the file: {% tabs %} @@ -87,7 +94,8 @@ using System.IO; {% endtabs %} -Step 7: Create a new MemoryStream method with name as **CreateWord** in **WordService** class and include the following code snippet to **create a simple Word document in Blazor** Server app. +Step 7: Implement the `CreateWord` method in `WordService.cs`. +Create a new `MemoryStream` method named `CreateWord` in the `WordService` class, and include the following code snippet to **create a simple Word document in Blazor** Server app: {% tabs %} @@ -95,16 +103,16 @@ Step 7: Create a new MemoryStream method with name as **CreateWord** in **WordSe public MemoryStream CreateWord() { - //Creating a new document + // Creating a new Word document WordDocument document = new WordDocument(); - //Adding a new section to the document + // Adding a new section to the document WSection section = document.AddSection() as WSection; - //Set Margin of the section + // Set Margin of the section section.PageSetup.Margins.All = 72; - //Set page size of the section + // Set page size of the section section.PageSetup.PageSize = new Syncfusion.Drawing.SizeF(612, 792); - //Create Paragraph styles + // Create Paragraph styles WParagraphStyle style = document.AddParagraphStyle("Normal") as WParagraphStyle; style.CharacterFormat.FontName = "Calibri"; style.CharacterFormat.FontSize = 11f; @@ -131,7 +139,7 @@ public MemoryStream CreateWord() textRange.CharacterFormat.FontName = "Calibri"; textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Red; - //Appends paragraph + // Appends paragraph paragraph = section.AddParagraph(); paragraph.ApplyStyle("Heading 1"); paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; @@ -139,24 +147,24 @@ public MemoryStream CreateWord() textRange.CharacterFormat.FontSize = 18f; textRange.CharacterFormat.FontName = "Calibri"; - //Appends paragraph + // Appends paragraph paragraph = section.AddParagraph(); paragraph.ParagraphFormat.FirstLineIndent = 36; paragraph.BreakCharacterFormat.FontSize = 12f; textRange = paragraph.AppendText("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.") as WTextRange; textRange.CharacterFormat.FontSize = 12f; - //Appends paragraph + // Appends paragraph paragraph = section.AddParagraph(); paragraph.ParagraphFormat.FirstLineIndent = 36; paragraph.BreakCharacterFormat.FontSize = 12f; 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 WTextRange; textRange.CharacterFormat.FontSize = 12f; - //Saves the Word document to MemoryStream + // Saves the Word document to MemoryStream MemoryStream stream = new MemoryStream(); document.Save(stream, FormatType.Docx); - //Closes the Word document + // Closes the Word document document.Close(); stream.Position = 0; return stream; @@ -165,7 +173,8 @@ public MemoryStream CreateWord() {% endtabs %} -Step 8: Add the following line to the Program.cs file to register the WordService as a scoped service in your Blazor application. +Step 8: Register `WordService` in `Program.cs`. +Add the following line to the `Program.cs` file to register `WordService` as a scoped service in your Blazor application. {% tabs %} {% highlight c# tabtitle="C#" %} @@ -176,7 +185,8 @@ builder.Services.AddSingleton(); {% endtabs %} -Step 9: Create a new class file in the project, with name as FileUtils and add the following code to invoke the JavaScript action to download the file in the browser. +Step 9: Create `FileUtils.cs` for JavaScript interoperability. +Create a new class file named `FileUtils` in the project and add the following code to invoke the JavaScript action for file download in the browser. {% tabs %} @@ -194,7 +204,8 @@ public static class FileUtils {% endtabs %} -Step 10: Add the following JavaScript function in the _Host.cshtml in the Pages folder. +Step 10: Add JavaScript function to `_Host.cshtml`. +Add the following JavaScript function in the `_Host.cshtml` file located in the `Pages` folder. {% tabs %} @@ -203,7 +214,7 @@ Step 10: Add the following JavaScript function in the _Host.cshtml in the Pages