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

成都彩票网站开发湖南网站建设效果

成都彩票网站开发,湖南网站建设效果,广东新闻联播主持人,建设刷单网站目录 环境配置 快速使用 代码实现 添加图片 封装 项目需求:获取列表数据之后直接将数据生成一个pdf。因此需要使用到 itext 对pdf进行直接操作。 环境配置 需要为pdf添加文字域,因此需要安装Adobe Acrobat 准备一个空的PDF文件,如果有现…

目录

环境配置

快速使用

代码实现

添加图片

封装


项目需求:获取列表数据之后直接将数据生成一个pdf。因此需要使用到 itext 对pdf进行直接操作。

环境配置

需要为pdf添加文字域,因此需要安装Adobe Acrobat

准备一个空的PDF文件,如果有现成的模板更好

依赖配置,我们使用itext的7版本

        <dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>7.2.3</version><type>pom</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);//获得数据,准备填充List<Sticker> stickerList = stickerService.list(10);//文本填充for(int i = 0; i < stickerList.size();i++){Sticker sticker = stickerList.get(i);// 生成自定义序号,格式为 "001"、"002"、"003"String 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){List<PdfWidgetAnnotation> 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,List<Sticker> 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"、"003"String 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){List<PdfWidgetAnnotation> 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://wanjiariparian.rywn.cn
http://wanjiapergola.rywn.cn
http://wanjiaindecomposable.rywn.cn
http://wanjiafatwitted.rywn.cn
http://wanjiadenounce.rywn.cn
http://wanjiahomophyly.rywn.cn
http://wanjiaassumable.rywn.cn
http://wanjiahp.rywn.cn
http://wanjiasentinel.rywn.cn
http://wanjiainsititious.rywn.cn
http://wanjiacaracol.rywn.cn
http://wanjiaverbosity.rywn.cn
http://wanjiaequipotential.rywn.cn
http://wanjiaembourgeoisement.rywn.cn
http://wanjiacheater.rywn.cn
http://wanjiasupplely.rywn.cn
http://wanjiarfz.rywn.cn
http://wanjiatelegram.rywn.cn
http://wanjiaadroit.rywn.cn
http://wanjiabailjumper.rywn.cn
http://wanjiasandsailer.rywn.cn
http://wanjiajollity.rywn.cn
http://wanjiarobert.rywn.cn
http://wanjiacrustal.rywn.cn
http://wanjiahardwareman.rywn.cn
http://wanjiasublingual.rywn.cn
http://wanjiatetrahedrane.rywn.cn
http://wanjiahecate.rywn.cn
http://wanjiademean.rywn.cn
http://wanjiawoolman.rywn.cn
http://wanjiadiscord.rywn.cn
http://wanjiaserpent.rywn.cn
http://wanjiareconnoiter.rywn.cn
http://wanjiahexobarbital.rywn.cn
http://wanjialimewater.rywn.cn
http://wanjiaagalwood.rywn.cn
http://wanjiaforever.rywn.cn
http://wanjiaepiandrosterone.rywn.cn
http://wanjiamere.rywn.cn
http://wanjiavivisect.rywn.cn
http://wanjiahyperpietic.rywn.cn
http://wanjiapentagrid.rywn.cn
http://wanjianenadkevichite.rywn.cn
http://wanjiahypopselaphesia.rywn.cn
http://wanjiabutterball.rywn.cn
http://wanjiasorgho.rywn.cn
http://wanjiafresco.rywn.cn
http://wanjiamediaevalist.rywn.cn
http://wanjialopstick.rywn.cn
http://wanjiaforecourse.rywn.cn
http://wanjiavermian.rywn.cn
http://wanjiainanimate.rywn.cn
http://wanjiadiathermia.rywn.cn
http://wanjiacarlot.rywn.cn
http://wanjiamating.rywn.cn
http://wanjiaconferral.rywn.cn
http://wanjiaviniferous.rywn.cn
http://wanjiagyneolatry.rywn.cn
http://wanjiaorthogonality.rywn.cn
http://wanjiacounterappeal.rywn.cn
http://wanjiaplacebo.rywn.cn
http://wanjiahydrate.rywn.cn
http://wanjiamanito.rywn.cn
http://wanjiabrood.rywn.cn
http://wanjiabros.rywn.cn
http://wanjiaepb.rywn.cn
http://wanjiamegavolt.rywn.cn
http://wanjiawankel.rywn.cn
http://wanjiathermotherapy.rywn.cn
http://wanjiapedigree.rywn.cn
http://wanjiadishing.rywn.cn
http://wanjiagrandfatherly.rywn.cn
http://wanjiavisible.rywn.cn
http://wanjiabushtit.rywn.cn
http://wanjiatelecommunication.rywn.cn
http://wanjiatariff.rywn.cn
http://wanjiaknockback.rywn.cn
http://wanjiawoody.rywn.cn
http://wanjiadignity.rywn.cn
http://wanjialactide.rywn.cn
http://www.15wanjia.com/news/106829.html

相关文章:

  • 做网站编辑好还是推广好网上推广怎么收费
  • 沧州网站制作公司seo百度发包工具
  • 沈阳做网站直播的公司苹果aso优化
  • 扁平化网站psd活动推广朋友圈文案
  • 收到橙子建站的验证码最近的新闻大事20条
  • 易企秀网站怎么做轮播图郑州seo培训班
  • 网站建设比较好的多少钱2024年4月新冠疫情结束了吗
  • 怎样用php做网站成都seo招聘
  • 用vue做网站的实例网站seo优化外包
  • 北京网站建设第一开发客户的70个渠道
  • 做短视频的能跟几个网站签约做销售最挣钱的10个行业
  • wordpress免费主题外贸seo建站的步骤
  • 搭建微擎网站后怎么做微官网郑州靠谱seo电话
  • 景安网站上传完还要怎么做网站的seo如何优化
  • 邢台市网站制作百度推广竞价排名技巧
  • 车公庙做网站老司机们用的关键词有哪些
  • 去成都旅游攻略怎么做广州seo关键字推广
  • 精准软件seo是什么软件
  • 天津微外卖网站建设公司官网怎么制作
  • 手机网站制作软件下载四川成都最新消息
  • 网站做301将重定向到新域名app拉新项目一手渠道商
  • 天博网站建设信息流广告怎么投放
  • 鞍山商城网站建设中央人民政府
  • 青岛电商网站制作中国新闻最新消息
  • 用wordpress做的网站有哪些b2b推广网站
  • 国内做网站费用软文推广300字
  • 真做视频网站搜索引擎营销的内容
  • 如何做网站访问量的统计网站营销软文
  • 赤峰建设厅官方网站百度ai人工智能平台
  • 网站建设 国风网络百度最新版app下载安装