Aspose.words 操作 word 文档

Aspose.words 操作 word 文档

对文档文字进行替换

/**
  * 对word文档文字进行替换
  */
public static void test2(){
    try {
        Document document = new Document("E:\\测试文档.docx");
        document.getRange().replace("测试文档","非测试文档");
        document.save("E:\\测试文档1.docx");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

效果演示(左为原文档):
image-20210527214615097

对文档插入文本、表格以及删除表格

/**
  * 去掉文档表格,并插入指定格式的文字以及表格
  * @throws Exception
  */
public static void test1() throws Exception {
    FileInputStream inputStream = new FileInputStream("E:\\测试文档.docx");

    Document document = new Document(inputStream);
    // 去掉表格
    NodeCollection allNode = document.getChildNodes(NodeType.TABLE, true);
    for(int i = 0; i < allNode.getCount(); i++) {
        Node node = allNode.get(i);
        node.remove();
    }
    // 获取所有的段落
    ParagraphCollection paras = document.getFirstSection().getBody().getParagraphs();
    DocumentBuilder builder = new DocumentBuilder(document);

    // 在所有段落之前添加一个新的段落
    Paragraph newPara2 = new Paragraph(document);
    // 设置新段落的样式
    newPara2.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
    newPara2.getParagraphFormat().setSpaceAfter(5);
    // 插入新的段落
    paras.insert(0,newPara2);
    Paragraph firstPara2 = paras.get(0);
    // 移动到新的段落,设置段落的字体样式,并添加文字
    builder.moveTo(firstPara2);
    Font font2 = builder.getFont();
    font2.setSize(16);
    font2.setName("仿宋_GB2312");
    builder.write("\r\n 这是新段落1");

    // 插入表格
    // 移动到第二个段落
    Paragraph firstPara = document.getFirstSection().getBody().getParagraphs().get(1);
    builder.moveTo(firstPara);
    // 插入新的表格
    Table table = builder.startTable();
    // 插入单元格
    builder.insertCell();
    // 设置表格样式
    table.setAlignment(CellVerticalAlignment.CENTER);
    table.setPreferredWidth(PreferredWidth.fromPercent(97));
    // 设置单元格样式
    Border topBorder = builder.getCellFormat().getBorders().getTop();
    topBorder.setColor(Color.RED);
    topBorder.setLineWidth(2.25);
    builder.getCellFormat().getBorders().getLeft().setLineStyle(LineStyle.NONE);
    builder.getCellFormat().getBorders().getRight().setLineStyle(LineStyle.NONE);
    builder.getCellFormat().getBorders().getBottom().setLineStyle(LineStyle.NONE);
    builder.endTable();
    // 删除多余的空行段落
    document.getFirstSection().getBody().getParagraphs().removeAt(2);

    // 在文档开头再加一个新段落
    paras = document.getFirstSection().getBody().getParagraphs();
    Paragraph newPara3 = new Paragraph(document);
    newPara3.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
    paras.insert(0,newPara3);
    Paragraph firstPara3 = paras.get(0);
    builder.moveTo(firstPara3);
    final Font font3 = builder.getFont();
    font3.setSize(65);
    font3.setColor(Color.red);
    font3.setName("方正小标宋简体");
    font3.setScaling(80);
    font3.setSpacing(-2);
    builder.write("这是新段落2");

    document.save("E:\\测试文档1.docx");
}

效果演示(左为原文档):

image-20210527214735337

参考资料:

aspose.words 操作 word 文档-文字替换、删除首行、添加水印等方案

Aspose.Words for Net之在Word中创建表格

Aspose官方文档

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值