当前位置: 首页 > news >正文

北京营销型网站建设哪家好站长

北京营销型网站建设哪家好,站长,网站开发aichengkeji,网页制作网站建设公司本项目所有源码和依赖资源都在文章顶部链接,有需要可以下载使用 1. 需求描述 从指定位置读取一个 word 模板获取业务数据并写入该 word 模板,生成新的 word 文档将新生成的 word 文档转换为 pdf 格式对 pdf 文档添加水印 2. 效果预览 word 模板 带水印的…

本项目所有源码和依赖资源都在文章顶部链接,有需要可以下载使用

1. 需求描述


  1. 从指定位置读取一个 word 模板
  2. 获取业务数据并写入该 word 模板,生成新的 word 文档
  3. 将新生成的 word 文档转换为 pdf 格式
  4. 对 pdf 文档添加水印

2. 效果预览


  1. word 模板
    在这里插入图片描述
  2. 带水印的 pdf 文档
    在这里插入图片描述

3. 实现思路


  • word 模板数据写入:使用 poi-tl 库实现
  • word 转 pdf 格式:aspose-words 库实现
  • pdf 增加水印:aspose-pdf 库实现

4. 实现过程


4.1 依赖库准备

poi-tl 可以使用 maven 直接从中央仓库下载,但是 aspose 无法下载,需要从网上下载 jar 包并导入本地仓库

  • poi-tl

        <dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.12.1</version></dependency>
    
  • aspose-word
    将 jar 包导入本地仓库

        mvn install:install-file \-DgroupId="com.aspose" \-DartifactId="aspose-words" \-Dversion="15.8.0" \-Dpackaging="jar" \-Dfile="aspose-words-15.8.0-jdk16.jar"
    

    项目中添加依赖

        <dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>15.8.0</version></dependency>
    
  • aspose-pdf
    将 jar 包导入本地仓库

        mvn install:install-file \-DgroupId="com.aspose" \-DartifactId="aspose-pdf" \-Dversion="17.8" \-Dpackaging="jar" \-Dfile="aspose.pdf-17.8.jar"
    

    项目中添加依赖

        <dependency><groupId>com.aspose</groupId><artifactId>aspose-pdf</artifactId><version>17.8</version></dependency>
    
  • license.xml
    由于 aspose 库分为免费版和收费版,免费版导出的文档带有试用水印,所以需要添加 license.xml,版权关系不在文章中写出,有需要的可以下载文章顶部链接的完整源码包。

