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

深圳蕾奥规划设计公司网站网页版梦幻西游好玩吗

深圳蕾奥规划设计公司网站,网页版梦幻西游好玩吗,手机微网站建设多少钱,网页制作工作怎么样目录 环境配置 快速使用 代码实现 添加图片 封装 项目需求#xff1a;获取列表数据之后直接将数据生成一个pdf。因此需要使用到 itext 对pdf进行直接操作。 环境配置 需要为pdf添加文字域#xff0c;因此需要安装Adobe Acrobat 准备一个空的PDF文件#xff0c;如果有现…目录 环境配置 快速使用 代码实现 添加图片 封装 项目需求获取列表数据之后直接将数据生成一个pdf。因此需要使用到 itext 对pdf进行直接操作。 环境配置 需要为pdf添加文字域因此需要安装Adobe Acrobat 准备一个空的PDF文件如果有现成的模板更好 依赖配置我们使用itext的7版本 dependencygroupIdcom.itextpdf/groupIdartifactIditext7-core/artifactIdversion7.2.3/versiontypepom/type/dependency 快速使用 使用Adobe Acrobat Pro DC打开空PDF使用 文字域 工具为PDF添加文字域要注意为每个文字域命名。 如果你有现成的模板PDF直接使用识别域可以识别空白区域然后自动生成文字域但是一般都不太准确 如果你的单个数据很多的话可以在属性中设置多行  设置完文字域之后记得保存。 代码实现 SpringBootTest class StickerApplicationTests {private static final String TEMP_PATH C:\\Users\\An1ong\\Desktop\\Stickers.pdf;//生成PDF的位置private static final String DEST_PATH C:\\Users\\An1ong\\Desktop\\StickersOut.pdf;//本地上字体的路径private static final String FONT_PATH ;Autowiredprivate StickerService stickerService;Testvoid contextLoads() throws IOException {//创建一个新的PDF文件并写入数据PdfReader reader new PdfReader(TEMP_PATH);// 创建一个 PdfWriter 对象以写入新的PDFPdfWriter writer new PdfWriter(DEST_PATH);// 创建一个 PdfDocument 对象PdfDocument pdfDoc new PdfDocument(reader, writer);// 获取 PDF 表单PdfAcroForm form PdfAcroForm.getAcroForm(pdfDoc, false);//获得数据准备填充ListSticker stickerList stickerService.list(10);//文本填充for(int i 0; i stickerList.size();i){Sticker sticker stickerList.get(i);// 生成自定义序号格式为 001、002、003String customId String.format(%03d, i 1);String idFieldName id (i 1);String nameFieldName name (i 1);PdfFormField idField form.getField(idFieldName);if (idField ! null) {idField.setValue(customId);}PdfFormField nameField form.getField(nameFieldName);if (nameField ! null) {nameField.setValue(sticker.getStickerName());}}//消除掉表单域form.flattenFields();//关闭流pdfDoc.close();} } 行数也不算少但里面的逻辑其实很简单。这是一个Springboot的单元测试我调用service中的方法获取了一个装着对象的列表。 用PdfReader读取你要套写的模板用PdfWriter将数据写入模板。创建出一个PdfDocument对象并将这两个参数传入就可以开始对PDF操作了。 注意这个过程不会直接在原PDF上操作而是生成一个新的PDF进行操作程序结束后原PDF模版还是空白的。 PdfAcroFrom获取PDF表单然后PdfFormField获取其中的文字域最后使用for循环动态的将数据套打在模板上就完成了。 最终会生成一个新的文件 最终效果 之所以要在最后调用form.flattenFields消除掉表单域是因为如果不消除表单域的话就会变成这样。 更新...... 添加图片 现在又有新的需求除了自定义序号和文本之外还要每个的后面添加图片。 添加文本域 代码实现 PdfFormField imgField form.getField(imgFieldName);if (imgField ! null){ListPdfWidgetAnnotation widgets imgField.getWidgets();PdfWidgetAnnotation widget widgets.get(0);float x1 widget.getRectangle().getAsNumber(0).floatValue();float y1 widget.getRectangle().getAsNumber(1).floatValue();float x2 widget.getRectangle().getAsNumber(2).floatValue();float y2 widget.getRectangle().getAsNumber(3).floatValue();float fieldWidth x2 - x1;float fieldHeight y2 - y1;Image image img.scaleToFit(fieldWidth,fieldHeight);image.setFixedPosition(x1,y1);Document document new Document(pdfDoc);document.add(image);} 由于文本域是放置文本的不能直接放置一个图片上去。所以我们实现的思路是在放置图片的位置一个文本域然后根据文本域的坐标将图片移过去这就是这段代码的思路。 可以得到文本域左下角坐标和右上角坐标 通过简单的数学计算得到这块矩形的长和宽然后使用scaleToFit就可以让图片的长和宽与文字域矩形的长宽一样了。 封装 我们可以把这个在单元测试中的程序封装成工具类重复使用 package com.wal.sticker.util;import com.itextpdf.forms.PdfAcroForm; import com.itextpdf.forms.fields.PdfFormField; import com.itextpdf.io.image.ImageDataFactory; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.annot.PdfWidgetAnnotation; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Image; import com.wal.sticker.pojo.Sticker;import java.io.IOException; import java.net.URISyntaxException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List;public class PdfPrintUtil {// private static final String TEMP_PATH C:\\Users\\An1ong\\Desktop\\Stickers.pdf; // // private static final String DEST_PATH C:\\Users\\An1ong\\Desktop\\StickersOut.pdf;public static void printPDF(String DEST_PATH,ListSticker stickerList) throws IOException, URISyntaxException {String TEMP_PATH getResourcePath(templates/background.pdf);String IMG_PATH getResourcePath(static/img/buttonImg.png);//创建一个新的PDF文件并写入数据PdfReader reader new PdfReader(TEMP_PATH);// 创建一个 PdfWriter 对象以写入新的PDFPdfWriter writer new PdfWriter(DEST_PATH);// 创建一个 PdfDocument 对象PdfDocument pdfDoc new PdfDocument(reader, writer);// 获取 PDF 表单PdfAcroForm form PdfAcroForm.getAcroForm(pdfDoc, false);// 加载图片Image img new Image(ImageDataFactory.create(IMG_PATH));//文本和图片填充for(int i 0; i stickerList.size();i){Sticker sticker stickerList.get(i);// 生成自定义序号格式为 001、002、003String customId String.format(%03d, i 1);String idFieldName id (i 1);String nameFieldName text (i 1);String imgFieldName img (i 1);PdfFormField idField form.getField(idFieldName);if (idField ! null) {idField.setValue(customId);}PdfFormField nameField form.getField(nameFieldName);if (nameField ! null) {nameField.setValue(sticker.getStickerName());}// 添加图像PdfFormField imgField form.getField(imgFieldName);if (imgField ! null){ListPdfWidgetAnnotation widgets imgField.getWidgets();PdfWidgetAnnotation widget widgets.get(0);float x1 widget.getRectangle().getAsNumber(0).floatValue();float y1 widget.getRectangle().getAsNumber(1).floatValue();float x2 widget.getRectangle().getAsNumber(2).floatValue();float y2 widget.getRectangle().getAsNumber(3).floatValue();float fieldWidth x2 - x1;float fieldHeight y2 - y1;Image image img.scaleToFit(fieldWidth,fieldHeight); // // float scaledWidth image.getImageScaledWidth(); // float scaledHeight image.getImageScaledHeight(); // // float centerX x1 (fieldWidth / 2) - (scaledWidth / 2); // float centerY x2 (fieldHeight / 2) - (scaledHeight / 2);image.setFixedPosition(x1,y1);Document document new Document(pdfDoc);document.add(image);}}//消除掉表单域 // form.flattenFields();//关闭流pdfDoc.close();}private static String getResourcePath(String fileName) throws URISyntaxException {ClassLoader classLoader PdfPrintUtil.class.getClassLoader();Path path Paths.get(classLoader.getResource(fileName).toURI());return path.toString();}}
http://www.hkea.cn/news/14334486/

