Aspose的使用

一、Aspose是什么?
Aspose.Words是一个商业.NET类库,可以使得应用程序处理大量的文件任务。Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XPS,EPUB和其他格式。使用Aspose.Words可以在不使用Microsoft.Word的情况下生成、修改、转换和打印文档。在项目中使用
Aspose.Words可以运行在Windows,Linux和Mac OS操作系统上面
虽然aspose的jar包是收费的,但是网上能搜到很多破解版的,去掉了页眉和水印 之类的限制
在项目中的使用就是将html标签转换为了docx文档,然后在写到输出流从浏览器中下载
部分代码:

	 String fileName =new String(getFileName(docId).getBytes("GB2312"), "ISO_8859_1") +".docx";
        Document document = null;
        String filePath = this.getClass().getResource("/").getPath()+"/"+getFileName(docId)+".docx";
        XWPFDocument docx = null;
        try {
            if (!getLicense()) {
                return;
            }
            document = new Document();
            DocumentBuilder build = new DocumentBuilder(document);
            build.insertHtml(html);
            document.save(filePath);
            docx = new XWPFDocument(new FileInputStream(filePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        outputStream = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename="+fileName);
        response.setContentType("application/msword");
        response.setCharacterEncoding("GB2312");
        docx.write(outputStream);
        -----------------------------------------------------------------------------
        public static boolean getLicense() {
    boolean result = false;
    try {
        InputStream is = HtmlImageToBase64.class.getClassLoader().getResourceAsStream("license.xml");
        License aposeLic = new License();
        aposeLic.setLicense(is);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

其中license.xml是放在src目录下的

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Aspose.Words for Java 可以很容易地修改 Word 文档中的复选框的颜色。具体步骤如下: 1. 导入 Aspose.Words for Java 的依赖包,如下: ``` <!-- Aspose.Words for Java --> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>21.5</version> </dependency> ``` 2. 加载 Word 文档,并获取其中所有的复选框,如下: ``` Document doc = new Document("example.docx"); NodeCollection checkboxes = doc.getChildNodes(NodeType.CHECK_BOX, true); ``` 3. 遍历复选框列表,修改其颜色,如下: ``` for (CheckBox checkbox : (Iterable<CheckBox>) checkboxes) { Shading shading = checkbox.getShading(); if (shading != null) { shading.setBackgroundPatternColor(Color.BLUE); } } ``` 在上面的代码中,使用 getChildNodes() 方法获取 Word 文档中的所有复选框,然后遍历列表,使用 getShading() 方法获取其 Shading 对象,再使用 setBackgroundPatternColor() 方法设置背景颜色为蓝色。 完整的示例代码如下: ``` import java.awt.Color; import java.io.FileInputStream; import java.io.FileOutputStream; import com.aspose.words.CheckBox; import com.aspose.words.Document; import com.aspose.words.NodeCollection; import com.aspose.words.NodeType; import com.aspose.words.Shading; public class ModifyWordCheckboxColor { public static void main(String[] args) throws Exception { Document doc = new Document("example.docx"); NodeCollection checkboxes = doc.getChildNodes(NodeType.CHECK_BOX, true); for (CheckBox checkbox : (Iterable<CheckBox>) checkboxes) { Shading shading = checkbox.getShading(); if (shading != null) { shading.setBackgroundPatternColor(Color.BLUE); } } doc.save("example_modified.docx"); doc.close(); } } ``` 在上面的示例代码中,使用 Document 类加载 Word 文档,修改复选框颜色后,使用 save() 方法将文档保存到指定的文件中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值