Spire.Cloud.Word provides the ParagraphsApi interface which is used for handling paragraphs in Word documents. This article introduces how to add, remove or edit a paragraph in an existing Word document.
Example 1. Add a paragraph
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using System;
namespace CloudAddParagraph
{
class Program
{
static string appId = "App ID";
static string appKey = "App Key";
static string baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on App ID, App Key and base URL
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create a ParagraphsApi object
static ParagraphsApi paragraphsApi = new ParagraphsApi(configuration);
static void Main(string[] args)
{
//Specify the source document name
string fileName = "sample.docx";
//Specify the section and body to insert paragraph
string nodePath = "Section/0/Body/0";
//Specify the path and name of the generated document
string destFilePath = "output/AddParagraph.docx";
//Specify the folder where the source document is stored
string folder = "input";
//Use the default storage, set to null
string storage = null;
//Specify the index where the new paragraph will be inserted
int indexOfParagraph = 2;
//Specify the open password of the source document, set to null if there’s no password
string password = null;
//Specify the text of the new paragraph
string text = "This is a new paragraph.";
//Call AddParagraph method to insert a new paragraph
paragraphsApi.AddParagraph(fileName, nodePath, destFilePath, folder, storage, indexOfParagraph, password, text);
}
}
}
Example 2. Remove a paragraph
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using System;
namespace CloudDeleteParagraph
{
class Program
{
static string appId = "App ID";
static string appKey = "App Key";
static string baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on App ID, App Key and base URL
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create a ParagraphsApi object
static ParagraphsApi paragraphsApi = new ParagraphsApi(configuration);
static void Main(string[] args)
{
//Specify the source document name
string fileName = "sample.docx";
//Specify the section and body
string nodePath = "Section/0/Body/0";
//Specify the index of the paragraph that'll be deleted
int? index = 0;
//Specify the path and name of the generated document
string destFilePath = "output/DeleteParagraph.docx";
//Specify the folder where the source document is stored
string folder = "input";
//Use the default storage, set to null
string storage = null;
//Specify the open password of the source document, set to null if there’s no password
string password = null;
//Call DeleteParagraph method to delete the specific paragraph
paragraphsApi.DeleteParagraph(fileName, nodePath, index, destFilePath, folder, storage, password);
}
}
}
Example 3. Edit a pragraph
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using System;
namespace CloudEditParagraph
{
class Program
{
static string appId = "App ID";
static string appKey = "App Key";
static string baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on App ID, App Key and base URL
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create a ParagraphsApi object
static ParagraphsApi paragraphsApi = new ParagraphsApi(configuration);
static void Main(string[] args)
{
//Specify the source document name
string fileName = "sample.docx";
//Specify the section and body
string nodePath = "Section/0/Body/0";
//Specify the index of the paragraph that'll be updated
int? index = 0;
//Specify new text
string text = "This is the updated paragraph";
//Specify the path and name of the generated document
string destFilePath = "output/EditParagraph.docx";
//Specify the folder where the source document is stored
string folder = "input";
//Use the default storage, set to null
string storage = null;
//Specify the open password of the source document, set to null if there's no password
string password = null;
//Call UpdateParagraphText method to replace text of an existing paragraph
paragraphsApi.UpdateParagraphText(fileName, nodePath, index, text, destFilePath, folder, storage, password);
}
}
}