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

网站信息核验单网站开发最适合的浏览器

网站信息核验单,网站开发最适合的浏览器,竞价托管是啥意思,辽宁建设集团招聘信息网站文章目录 前言源码获取一、认识PDFBox二、导入依赖三、基础功能demo1#xff1a;读取pdf所有内容demo2#xff1a;读取所有页内容#xff08;分页#xff09;demo3#xff1a;添加页眉、页脚demo4#xff1a;添加居中45文字水印demo5#xff1a;添加图片到右上角 参考文… 文章目录 前言源码获取一、认识PDFBox二、导入依赖三、基础功能demo1读取pdf所有内容demo2读取所有页内容分页demo3添加页眉、页脚demo4添加居中45°文字水印demo5添加图片到右上角 参考文章资料获取 前言 博主介绍✌目前全网粉丝2Wcsdn博客专家、Java领域优质创作者博客之星、阿里云平台优质作者、专注于Java后端技术领域。 涵盖技术内容Java后端、算法、分布式微服务、中间件、前端、运维、ROS等。 博主所有博客文件目录索引博客目录索引(持续更新) 视频平台b站-Coder长路 源码获取 项目源码Gitee、Github 本篇文档的视频系列讲解Java实现自动化pdf打水印工具 开源PDF工具PDFBoxWord、Word转PDF开源工具Documents4j 一、认识PDFBox Apache PDFBox库是一个开源的Java工具专门用于处理PDF文档。它允许用户创建全新的PDF文件编辑现有的PDF文档以及从PDF文件中提取内容。 功能创建、渲染、打印、合并、拆分、加密、解密、签名等多种操作PDF文件的功能包括一个命令行工具可以用于执行各种PDF处理任务。支持文本提取和搜索以及将PDF转换为其他格式如图片和文本。 应用场景广泛应用于企业和开发者构建PDF处理相关的应用程序和工具。 Apache PDFBox具备以下主要功能 从PDF文件中提取Unicode文本。将单个PDF文件拆分成多个文件或将多个PDF文件合并成一个。从PDF表单中提取数据或填写PDF表单。验证PDF文件是否符合PDF/A-1b标准。使用标准的Java打印API打印PDF文件。将PDF文件另存为图像格式如PNG或JPEG。从零开始创建PDF文件包括嵌入字体和图像。对PDF文件进行数字签名。 二、导入依赖 dependenciesdependencygroupIdorg.apache.pdfbox/groupIdartifactIdpdfbox/artifactIdversion2.0.28/version/dependency /dependencies三、基础功能 demo1读取pdf所有内容 package com.changlu.demos;import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper;import java.io.File; import java.net.URLDecoder;/*** Description:* Author: changlu* Date: 1:28 PM*/ public class Demo1 {public static void main(String[] args) throws Exception{//读取resources目录下input.pdf文件String inputFile URLDecoder.decode(Demo1.class.getClassLoader().getResource(input.pdf).getFile(), UTF-8);PDDocument pdDocument PDDocument.load(new File(inputFile));PDFTextStripper pdfTextStripper new PDFTextStripper();//读取pdf中所有的文件String fullText pdfTextStripper.getText(pdDocument);System.out.println(fullText);} } demo2读取所有页内容分页 package com.changlu;import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper;import java.io.InputStream;/*** Description:* Author: changlu* Date: 11:19 AM*/ public class Main {public static void main(String[] args) throws Exception{//读取resources目录下input.pdf文件InputStream is Main.class.getClassLoader().getResourceAsStream(input.pdf);PDDocument pdDocument PDDocument.load(is);PDFTextStripper pdfTextStripper new PDFTextStripper();//读取所有的分页for (int i 1; i pdDocument.getNumberOfPages(); i) {//设置起始-结束页 这里设置指定某页pdfTextStripper.setStartPage(i);pdfTextStripper.setEndPage(i);//读取每一页String pageText pdfTextStripper.getText(pdDocument);System.out.println(String.format(第%s页读取内容, i));System.out.println(pageText);}} }demo3添加页眉、页脚 要求页眉页脚居中显示。 package com.changlu.demos;import com.changlu.Main; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType0Font; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.net.URLDecoder; import java.nio.file.Files; import java.nio.file.Paths;/*** Description: 添加页眉、页脚* Author: changlu* Date: 1:38 PM*/ public class Demo3 {public static void main(String[] args) throws Exception{//读取resources目录下input.pdf文件InputStream is Main.class.getClassLoader().getResourceAsStream(input.pdf);PDDocument pdDocument PDDocument.load(is);//自定义字体 C:\Users\93997\Desktop\watermark tools\watermarkTools\target\classes\ttfs//URLDecoder.decode() 方法来解码 URL 编码的路径将 %20 转换回空格String fontFile URLDecoder.decode(Main.class.getClassLoader().getResource(File.separator ttfs File.separator Alibaba_PuHuiTi_2.0_65_Medium_65_Medium.ttf).getFile(), UTF-8);PDType0Font font PDType0Font.load(pdDocument, new File(fontFile));float fontSize 10; // 设置字体大小为12// 设置透明度状态对象PDExtendedGraphicsState graphicsState new PDExtendedGraphicsState();graphicsState.setNonStrokingAlphaConstant(0.2f);graphicsState.setAlphaSourceFlag(true);graphicsState.setStrokingAlphaConstant(0.2f);//设置新的页眉String headerText 咨询专转本默默学课程联系官方报名处QQ3503851091更多资料可加群828303961;String footerText 江苏专转本公众号专转本智慧树;//遍历原先的pdf文档for (PDPage page : pdDocument.getPages()) {float pageWidth page.getMediaBox().getWidth();//计算页眉的居中位置float headerTextWidth font.getStringWidth(headerText) / 1000 * fontSize;float headerCenteredX (pageWidth - headerTextWidth) / 2; // 计算水平居中位置//计算页脚的居中位置float footerTextWidth font.getStringWidth(footerText) / 1000 * fontSize;float footerCenteredX (pageWidth - footerTextWidth) / 2; // 计算水平居中位置// 创建用于页眉的内容流PDPageContentStream headerContentStream new PDPageContentStream(pdDocument, page, PDPageContentStream.AppendMode.APPEND, true, true);headerContentStream.beginText(); // 开始文本操作headerContentStream.setFont(font, fontSize); // 设置字体和字号headerContentStream.newLineAtOffset(headerCenteredX, page.getMediaBox().getHeight() - 30); // 设置文本起始位置headerContentStream.showText(headerText); // 绘制页眉内容headerContentStream.endText(); // 结束文本操作headerContentStream.close(); // 关闭内容流// 添加页脚PDPageContentStream footerContentStream new PDPageContentStream(pdDocument, page, PDPageContentStream.AppendMode.APPEND, true, true);footerContentStream.beginText(); // 开始文本操作footerContentStream.setFont(font, fontSize); // 设置字体和字号footerContentStream.newLineAtOffset(footerCenteredX, 30); // 设置文本起始位置footerContentStream.showText(footerText); // 绘制页脚内容footerContentStream.endText(); // 结束文本操作footerContentStream.close(); // 关闭内容流}//目标目录 Thread.currentThread().getContextClassLoader().getResource().getPath() 当前工程目录路径//String targetPDFPath URLDecoder.decode(Demo3.class.getClassLoader().getResource(resources).getPath() File.separator output.pdf, UTF-8); // String targetPDFPath URLDecoder.decode(Main.class.getClassLoader().getResource(output.pdf).getFile(), UTF-8);String targetPDFPath F:\\00核心知识、成果、视频产出区\\技术视频\\2024.2.15 自制默默学打水印工具 watermark tools\\watermarkTools\\src\\main\\resources\\output.pdf;File outputFile new File(targetPDFPath);// 若是文件存在先进行删除Files.deleteIfExists(Paths.get(outputFile.toURI()));// 保存修改后的文档pdDocument.save(outputFile);System.out.println(转换任务 targetPDFPath 成功);// 关闭文档pdDocument.close(); // 关闭文档} }效果 demo4添加居中45°文字水印 要求对pdf每页都添加上旋转45°水印透明度为20%。 package com.changlu.demos;import com.changlu.Main; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType0Font; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;import java.io.File; import java.io.InputStream; import java.net.URLDecoder; import java.nio.file.Files; import java.nio.file.Paths;/*** Description: Apache PDFBox案例对pdf每页都添加上旋转45°水印。* Author: changlu* Date: 1:38 PM*/ public class Demo4 {public static void main(String[] args) throws Exception{//读取resources目录下input.pdf文件InputStream is Main.class.getClassLoader().getResourceAsStream(input.pdf);PDDocument pdDocument PDDocument.load(is);//自定义字体 C:\Users\93997\Desktop\watermark tools\watermarkTools\target\classes\ttfs//URLDecoder.decode() 方法来解码 URL 编码的路径将 %20 转换回空格String fontFile URLDecoder.decode(Main.class.getClassLoader().getResource(File.separator ttfs File.separator Alibaba_PuHuiTi_2.0_65_Medium_65_Medium.ttf).getFile(), UTF-8);PDType0Font font PDType0Font.load(pdDocument, new File(fontFile));// 设置透明度状态对象PDExtendedGraphicsState graphicsState new PDExtendedGraphicsState();graphicsState.setNonStrokingAlphaConstant(0.2f);graphicsState.setAlphaSourceFlag(true);graphicsState.setStrokingAlphaConstant(0.2f);//设置水印名String waterText 江苏专转本网课报名vxmmxchanglu;//遍历原先的pdf文档for (PDPage page : pdDocument.getPages()) {float pageWidth page.getMediaBox().getWidth();float pageHeight page.getMediaBox().getHeight();//添加水印 要求旋转45°不透明度30%float waterTextWidth font.getStringWidth(waterText) / 1000 * 30;float waterCenteredX (pageWidth - waterTextWidth) / 2;float waterCenteredY pageHeight / 2;//创建一个水印内容流PDPageContentStream waterContentStream new PDPageContentStream(pdDocument, page, PDPageContentStream.AppendMode.APPEND, true, true);waterContentStream.beginText();waterContentStream.setFont(font, 30);// 设置不透明度waterContentStream.setNonStrokingColor(0, 0, 0); // black colorwaterContentStream.setStrokingColor(0, 0, 0); // black colorwaterContentStream.setGraphicsStateParameters(graphicsState);//设置透明度//设置旋转文本 45° 对于tx、ty是以左下角为偏移位置中心来进行旋转角度waterContentStream.setTextRotation(Math.toRadians(45), 400, -50);//设置文本waterContentStream.newLineAtOffset(waterCenteredX, waterCenteredY);waterContentStream.showText(waterText);waterContentStream.endText();waterContentStream.close();}//目标目录 Thread.currentThread().getContextClassLoader().getResource().getPath() 当前工程目录路径String targetPDFPath F:\\00核心知识、成果、视频产出区\\技术视频\\2024.2.15 自制默默学打水印工具 watermark tools\\watermarkTools\\src\\main\\resources\\output.pdf;File outputFile new File(targetPDFPath);// 若是文件存在先进行删除Files.deleteIfExists(Paths.get(outputFile.toURI()));// 保存修改后的文档pdDocument.save(outputFile);System.out.println(转换任务 targetPDFPath 成功);// 关闭文档pdDocument.close(); // 关闭文档} }效果 demo5添加图片到右上角 要求将图片缩小25%后插入到右上角。 package com.changlu.demos;import com.changlu.Main; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType0Font; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;import java.io.File; import java.io.InputStream; import java.net.URLDecoder; import java.nio.file.Files; import java.nio.file.Paths;/*** Description: Apache PDFBox案例将图片缩小25%后插入到右上角。* Author: changlu* Date: 1:38 PM*/ public class Demo5 {public static void main(String[] args) throws Exception{//读取resources目录下input.pdf文件InputStream is Main.class.getClassLoader().getResourceAsStream(input.pdf);PDDocument pdDocument PDDocument.load(is);//自定义字体 C:\Users\93997\Desktop\watermark tools\watermarkTools\target\classes\ttfs//URLDecoder.decode() 方法来解码 URL 编码的路径将 %20 转换回空格String fontFile URLDecoder.decode(Main.class.getClassLoader().getResource(File.separator ttfs File.separator Alibaba_PuHuiTi_2.0_65_Medium_65_Medium.ttf).getFile(), UTF-8);PDType0Font font PDType0Font.load(pdDocument, new File(fontFile));//遍历原先的pdf文档for (PDPage page : pdDocument.getPages()) {float pageWidth page.getMediaBox().getWidth();float pageHeight page.getMediaBox().getHeight();//添加图片水印//创建一个水印内容流PDPageContentStream imageContentStream new PDPageContentStream(pdDocument, page, PDPageContentStream.AppendMode.APPEND, true, true);// 创建图像对象 // PDImageXObject image PDImageXObject.createFromFile(C:\\Users\\93997\\Desktop\\watermark tools\\watermarkTools\\src\\main\\resources\\images\\ConsultationGroupQRCode.jpg, pdDocument);String pictureFile URLDecoder.decode(Main.class.getClassLoader().getResource(File.separator images File.separator ConsultationGroupQRCode.jpg).getFile(), UTF-8);PDImageXObject image PDImageXObject.createFromFile(pictureFile, pdDocument);// 计算图像的宽度和高度缩小比例为0.3float imageWidth (float) (image.getWidth() * 0.25);float imageHeight (float) (image.getHeight() * 0.25);//具体图片位置float imageX pageWidth - imageWidth - 10;float imageY pageHeight - imageHeight - 10;// 在指定位置绘制图像imageContentStream.drawImage(image, imageX, imageY, imageWidth, imageHeight);imageContentStream.close();}//目标目录 Thread.currentThread().getContextClassLoader().getResource().getPath() 当前工程目录路径String targetPDFPath F:\\00核心知识、成果、视频产出区\\技术视频\\2024.2.15 自制默默学打水印工具 watermark tools\\watermarkTools\\src\\main\\resources\\output.pdf;File outputFile new File(targetPDFPath);// 若是文件存在先进行删除Files.deleteIfExists(Paths.get(outputFile.toURI()));// 保存修改后的文档pdDocument.save(outputFile);System.out.println(转换任务 targetPDFPath 成功);// 关闭文档pdDocument.close(); // 关闭文档} }效果 参考文章 [1]. 使用 Apache PDFBox 操作PDF文件 [2]. Java使用pdfbox将已有的pdf添加页眉 [3]. 基于pdfbox实现的pdf添加文字水印工具 资料获取 大家点赞、收藏、关注、评论啦~ 精彩专栏推荐订阅在下方专栏 长路-文章目录汇总算法、后端Java、前端、运维技术导航博主所有博客导航索引汇总开源项目Studio-Vue—校园工作室管理系统(含前后台SpringBootVue)博主个人独立项目包含详细部署上线视频已开源学习与生活-专栏可以了解博主的学习历程算法专栏算法收录 更多博客与资料可查看获取联系方式文末获取开发资源及更多资源博客获取
http://www.hkea.cn/news/14387994/

