• Java 操作Word书签(三):用文本、图片、表格替换书签


    本篇文章将继续介绍通过Java来操作Word书签的方法,即替换Word中已有书签,包括用新的文本、图片、表格等替换原有书签处的内容。

    使用工具:Free Spire.Doc for Java (免费版)

    Jar文件获取及导入:

    方法1通过官网下载jar文件包。下载后,解压文件。并将lib文件夹下的Spire.Doc.jar文件导入到java程序。参考如下导入效果:

    方法2可通过maven仓库安装导入。可参考安装导入方法

    Java代码示例

     【示例1】用文本替换书签

    import com.spire.doc.*;
    import com.spire.doc.documents.BookmarksNavigator;
    
    public class ReplaceBookmarkContentWithNewContent {
        public static void main(String[]args){
            //加载包含书签的Word文档
            Document doc = new Document();
            doc.loadFromFile("test.docx");
    
            //定位到指定书签位置
            BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
            bookmarksNavigator.moveToBookmark("bookmark1");
    
            //用文本内容替换原有书签位置的文本,新替换的内容与原文格式一致
            bookmarksNavigator.replaceBookmarkContent("新文本内容!",true);
    
    
            //保存文档
            doc.saveToFile("replaceWithNewContent.docx",FileFormat.Docx_2013);
            doc.dispose();
        }
    }
    

      替换效果:

    【示例2】用图片替换书签

    import com.spire.doc.*;
    import com.spire.doc.documents.BookmarksNavigator;
    import com.spire.doc.documents.Paragraph;
    import com.spire.doc.documents.TextBodyPart;
    
    public class ReplaceBookmarkWithImg {
        public static void main(String[]args){
            //加载包含书签的文档
            Document doc = new Document();
            doc.loadFromFile("test.docx");
    
            //定位到指定书签位置
            BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
            bookmarksNavigator.moveToBookmark("bookmark1");
    
            //添加图片,替换原有书签内容
            Paragraph para= new Paragraph(doc);
            para.appendPicture("eth.png");
            TextBodyPart bodyPart = new TextBodyPart(doc);
            bodyPart.getBodyItems().add(para);
            bookmarksNavigator.replaceBookmarkContent(bodyPart);
    
            //保存文档
            doc.saveToFile("replaceWithImg.docx",FileFormat.Docx_2013);
            doc.dispose();
        }
    }
    

      替换效果:

    【示例3】用表格替换书签

    import com.spire.doc.*;
    import com.spire.doc.documents.*;
    import com.spire.doc.fields.TextRange;
    
    public class ReplaceBookmarkContentWithTable {
        public static void main(String[]args){
            //加载包含书签的Word文档
            Document doc = new Document();
            doc.loadFromFile("test.docx");
    
            //声明数组内容
            String[][] data =
                    {
                            new String[]{"分类", "等级", "编号"},
                            new String[]{"A", "一级", "01A"},
                            new String[]{"B", "二级", "02B"},
                            new String[]{"C", "三级", "03C"},
                    };
    
            //创建表格
            Table table = new Table(doc, true);
            table.resetCells(4, 3);
            for (int i = 0; i < data.length; i++) {
                TableRow dataRow = table.getRows().get(i);
                for (int j = 0; j < data[i].length; j++) {
                    TextRange range = dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
                    range.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
                    range.getCharacterFormat().setFontName("楷体");
                    dataRow.getRowFormat().setHorizontalAlignment(RowAlignment.Center);
                    dataRow.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                }
            }
    
            //创建TextBodyPart对象
            TextBodyPart bodyPart= new TextBodyPart(doc);
            bodyPart.getBodyItems().add(table);
    
            //定位到指定书签位置
            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
            bookmarkNavigator.moveToBookmark("bookmark1");
    
            //使用表格替换原书签的内容
            bookmarkNavigator.replaceBookmarkContent(bodyPart);
    
            //保存文档
            doc.saveToFile("replaceWithTable.docx", FileFormat.Docx);
            doc.dispose();
        }
    }
    

     替换效果:

     转自:Java 操作Word书签(三):用文本、图片、表格替换书签 - E-iceblue - 博客园 (cnblogs.com)

  • 相关阅读:
    UNIGUI如何禁止关闭新窗口
    【转】华为编码规范
    awk中 使用shell的环境变量
    【转】SDL与MFC的混合
    MSSQL学习笔记
    转 在.NET环境下为网站增加IP过滤功能
    欢迎加入asp.net交流群
    配置SQL Server2005以允许远程访问
    实用的文件操作类
    VS2005 + VSS2005 实现团队开发、源代码管理、版本
  • 原文地址:https://www.cnblogs.com/wwssgg/p/15690967.html
Copyright © 2020-2023  润新知