相关文章:

  • 湖南网站建设欧黎明wordpress微信登录调用
  • 网站原型设计和版式设计沈阳网站建设活动方案
  • 网站建设设计基础如何绑定网站域名
  • 简单企业网站模板百度百科官网首页
  • 备案添加网站网站建设费专用票
  • 做网站卖机器怎么弄沈阳网站制作公司哪家好
  • 如何让搜索引擎不收录网站电子商务平台定制开发
  • 山东建设局网站电工做营销型网站多少钱
  • 自己的电脑做网站服务器企业员工管理系统
  • 宁波建网站找哪家wordpress 手机 判断
  • 楼盘网站建设方案ppt成都电商网站建设
  • 定制型网站制作wordpress 欢迎插件
  • 做毕业设计实物的网站网站付费推广有哪些
  • 有高并发,高访问量网站开发企业级软件
  • 鹿班设计网站官网wordpress 文档 插件
  • 常用网站推广方法及资源wordpress 修改后台登陆名字
  • 网站淘客宝怎么做免费的进销存软件哪个简单好用
  • 宁波网站推广公司有哪些互联网技术网站
  • 室内设计师的网站网站模板内容页
  • 定制网站对公司有什么好处上传网站
  • 企业网站的建设与维护是什么网站建设 办公系统
  • 旅游网站建设规划方案免费logo设计生成器在线制作
  • dw做的网站如何上传云服务器网络推广公司简介模板
  • hyip网站开发怎么做淘宝客网站赚钱
  • 网站管理助手未找到iis广告推广公司
  • 网站如何做站内站跑腿个人网站怎么做
  • django企业网站源码网站规划与开发技术
  • 素材网站的图可以做海报吗免费签名设计
  • 做网站报价手机网站的制作
  • 网站推广的常用方法有哪些?西安seo包年服务