Spire.Cloud.Word provides developers with the ParagraphsApi interface to manipulate paragraphs in Word documents. This article will introduce how to add, replace and delete paragraphs in Word Document with this interface.
The original Word document is shown as below:
Add a paragraph
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ParagraphsApi;
public class addParagraph {
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 wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create an instance of ParagraphsApi
static ParagraphsApi paragraphsApi = new ParagraphsApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Specify the name of the original Word document
String fileName = "Spire.Office.docx";
String name = fileName;
//Add a paragraph
String nodePath = "Section/0/Body/0";
Integer indexOfParagraph = 1;
String text = "This is a newly added paragraph";
//Specify the folder where the original document is stored, set it to null if there's none
String folder = "input";
//Specify the original password of the input document, set it to null if there's none
String password = null;
//Store the document in the 2G default storage space offered by E-iceblue Cloud
String storage = null;
//Specify the storage path and name of the generated document
String destFilePath = "output/AddParagraph.docx";
//Add a paragraph to the Word document with "addParagraph" method
paragraphsApi.addParagraph(name, nodePath, destFilePath, folder, storage, indexOfParagraph, password, text);
}
}
Replace a paragraph
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ParagraphsApi;
public class updateParagraphText {
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 wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create an instance of ParagraphsApi
static ParagraphsApi paragraphsApi = new ParagraphsApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Specify the name of the original Word document
String fileName = "Spire.Office.docx";
String name = fileName;
//Replace the text in the third paragraph
String nodePath = "Section/0/Body/0";
Integer index = 2;
String text = "A Brief Introduction of Spire.Office";
//Specify the folder where the original document is stored, set it to null if there's none
String folder = "input";
//Specify the original password of the input document, set it to null if there's none
String password = null;
//Store the document in the 2G default storage space offered by E-iceblue Cloud
String storage = null;
//Specify the storage path and name of the generated document
String destFilePath = "output/UpdateParagraphText.docx";
//Replace the paragraph in the Word document with "updateParagraphText" method
paragraphsApi.updateParagraphText(name, nodePath, index, text, destFilePath,folder, storage, password);
}
}
Delete a paragraph
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ParagraphsApi;
public class deleteParagraph {
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 wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create an instance of ParagraphsApi
static ParagraphsApi paragraphsApi = new ParagraphsApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Specify the name of the original Word document
String fileName = "Spire.Office.docx";
String name = fileName;
//Delete the fourth paragraph
String nodePath = "Section/0/Body/0";
Integer index = 3;
//Specify the folder where the original document is stored, set it to null if there's none
String folder = "input";
//Specify the original password of the input document, set it to null if there's none
String password = null;
//Store the document in the 2G default storage space offered by E-iceblue Cloud
String storage = null;
//Specify the storage path and name of the generated document
String destFilePath = "output/DeleteParagraph.docx";
//Delete a paragraph in the Word document with "deleteParagraph" method
paragraphsApi.deleteParagraph(name, nodePath, index, destFilePath, folder, storage, password);
}
}