`

Java 添加、删除、替换、格式化Word中的文本(基于Spire.Cloud.SDK for Java)

阅读更多

Spire.Cloud.SDK for Java提供了TextRangesApi接口可通过addTextRange()添加文本、deleteTextRange()删除文本、updateTextRangeText()替换文本、updateTextRangeFormat()格式化文本等。本文将从以上方法介绍如何来实现对文本的操作。可参考以下步骤进行准备:

一、导入jar文件

 

创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在pom.xml文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
           <name>cloud</name>
        <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>

<dependencies>
        <dependency>
            <groupId> cloud </groupId>
            <artifactId>spire.cloud.sdk</artifactId>
            <version>3.5.0</version>
        </dependency>

        <dependency>
        <groupId> com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.1</version>
        </dependency>

        <dependency>
            <groupId> com.squareup.okhttp</groupId>
            <artifactId>logging-interceptor</artifactId>
            <version>2.7.5</version>
        </dependency>

        <dependency>
            <groupId> com.squareup.okhttp </groupId>
            <artifactId>okhttp</artifactId>
            <version>2.7.5</version>
        </dependency>

        <dependency>
            <groupId> com.squareup.okio </groupId>
            <artifactId>okio</artifactId>
            <version>1.6.0</version>
        </dependency>

        <dependency>
            <groupId> io.gsonfire</groupId>
            <artifactId>gson-fire</artifactId>
            <version>1.8.0</version>
        </dependency>

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.18</version>
        </dependency>

        <dependency>
            <groupId> org.threeten </groupId>
            <artifactId>threetenbp</artifactId>
            <version>1.3.5</version>
        </dependency>
</dependencies>

 完成配置后,点击“Import Changes” 即可导入所有需要的jar文件。如果使用的是Eclipse,可参考这里的导入方法。

 

二、登录冰蓝云账号,在“文档管理”页面创建文件夹,这里我创建了input文件夹用于存放测试的源文档和output文件夹用于存放结果文档。

 

三、在“我的应用”页面,创建应用程序,获取程序所需要的App ID及App Key

 

完成以上步骤后,可参考以下代码,进行文档操作。

 

1. 添加文本到Word

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi;

public class AddTextRange {
    //配置App账号信息
    static String appId = "App ID";
    static String appKey = "App Key";
    static String baseUrl = "https://api.e-iceblue.cn";
    static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
    static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration);

    public static void main(String[] args) throws ApiException {
        String name = "testfile.docx";//用于测试的Word源文档
        String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取文档中的段落
        Integer  indexInParagraph = 0;
        String text = "新添加的文本内容!";//指定需要添加的文本内容
        String folder = "input";//源文档所在的云端文件夹
        String storage = null;//冰蓝云存储空间
        String password = null;//源文档密码
        String destFilePath = "output/AddTextRange.docx";//结果文档路径

        //调用方法添加文本内容到Word段落
        textRangesApi.addTextRange(name, paragraphPath, text, destFilePath, folder, storage, indexInParagraph, password);
    }
}

2. 删除Word中的文本

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi;

public class DeleteTextRange {
    //配置App账号信息
    static String appId = "App ID";
    static String appKey = "App Key";
    static String baseUrl = "https://api.e-iceblue.cn";
    static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
    static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration);

    public static void main(String[] args) throws ApiException {
        String name = "testfile.docx";//源文档
        String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落
        Integer  index = 0;
        String folder = "input";//源文档所在文件夹
        String storage = null;//冰蓝云存储空间
        String password = null;//源文档密码
        String destFilePath = "output/DeleteTextRange.docx";//结果文档路径

        //调用方法删除Word第一段文本
        textRangesApi.deleteTextRange(name, paragraphPath, index, destFilePath,folder, storage, password);
    }
}

 

3. 替换Word中的文本

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi;

public class UpdateTextRange {
    //配置App账号信息
    static String appId = "App ID";
    static String appKey = "App Key";
    static String baseUrl = "https://api.e-iceblue.cn";
    static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
    static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration);

    public static void main(String[] args) throws ApiException {
        String name = "testfile.docx";//源文档
        String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落
        Integer  index = 0;
        String text = "新替换文本";//指定新文本
        String folder = "input";//源文档所在文件夹
        String storage = null;
        String password = null;
        String destFilePath = "output/UpdateTextRangeText.docx";//结果文档路径

        //调用方法更新(替换)原有的文本
        textRangesApi.updateTextRangeText(name, paragraphPath, index, text, destFilePath, folder, storage, password);
    }
}

 

4. 格式化Word中的文本

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi;
import spire.cloud.word.sdk.client.model.Color;
import spire.cloud.word.sdk.client.model.Font;
import spire.cloud.word.sdk.client.model.TextRangeFormat;

public class UpdateTextRangeFormat {
    //配置App账号信息
    static String appId = "App ID";
    static String appKey = "App Key";
    static String baseUrl = "https://api.e-iceblue.cn";
    static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
    static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration);

    public static void main(String[] args) throws ApiException {
        String name = "testfile.docx";//源文档
        String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落
        Integer  index = 0;

        //创建文本样式,指定字体、颜色、字号,并应用到文本
        TextRangeFormat format = new TextRangeFormat();
        Color color = new Color(34,139,34);
        Font font = new Font("宋体", 20f, color);
        format.setFont(font);
        TextRangeFormat textRange = format;

        String folder = "input";//源文档所在文件夹
        String storage = null;
        String password = null;
        String destFilePath = "output/UpdateTextRangeFormat.docx";//结果文档路径

        //调用方法更新(应用)文本样式
        textRangesApi.updateTextRangeFormat(name, paragraphPath, index, textRange, destFilePath, folder, storage, password);
    }
}

 

生成的文档可在output文件夹下查看。

 

0
0
分享到:
评论

相关推荐

    Java基于Spire.Cloud.SDK for Java基础上添加删除格式化Word中的图片.docx

    在Java开发中,有时我们需要对Word文档进行编辑,例如添加、删除或格式化其中的图片。Spire.Cloud.SDK for Java提供了一个强大的工具集,使得这些操作变得简单易行。本篇将详细介绍如何在Java项目中利用这个SDK进行...

    Spire.Cloud.Excel.SDK_Java及WebAPI示例.zip

    Spire.Cloud.Excel.SDK_Java及WebAPI示例.zip是一个压缩包,主要针对Java开发者,提供了与Excel处理相关的SDK(Software Development Kit)和Web API的示例代码。这个资源旨在帮助开发人员实现对Excel文档的创建、...

    Spire.Cloud.Word_Python_源码及WebAPI示例.zip

    通过学习这些源码,你可以了解到如何上传文档到云端,执行诸如添加文本、插入图片、格式化段落等操作,以及如何下载修改后的文档。 **WebAPI示例**是理解Spire.Cloud.Word功能的关键。Web API允许开发者通过HTTP...

    Spire.Cloud.Word.SDK及WebAPI示例.zip

    Spire.Cloud.Word.SDK和Web API是用于在云端处理Microsoft Word文档的强大工具,主要面向.NET开发者。这个压缩包“Spire.Cloud.Word.SDK及WebAPI示例.zip”提供了详细的示例代码和指南,帮助开发者理解如何利用这些...

    Java 添加、删除、替换、格式化Word中的文本的步骤详解(基于Spire.Cloud.SDK for Java)

    本文将详细介绍如何使用Spire.Cloud.SDK for Java库来实现Word文档中文本的添加、删除、替换和格式化操作。Spire.Cloud.SDK for Java提供了一个强大的TextRangesApi接口,使得这些操作变得简单易行。 首先,我们...

    Java Spire.Cloud.Word 在Word 文档中添加、删除形状.docx

    【Java Spire.Cloud.Word 在 Word 文档中添加、删除形状】 在 Java 开发中,处理 Word 文档的任务可能涉及到各种复杂操作,例如插入图形、文本框等形状。Spire.Cloud 提供了一款在线编辑器,它允许开发人员通过 Web...

    Spire.Cloud.PowerPoint.SDK_Java及WebAPI示例.zip

    **Spire.Cloud.PowerPoint.SDK_Java及WebAPI示例** 是一个专为Java开发者设计的工具包,它提供了丰富的功能,使用户能够通过编程方式创建、编辑、保存和打印PowerPoint(PPT)文档。这个资源包不仅包含了SDK本身,还...

    Spire.Cloud.Powerpoint.SDK及WebAPI示例.zip

    - **SDK介绍**:Spire.Powerpoint SDK是一个用于处理Microsoft PowerPoint文件的强大库,它允许程序员在C#环境中实现对PPT文件的各种操作,如创建新幻灯片、插入图片、添加文本、应用模板、转换格式等。 - **功能...

Global site tag (gtag.js) - Google Analytics