aspose.word for java 完成word生成转换及下载

使用aspose.word for java生成word并转成pdf文件供页面下载
实际开发中遇到一个问题,生成的word文件中的复选选框一直有问题
此为解决此问题的一个示例

文件模板:
文件模板
目标文件:
在这里插入图片描述

//主要使用的jar
<dependency>
	<groupId>com.aspose.words</groupId>
	<artifactId>aspose-words-jdk16</artifactId>
	<classifier>jdk16</classifier>
	<version>15.8.0</version>
</dependency>
//aspose.word for java 认证
/**
按照自己需求选择其中一种方式
aposeLic.setLicense(String licenseName);
aposeLic.setLicense(InputStream stream);
*/

private static boolean getLicense() {
        boolean result = false;
        try {
//            InputStream is = AsposeWordDemoTest.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
//            aposeLic.setLicense(is);
            aposeLic.setLicense("D:\\temp\\license.xml");
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

/** 方框value */
    public static final String CODE_BOX = "\uF0A3";
    /** 方框打勾value */
    public static final String CODE_TICK = "\uF052";

public static void generate() throws Exception {

        getLicense();

        Document doc = new Document("D:\\temp\\temp1.docx");

        // Add a handler for the MergeField event
        doc.getMailMerge().setFieldMergingCallback(new HandleMergeFieldTest());


        String[] titles = new String[]{"title","date"};
        String[] titleVals = new String[]{"示例表","2021-04-26"};
        doc.getMailMerge().execute(titles, titleVals);

        DataTable stationWorkLogTable = new DataTable("tableName");
        stationWorkLogTable.getColumns().add("name");
        stationWorkLogTable.getColumns().add("field1");
        stationWorkLogTable.getColumns().add("field2");
        stationWorkLogTable.getColumns().add("field3");
        stationWorkLogTable.getColumns().add("field4");
        stationWorkLogTable.getColumns().add("checkBox1");
        stationWorkLogTable.getColumns().add("checkBox2");

        DataRow row1 = stationWorkLogTable.newRow();
        row1.set(0, "XX1");
        row1.set(1, "数据1");
        row1.set(2, "数据2");
        row1.set(3, "数据3");
        row1.set(4, "数据4");
        row1.set(5, "OFF");
        row1.set(6, "ON");
        stationWorkLogTable.getRows().add(row1);

        DataRow row2 = stationWorkLogTable.newRow();
        row2.set(0, "XX2");
        row2.set(1, "数据1");
        row2.set(2, "数据2");
        row2.set(3, "数据3");
        row2.set(4, "数据4");
        row2.set(5, "ON");
        row2.set(6, "OFF");
        stationWorkLogTable.getRows().add(row2);

        DataRow row3 = stationWorkLogTable.newRow();
        row3.set(0, "XX3");
        row3.set(1, "数据1");
        row3.set(2, "数据2");
        row3.set(3, "数据3");
        row3.set(4, "数据4");
        row3.set(5, "ON");
        row3.set(6, "ON");
        stationWorkLogTable.getRows().add(row3);

        doc.getMailMerge().executeWithRegions(stationWorkLogTable);

        // Save resulting document with a new name
        doc.save("D:\\temp\\dest.docx");
    }

private static class HandleMergeFieldTest implements IFieldMergingCallback {

        /**
         * This is called when merge field is actually merged with data in the document.
         */
        @Override
        public void fieldMerging(final FieldMergingArgs args) throws Exception {

            if (args.getFieldValue().equals("OFF")) {
                DocumentBuilder builder = new DocumentBuilder(args.getDocument());
                builder.moveToMergeField(args.getDocumentFieldName());
                builder.getFont().setName("Wingdings 2");
                builder.write(CODE_BOX);
            } else if (args.getFieldValue().equals("ON")) {
                DocumentBuilder builder = new DocumentBuilder(args.getDocument());
                builder.moveToMergeField(args.getDocumentFieldName());
                builder.getFont().setName("Wingdings 2");
                builder.write(CODE_TICK);
            }

        }

        @Override
        public void imageFieldMerging(ImageFieldMergingArgs imageFieldMergingArgs) throws Exception {

        }
    }

参考文档 https://apireference.aspose.com/words/java/com.aspose.words/IFieldMergingCallback

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值