Add or update Microsoft Word text formatting with Aspose.Words REST API

Hi,


We are successfully using Aspose.Cloud to edit the text of an existing bookmark in a Word-document through the REST API. Essentially we have a Word template with lots of bookmarks and our application fills those bookmarks with relevant text.

Now we would like to extend this by adding formatting to the text, i.e. bold text, italics, bulleted list etc. We have tried to use HTML-tags to accomplish this, but it doesn’t work. What is the correct way to achieve this?

-Tuukka
Hi Tuukka,

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output document showing the undesired behavior
  • Your expected document which shows the correct output. Please create this document using Microsoft Word application.
  • Piece of source code without compilation errors that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we'll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click 'Reply' button that will bring you to the 'reply page' and there at the bottom you can include any attachments with that post by clicking the 'Add/Update' button.

Best regards,

I am trying to do the same thing.

Tuukka, were you able to format the styling properly after inserting your data?

@Clayton_Collie

Thank you for contacting Aspose Support.

POST /words/{name}/{paragraphPath}/runs/{index}/font API let you format the text. The API accepts Font object (in JSON or XML format) in the request body. Following are some of the attributes of the Font object:

  1. AllCaps
    Type: bool
    Description: True if the font is formatted as all capital letters.

  2. Bold
    Type: bool
    Description: True if the font is formatted as bold.

  3. Color
    Type: Color
    Description: Gets or sets the colour of the font.

  4. Italic
    Type: bool
    Description: True if the font is formatted as italic.

  5. Name
    Type: string
    Description: Gets or sets the name of the font.

Please check this article for the complete list of Font object attributes. I have written following cURL example to help you understand how to call this API:

// First get Access Token
// Get App Key and App SID from https://dashboard.aspose.cloud/
curl -v "https://api.aspose.cloud/oauth2/token" \
-X POST \
-d 'grant_type=client_credentials&client_id=0B17F60A-6D69-426B-9ABD-79F35A6E9F7B&client_secret=53b8b19adffa41a3e87dbbd8858977ae' \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

// cURL example to update Font Properties
curl -v "https://api.aspose.cloud/v1.1/words/SampleWordDocument.docx/sections/0/paragraphs/2/runs/0/font?destFileName=ResultWordDocument.docx" \
-X POST \
-d '{"Bold":true, "Size": 31.0, "Name": "Calibri"}' \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer csXaRwlL_WKJHrwmd81vyehkHCU3cT-25nYKGsOMoAnjKzfNcKPXaa4NEdDH7F19-KAGjhUAMAPMUgCA-z5SQdIwC4vlQ-MwOH1NCNJfMMoVB_LfrBpkhwpPzrjAlsJ9007jWI5NrcK_GH-zh6GX-qubydr-ucc6LJm_pHotK9iDrvMSeRDf0SAXUrdMXO8uZkn9WWRTpdrr0F7baet2vGveiTfflEn_jjCXtBg0TDUEdPp3Q1F1jA_8vvBp9XZRJAQKpR3OzHx4Qg9N_3MuZVGVza6r6blfshdTG5kKO8l4MMv-ucg2a7uGPBVBiNWSx9VdgicUF8AyPaxnFiaLNKJQHsWHxU56U1VdE5VHsg9W9nbkwkLIMDfkmye6oKNXiqE11hwK4P9Q1fIFUQyRGHuegyu8o7P4Gy8uXhq7HuJ70hblPAXNnzxRyQrMV-HMs6Z1cAwSQhDkDAtv4cCB1bzZ0yA"

Input Document: SampleWordDocument.docx.zip (50.5 KB)
Output Document: ResultWordDocument.docx.zip (16.8 KB)

Please call GET /words/{name}/{nodePath}/paragraphs API to get list of paragraphs that are contained in the document.

We hope the above is useful to you.

@Clayton_Collie, @Tuukka_Haapaniemi

Please note we have changed PUT to POST (and vice versa) in updated API version 4.0. In this API version, all idempotent methods are PUT and non-idempotent ones are POST. So UpdateRunFont API method is changed to PUT in the new release. Please find the updated cURL code for reference.

# Get JWT Access Token
curl -X POST "https://api.aspose.cloud/connect/token" 
-d "grant_type=client_credentials&client_id=xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx&client_secret=xxxxxxxxxxxxxxxxxxxx" 
-H "Content-Type: application/x-www-form-urlencoded" 
-H "Accept: application/json"

# cURL example to update Font Properties
curl -v "https://api.aspose.cloud/v4.0/words/SampleWordDocument.docx/sections/0/paragraphs/2/runs/0/font?destFileName=ResultWordDocument.docx" \
-X PUT \
-d "{'Bold':true, 'Size': 31.0, 'Name': 'Calibri'}" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer <jwt token>"