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

做美篇发网站菠菜建设网站

做美篇发网站,菠菜建设网站,泰安百度做网站的,深圳定制开发公司本项目所有源码和依赖资源都在文章顶部链接#xff0c;有需要可以下载使用 1. 需求描述 从指定位置读取一个 word 模板获取业务数据并写入该 word 模板#xff0c;生成新的 word 文档将新生成的 word 文档转换为 pdf 格式对 pdf 文档添加水印 2. 效果预览 word 模板 带水印的… 本项目所有源码和依赖资源都在文章顶部链接有需要可以下载使用 1. 需求描述 从指定位置读取一个 word 模板获取业务数据并写入该 word 模板生成新的 word 文档将新生成的 word 文档转换为 pdf 格式对 pdf 文档添加水印 2. 效果预览 word 模板 带水印的 pdf 文档 3. 实现思路 word 模板数据写入使用 poi-tl 库实现word 转 pdf 格式aspose-words 库实现pdf 增加水印aspose-pdf 库实现 4. 实现过程 4.1 依赖库准备 poi-tl 可以使用 maven 直接从中央仓库下载但是 aspose 无法下载需要从网上下载 jar 包并导入本地仓库 poi-tl dependencygroupIdcom.deepoove/groupIdartifactIdpoi-tl/artifactIdversion1.12.1/version/dependencyaspose-word 将 jar 包导入本地仓库 mvn install:install-file \-DgroupIdcom.aspose \-DartifactIdaspose-words \-Dversion15.8.0 \-Dpackagingjar \-Dfileaspose-words-15.8.0-jdk16.jar项目中添加依赖 dependencygroupIdcom.aspose/groupIdartifactIdaspose-words/artifactIdversion15.8.0/version/dependencyaspose-pdf 将 jar 包导入本地仓库 mvn install:install-file \-DgroupIdcom.aspose \-DartifactIdaspose-pdf \-Dversion17.8 \-Dpackagingjar \-Dfileaspose.pdf-17.8.jar项目中添加依赖 dependencygroupIdcom.aspose/groupIdartifactIdaspose-pdf/artifactIdversion17.8/version/dependencylicense.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: 封装模板数据MapString, Object dataMap getPersonDataMap();// step 2: 将数据写入 word 模板writeDataToWord(dataMap, wordTemplatePath, wordOutputPath);// step 3: 将 word 转换为 pdfconvertWordToPdf(wordOutputPath, pdfOutputPath);// step 4: 将 pdf 增加水印addMarkerToPdf(pdfOutputPath, pdfWithMarkerOutputPath);}// 封装业务数据用于填入模板对应占位符中private static MapString, Object getPersonDataMap() {MapString, 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(MapString, 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.yutouwan.com/news/365714/

相关文章:

  • 西安有一个电影他要拉投资做网站学网页设计制作
  • 网站主机与服务器宁津有培训做网站的
  • 微信借口的网站怎么做采购信息发布
  • 免费域名做网站线下广告宣传方式有哪些
  • 怎么选择网站建设公司网站登陆注册怎么做
  • 苏州怎么做网站排名优化安康免费做网站公司
  • 淞南网站建设做网站 用 云主机
  • 专业购物网站定制淮北矿业工程建设公司网站
  • 东莞清洁服务网站建设未来的软件开发方向是什么
  • 杭州微网站建设公司哪家好网站开发相关技术
  • 数据百度做网站好用吗济源市建设网站
  • 邯郸网站建设选哪家郑州第一附属医院不孕不育科
  • 好的策划方案网站做h5页面网站有哪些
  • 怎么做网站搜索引擎大良营销网站建设市场
  • 创建免费论坛的10个网站郑州seo排名优化
  • 金融网站建设银行四川做网站设计公司价格
  • 做网站美工未来规划百科网站模板
  • 北海网站制作公司柳州建设网官方网站
  • 企业网站是什么一家专门做母婴的网站
  • 做的网站显示不了背景图片wordpress电商主题完成度
  • 知名跟单网站做信号提供方如何开发电子商务网站
  • 什么是网站静态化怎样做网站跳转
  • 建设银行互联网网站首页定远网站开发
  • 淘宝网站怎样建阿里云网站域名备案
  • html5 图片网站模板免费商务网
  • react做的电商网站能上线吗wordpress+博客主题
  • 汕头做网站多少钱做网站动态效果心得
  • 养老院网站建设方案prestashop和wordpress
  • 做网站一定要用到dw做网站网站如何定位
  • 河北seo技术网站建设或网站优化排名