C# set, delete, read the Word document background-based on Spire.Cloud.SDK for .NET

Spire.Cloud. SDK for .NET provides interfaces SetBackgroudColor(), SetBackgroudImage(), DeleteBackground(), GetBackgroudColor() for setting, deleting and reading the background of Word documents. This article will take the C# program as an example to demonstrate how to call the API interface to achieve the above operations.

Necessary steps:

Step 1: Obtain and import the dll file. Search and install through Nuget in the program and import directly.

dll add reference effect:

Step 2: Obtain App ID and Key Create an account in the cloud and create an application in the “My Apps” section to obtain the App ID and App Key.

Step 3: Upload the source document. In the “Document Management” section, upload the source document. If you want to facilitate document management, you can create a new folder and save the source document and the result document to the corresponding folder. When no folder is created, the source document and the result document are directly saved in the root directory. In the example of this article, two folders are created to store the source document and the result document respectively.

[Example ] Set the background color

using Spire.Cloud.Word;
 using Spire.Cloud.Word.Sdk.Api;
 using Spire.Cloud.Word.Sdk.Client;
 using Spire.Cloud.Word.Sdk.Model;
 using System;


namespace BackgroundColor
{
    class Program
    {
        static String appId = " App ID " ;
         static String appKey = " App Key " ;
         static  void Main( string [] args)
        {
            // Configure account information 
            Configuration wordConfiguration = new Configuration(appId, appKey);

            // Create BackgroundApi instance 
            BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
            
            // Source document 
            var fileName = " testfile.docx " ; 
             string name = fileName;

            //The folder where the source document is located. If there is no folder, set it to null 
            string folder = " input " ;

            // Set the background color RGB value 
            Color color = new Color( 255 , 255 , 205 );

            // Set the document password, if there is no password, set it to null 
            string password = null ;

            // Use the 2G space to store documents configured by Binglan Cloud, which can be set to null 
            string storage = null ;

            // Set the path and document name of the generated document 
            string destFilePath = " output/BackgroundColor.docx " ;

            // Call the method to set the background color 
            backgroundApi.SetBackgroundColor(name,color,destFilePath,folder,storage,password);
        }
    }
}

Background color setting result:

[Example ] Set the background picture

using Spire.Cloud.Word.Sdk;
 using Spire.Cloud.Word.Sdk.Api;
 using Spire.Cloud.Word.Sdk.Client;
 using System;


namespace BackgroundImg
{
    class Program
    {
        static String appId = " App ID " ;
         static String appKey = " App Key " ;
         static  void Main( string [] args)
        {
            // Configure account information 
            Configuration wordConfiguration = new Configuration(appId, appKey);

            // Create BackgroundApi instance 
            BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);

            // Source documents and pictures 
            var fileName = " testfile.docx " ;
             var imageName = " ss.png " ;
             string name = fileName;

            //The folder where the source document is located. If there is no folder, set it to null 
            string folder = " input " ;
             string imagePath = " input " + " / " + imageName;

            // Set the document password, if there is no password, set it to null 
            string password = null ;

            // Use the 2G space to store documents configured by Binglan Cloud, which can be set to null 
            string storage = null ;

            // Set the path and document name of the generated document 
            string destFilePath = " output/BackgroundImg.docx " ;

            // Call the method to set the background 
            backgroundApi.SetBackgroudImage(name,imagePath,folder,storage,password,destFilePath);
        }
    }
}

Background image setting effect:

[Example ] Delete background (including background color and background picture)

using Spire.Cloud.Word.Sdk;
 using Spire.Cloud.Word.Sdk.Api;
 using Spire.Cloud.Word.Sdk.Client;
 using System;

namespace DeleteBackground
{
    class Program
    {
        static String appId = " App ID " ;
         static String appKey = " App Key " ;
         static  void Main( string [] args)
        {
            // Configure account information 
            Configuration wordConfiguration = new Configuration(appId, appKey);

            // Create BackgroundApi instance 
            BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);

            // Source document 
            var fileName = " BackgroundImg.docx " ;
             string name = fileName;

            //The folder where the source document is located. If there is no folder, set it to null 
            string folder = " output " ;

            // Set the document password, if there is no password, set it to null 
            string password = null ;

            // Use the 2G space to store documents configured by Binglan Cloud, which can be set to null 
            string storage = null ;

            // Set the path and document name of the generated document 
            string destFilePath = " output/DeleteBackground.docx " ;

            // Call the method to delete the background in the document 
            backgroundApi.DeleteBackground(name,destFilePath,password,folder,storage);
        }
    }
}

Document background deletion effect:

[Example ] Read the background color

using Spire.Cloud.Word.Sdk.Api;
 using Spire.Cloud.Word.Sdk.Client;
 using Spire.Cloud.Word.Sdk.Model;
 using System;

namespace GetBackground
{
    class Program
    {
        static String appId = " App ID " ;
         static String appKey = " App Key " ;
         static  void Main( string [] args)
        {
            // Configure account information 
            Configuration wordConfiguration = new Configuration(appId, appKey);

            // Create BackgroundApi instance 
            BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);

            // Source document 
            var fileName = " BackgroundColor.docx " ; 
             string name = fileName;

            // Source document password, if there is no password, it can be set to null 
            string password = null ;

            //The folder where the source document is located. If there is no folder, set it to null 
            string folder = " output " ;

            // Use the 2G space to store documents configured by Binglan Cloud, which can be set to null 
            string storage = null ;
            
            // Get the document background color 
            System.Console.WriteLine(backgroundApi.GetBackgroundColor(name,password,folder,storage));
            System.Console.ReadLine();       
        }
    }
}

Reading result of background color RGB value:

(End of this article)

This article is reprinted from: https://www.cnblogs.com/Yesi/p/12034407.html

Leave a Reply