OFD开发系列(一)-添加水印

原文链接:https://blog.csdn.net/oAXuHui/article/details/109290539

一、OFD前言
        OFD文档目前在国内如雨后春笋般出现,这要归功于OFD本身巧妙的版式设计、友好的国标文档《GBT_33190-2016_电子文件存储与交换格式版式文档》和各大开源作者的贡献。目前参与到该版式文档的,除了数科、福析等商业公司之外,还有很多个人开发者的项目值得学习研究,例如:ofdrw、iofd、ofd.js等。OFD应用广泛,目前在税局发票和电子合同应用较多。在可预见的未来,OFD将成为比肩PDF的文档标准。OFD基于xml语言描述,有一定的层级结构,最后打包成zip,扩展名为.ofd,可以直接使用7z进行解压缩,详细描述参考上文提到的国标文档。

二、功能特性
        目前PDF支持的业务功能大体有如下几个部分:表单填充,水印填充,签章验章,文档生成,文档修改,文档阅读等。其中,OFD同样支持这些特性,另外,OFD支持附件,目录,大纲,模板,版本等更有意思的特性。本系列文章将对这些特性和功能进行解读和开发。文章基于ofdrw开源软件,git地址:https://github.com/Trisia/ofdrw。

三、本期看点
        本期首先对水印填充进行介绍和开发。水印,在OFD里属于注释的一部分,注释有很多种类型,其中一种就是水印,该部分位于《GBT_33190-2016_电子文件存储与交换格式版式文档》第15.1章节,感兴趣的读者可以下载该标准文档进行更详细的了解和学习。

四、代码开发
4.1、引入ofdrw

        首先引入ofdrw到maven项目,ofdrw只依赖commons-io包,如下:

<!--junit-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>provided</scope>
</dependency>
 
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
    <scope>provided</scope>
</dependency>
 
<!-- ofdrw -->
<dependency>
    <groupId>org.ofdrw</groupId>
    <artifactId>ofdrw-full</artifactId>
    <version>1.6.4</version>
</dependency>

4.2、编写代码

        此处代码注意有输入路径和输出路径,我们使用try-with-resource的方式进行编码,使用OFDReader类对文档进行读取,使用OFDDoc类对文档进行修改,同时使用Annotation添加注释,通过OFDDoc把注释添加到对应的页面。代码如下:

/**
 * 水印处理
 */
@Test
public void addWatermark() throws IOException {
    Path srcP = Paths.get("src/main/resources", "x.ofd");
    Path outP = Paths.get("target/AddWatermarkAnnot.ofd");
    Path imgPath = Paths.get("src/test/resources", "eg_tulip.jpg");
 
    try (OFDReader reader = new OFDReader(srcP);
         OFDDoc ofdDoc = new OFDDoc(reader, outP)) {
 
        Double width = ofdDoc.getPageLayout().getWidth();
        Double height = ofdDoc.getPageLayout().getHeight();
        Annotation annotation = new Annotation(new ST_Box(0d, 0d, width, height), AnnotType.Watermark, ctx -> {
            FontSetting setting = new FontSetting(8, FontName.SimSun.font());
 
            ctx.setFillColor(170, 160, 165)
                    .setFont(setting)
                    .setGlobalAlpha(0.4);
              
            //对ofd页面填充8行8列的水印,并顺时针旋转45°
            for (int i = 0; i <= 8; i++) {         
                for (int j = 0; j <= 8; j++) {
                    ctx.save();
                    ctx.rotate(45);
                    ctx.translate(22.4 * i, j * 50);
                    ctx.fillText("保密资料", 10, 10);
                    ctx.restore();
                }
            }
        });
 
        ofdDoc.addAnnotation(1, annotation);
        ofdDoc.addAnnotation(2, annotation);
        ofdDoc.addAnnotation(3, annotation);
    }
    System.out.println("生成文档位置:" + outP.toAbsolutePath().toString());
}

        通过传入ST_Box确定水印的外接矩形,设置注释类型为Watermark并创建一个canvas对象;首先对canvas对象进行简单字体设置,包括填充颜色,字体,透明度等;其次,调用ctx的api进行文字水印添加;该方法设置了文字顺时针45°旋转,当然,熟悉二维变换矩阵的读者也可以调用ctx.transform()方法进行变换;然后设置了偏移量,最后填充文本,然后恢复cavas状态。

        通过ofdDoc将刚才创建的注释对象添加到第1,2,3页,此处注意,若不使用try-with-resource的方式进行编码,需主动关闭reader和ofdDoc,reader会对临时文件进行清除,ofdDoc会对新的文档进行jar包,各位可以参考这两个类的close()方法。

4.3、效果示例

五、写在最后

      下一篇文章我们将对模板填充进行介绍,包括:日期填充,文本填充,图片填充图形填充等内容;再次感谢ofdrw作者,感谢开源。期待OFD版式文档发展得更好。

版权声明:本文为CSDN博主「阿徐汇」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/oAXuHui/article/details/109290539

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
public int convert2Ofd(int imagefileid, String filename,String docdcsurl) { writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------start"); //String docdcsurl = Util.null2String(getPropValue("yzDcsUrl", "docdcsurl")); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------imagefileid=" + imagefileid + ";filename=" + filename + ";docdcsurl=" + docdcsurl); int newimagefileid = -1; try { if (imagefileid > 0 && !"".equals(filename) && !"".equals(docdcsurl)) { String fileext = ""; if (filename.indexOf(".") != -1) { fileext = filename.substring(filename.lastIndexOf(".")); } writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------fileext=" + fileext); String sourcefilepath = getImageFile(imagefileid + ""); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------sourcefilepath=" + sourcefilepath); if (!"".equals(sourcefilepath)) { String dcsurl = convert(docdcsurl, sourcefilepath, "29"); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------dcsurl=" + dcsurl); if (!"".equals(dcsurl)) { InputStream input = getInputStreamFromDcs(dcsurl); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------input=" + input); String newfilename = filename.substring(0, filename.lastIndexOf(".")) + ".ofd"; if (input != null) { newimagefileid = savePdfImageFile(input, newfilename); } if (new File(sourcefilepath).exists() && new File(sourcefilepath).isFile()) { new File(sourcefilepath).delete(); } } } } } catch (Exception e) { writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------Exception=" + e); } writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------newimagefileid=" + newimagefileid); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------end"); return newimagefileid; } 下注释
最新发布
06-08

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值