4.2 核心实现方法
@SpringBootApplication
public class Word2PDFApplication {public static void main(String[] args) {SpringApplication.run(Word2PDFApplication.class, args);// word 模板String wordTemplatePath = "src/main/resources/templates/简历模板.docx";// 写入数据后的 wordString wordOutputPath = "src/main/resources/templates/简历模板-output.docx";// word 转换为 pdfString pdfOutputPath = "src/main/resources/templates/简历模板.pdf";// pdf 增加水印String pdfWithMarkerOutputPath = "src/main/resources/templates/简历模板-marker.pdf";// step 1: 封装模板数据Map<String, Object> dataMap = getPersonDataMap();// step 2: 将数据写入 word 模板writeDataToWord(dataMap, wordTemplatePath, wordOutputPath);// step 3: 将 word 转换为 pdfconvertWordToPdf(wordOutputPath, pdfOutputPath);// step 4: 将 pdf 增加水印addMarkerToPdf(pdfOutputPath, pdfWithMarkerOutputPath);}// 封装业务数据,用于填入模板对应占位符中private static Map<String, Object> getPersonDataMap() {Map<String, Object> personDataMap = new HashMap<>();personDataMap.put("name", "张三");personDataMap.put("sex", "男");personDataMap.put("birthDate", "1998-12-02");personDataMap.put("id", "420202199812020011");personDataMap.put("phone", "18819297766");personDataMap.put("skills", "java Spring MySQL ...");return personDataMap;}// 将业务数据写入 word 模板,并生成新的 word 文件private static void writeDataToWord(Map<String, Object> dataMap, String wordTemplatePath, String wordOutputPath) {XWPFTemplate render = XWPFTemplate.compile(wordTemplatePath).render(dataMap);File dest = new File(wordOutputPath);if (!dest.getParentFile().exists()) {dest.getParentFile().mkdirs();}try {render.writeToFile(wordOutputPath);} catch (IOException e) {throw new RuntimeException(e);}}// 将新生成的带有业务数据的 word 文档转换为 pdf 格式private static void convertWordToPdf(String wordOutputPath, String pdfOutputPath) {// 验证 License 若不验证则转化出的 pdf 文档带有水印if (!getAsposeWordLicense()) {return;}FileOutputStream os = null;try {long old = System.currentTimeMillis();File file = new File(pdfOutputPath);os = new FileOutputStream(file);Document doc = new Document(wordOutputPath);doc.save(os, SaveFormat.PDF);long now = System.currentTimeMillis();System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");} catch (Exception e) {e.printStackTrace();} finally {if (os != null) {try {os.flush();os.close();} catch (IOException e) {e.printStackTrace();}}}}// 对转换后的 pdf 文档添加水印效果private static void addMarkerToPdf(String pdfOutputPath, String pdfWithMarkerOutputPath) {// 验证 License 若不验证则增加水印后的 pdf 文档带有试用水印boolean asposePdfLicense = getAsposePdfLicense();if (!asposePdfLicense) {return;}com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(pdfOutputPath);TextStamp textStamp = new TextStamp("水印文本");textStamp.getTextState().setFontSize(14.0F);textStamp.getTextState().setFontStyle(FontStyles.Bold);textStamp.setRotateAngle(45);textStamp.setOpacity(0.2);// 设置水印间距float xOffset = 100;float yOffset = 100;// 添加水印到每一页for (Page page : pdfDocument.getPages()) {float xPosition = 0;float yPosition = 0;// 在页面上添加水印直到页面被覆盖while (yPosition < page.getRect().getHeight()) {textStamp.setXIndent(xPosition);textStamp.setYIndent(yPosition);page.addStamp(textStamp);xPosition += xOffset;// 如果水印超过页面宽度,移到下一行if (xPosition + textStamp.getWidth() > page.getRect().getWidth()) {xPosition = 0;yPosition += yOffset;}}}// 保存修改后的文档pdfDocument.save(pdfWithMarkerOutputPath);}// 验证 license,否则有试用水印private static boolean getAsposeWordLicense() {boolean result = false;InputStream is = null;try {ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml");is = resources[0].getInputStream();License asposeLic = new License();asposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}}return result;}// 验证 license,否则有试用水印private static boolean getAsposePdfLicense() {boolean result = false;InputStream is = null;try {ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml");is = resources[0].getInputStream();com.aspose.pdf.License asposeLic = new com.aspose.pdf.License();asposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}}return result;}
}
http://www.ritt.cn/news/25528.html

相关文章:

  • 晋中路桥建设集团网站广东省新闻
  • 网站logo怎么替换seo建站要求
  • 网站建设设计 飞沐网站目录
  • B2C网站开发功能表推广普通话的意义是什么
  • 上海免费网站建设seo网络营销技术
  • 百度hao123优化疫情防控措施
  • 贵州建设厅网站首页软文广告经典案例100字
  • 做网站要备案企业网站模板设计
  • 交易所网站建设教程网上的推广
  • 大连网站建设设计山东建站
  • 个人可以做导购网站吗seo实战技巧
  • 武昌做网站公司惠州百度seo在哪
  • 深圳公司注册开户怎么优化自己网站的关键词
  • 济南营销型网站建设贵吗百度热搜广告位
  • 网站备案在哪里审批软文兼职10元一篇
  • 古镇企业网站建设定制保定seo推广
  • 做网站所需要的技术优化关键词技巧
  • 某购物网站开发项目网站检测
  • 旅游网站规划方案淘宝关键词排名查询工具免费
  • 南宁网站建设策划方案怎样做企业推广
  • html做企业门户网站谷歌手机版下载安装
  • 网站建设销售开场白百度竞价推广技巧
  • 景安 怎么把网站做别名西地那非片的功效与作用
  • 中山三水网站建设搜索引擎优化什么意思
  • wordpress单位内网做网站培训机构有哪些
  • 5年网站续费多少钱广告接单平台app
  • 一般做一个网站多少钱seo综合查询是什么
  • 交易网站怎么做太原网络推广公司哪家好
  • 代刷网站是怎么做的营销型网站建设策划书
  • 网站月流量是什么意思上海网络推广营销策划方案