相关文章:

  • 小程序代码做网站淮安设计网站
  • 网泰网站建设邯郸高端网站建设价格
  • 网站制作的主要技术网奇seo赚钱培训
  • 个人网站如何获得流量seo 优化是什么
  • 建立网站的公司旅游公共信息服务网站建设及服务质量标准
  • 网站建设技术服务wordpress卡密销售
  • 网站建设属于电子方案网站建设方案
  • 网站系统中备案申请表培训网站方案
  • 樱花12e56手机优化助手下载
  • 开电商网站需要多少钱重庆网站建设如何
  • 怎么做网站链接天猫运营培训
  • 贵州省住房和城乡建设厅网网站网络推广优化方法
  • 我想克隆个网站 怎么做三水网站建设哪家好
  • 青海网站如何建设软件开发和网站开发
  • 哪些网站做的美网站一次性链接怎么做的
  • 中企动力做网站好吗南昌网站建设和推广
  • 佛山做外贸网站的公司吗全网搜索软件
  • 网站开发服务 税网站建设哪家g好
  • 江西旺达建设工程有限公司网站怎么做一个网站平台
  • 免费建设网站wordpress重置密码忘记
  • 静态手机网站seo文章代写平台
  • 四川住房城乡建设周刊网站wordpress 主题 教程
  • 有哪些做的很漂亮的网站公司网站建站要多少钱一年
  • 网站建设学什么语音网站备案需要资料
  • mip网站实例h5页面和小程序的页面有什么区别
  • 社交网络推广方法重庆网站推广优化软件业务
  • wordpress制作网站教程湖北手机版建站系统价格
  • 普通门户网站开发价格wordpress 此网页包含重定向循环
  • 网站做百度百科的好处同城手机网站开发
  • 72建站网杭州建设网双标化工地2022年