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

pdf 添加页眉页脚,获取前五页

test

/*** 加水印、页眉、页脚*/
@Test
void d1() throws IOException {//水印 样式调整String file = "D:\\test\\2\\GB1.pdf";PdfUtil.WatermarkPDF(file);
}
/*** 获取前五页*/
@Test
void d2() throws IOException {String file = "E:\\test\\2\\3.pdf";PdfUtil.getPdfSs(file);
}

PdfUtil

package com.wlq.demo.uitls;import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.text.TextPosition;
import org.apache.pdfbox.util.Matrix;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.core.io.ClassPathResource;import java.io.*;/*** @author csb* @description: TODO* @date 2023/11/20 17:05*/
public class PdfUtil {public static void two (String in) throws IOException {// 加载PDF文件PDDocument document = PDDocument.load(new File(in));// 创建访问权限对象AccessPermission accessPermission = new AccessPermission();accessPermission.setCanPrint(true);accessPermission.setCanModify(false);accessPermission.setCanFillInForm(false);accessPermission.setCanExtractContent(false);// 创建保护策略对象StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy("password", "123456", accessPermission);// 应用保护策略到文档document.protect(protectionPolicy);PDType0Font font = PDType0Font.load(document, new File("D:\\test\\pdf\\11.ttf"));// 加水印 遍历每一页for (PDPage page : document.getPages()) {// 创建内容流以在页面上绘制文本PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);// 设置字体、颜色和透明度contentStream.setFont(font, 36);contentStream.setNonStrokingColor(200, 200, 200); // 灰色contentStream.setLineWidth(5);// 计算水印位置(居中)float x = page.getMediaBox().getWidth() / 2;float y = page.getMediaBox().getHeight() / 2;// 添加水印文本contentStream.beginText();contentStream.newLineAtOffset(x, y);contentStream.showText("水印");contentStream.endText();// 关闭内容流并保存更改contentStream.close();}// 保存加密后的PDF文件document.save("D:\\test\\pdf\\22.pdf");// 关闭文档document.close();}public static void WatermarkPDF(String in) throws IOException {// 加载PDF文档PDDocument document = PDDocument.load(new File(in));//        PDType0Font font = PDType0Font.load(document, new File("D:\\test\\pdf\\11.ttf"));PDType0Font font = PDType0Font.load(document, new ClassPathResource("/static/MC.ttf").getInputStream());
//        PDType1Font font = PDType1Font.HELVETICA_BOLD;
//        PDImageXObject image = PDImageXObject.createFromFile("D:\\test\\2\\logo.png", document);PDImageXObject image = PDImageXObject.createFromFileByExtension(new ClassPathResource("/static/logo.png").getFile(), document);float pageHeight = 0;float pageWidth = 0;float margin = 40;// 遍历每一页for (PDPage page : document.getPages()) {// 计算水印位置(居中)pageHeight = page.getMediaBox().getHeight();pageWidth = page.getMediaBox().getWidth();// 创建内容流以在页面上绘制文本PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);// 设置字体、颜色和透明度contentStream.setFont(font, 18);contentStream.setNonStrokingColor(153, 153, 153); // 灰色contentStream.setLineWidth(5);// 设置水印透明度PDExtendedGraphicsState extendedGraphicsState = new PDExtendedGraphicsState();extendedGraphicsState.setNonStrokingAlphaConstant(0.8f);contentStream.setGraphicsStateParameters(extendedGraphicsState);// 添加水印文本contentStream.beginText();for (int h = 10; h < pageHeight; h = h + 250) {for (int w = -10; w < pageWidth; w = w + 200) {contentStream.setTextMatrix(Matrix.getRotateInstance(0.7, w, h));contentStream.showText("5555奶饼@163.com");}}contentStream.endText();contentStream.restoreGraphicsState();// 关闭内容流并保存更改contentStream.close();//添加页眉// 计算图片位置float imageWidth = image.getWidth() / 2;float imageHeight = image.getHeight() / 2;float x = margin + 20;float y = page.getMediaBox().getHeight() - imageHeight - margin;contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, true);contentStream.drawImage(image, x, y, imageWidth, imageHeight);contentStream.close();// 添加页脚contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, true);contentStream.setFont(font, 12);contentStream.setNonStrokingColor(51, 0, 0); // 灰色contentStream.beginText();contentStream.moveTextPositionByAmount(pageWidth - 350, margin);contentStream.drawString("STEM标准译文商城:https://client.mt.sunther.com/mall");contentStream.endText();contentStream.close();}// 保存带有水印的PDF文档document.save("D:\\test\\2\\GB16.pdf");// 关闭文档document.close();}public static void SetPdfPassword (String in){try {// 加载PDF文件PDDocument document = PDDocument.load(new File(in));// 创建访问权限对象AccessPermission accessPermission = new AccessPermission();accessPermission.setCanPrint(true);accessPermission.setCanModify(false);accessPermission.setCanFillInForm(false);accessPermission.setCanExtractContent(false);// 创建保护策略对象StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy("password", "123456", accessPermission);// 应用保护策略到文档document.protect(protectionPolicy);//            // 保存加密后的PDF文件document.save("D:\\test\\pdf\\2.pdf");// 关闭文档document.close();} catch (IOException e) {e.printStackTrace();}}/*** 截取PDF前五页*/public static void getPdfSs (String in){try (PDDocument document = PDDocument.load(new File(in))) {if (document.getNumberOfPages() > 5) {// 截取前五页for (int i = document.getNumberOfPages() - 1; i >= document.getNumberOfPages() - 5; i--) {PDPage page = document.getPage(i);document.removePage(i);if (i < 5){document.addPage(page);}}}// 保存修改后的文档document.save("E:\\test\\2\\GB456.pdf");} catch (IOException e) {e.printStackTrace();}}
}

http://www.mrgr.cn/news/65303.html

相关文章:

  • 鸿蒙进阶-AlphabetIndexer组件
  • 针对告警数量、告警位置、告警类型等参数进行统计,并做可视化处理的智慧能源开源了。
  • Java环境下配置环境(jar包)并连接mysql数据库
  • 代码随想录第十七天
  • 向量数据库|第2期|pgvectorscale
  • 深度学习与时间序列预测的关系
  • SpringBoot新闻稿件管理系统:架构与实现
  • 编程模拟生产者和消费者问题(java)
  • Qt6 CMake 中引入 Qt Linguist 翻译功能
  • LeetCode每日一题633---平方数之和
  • runner,hook介绍
  • 在Java中如何创建一个类和对象?
  • Chromium127编译指南 Mac篇(一)- 环境准备详解
  • [实战-11] FlinkSql 设置时区对TIMESTAMP和TIMESTAMP_LTZ的影响
  • 每日一问:什么是SQL注入?注入方式和预防方法有哪些?
  • 100种算法【Python版】第35篇——PageRank算法
  • Java中的排序
  • 爱普生SG-8101CA可编程晶振应用在工业自动化机器人
  • 从0开始学习Linux——文本编辑器
  • java动态导入导出excel,javassist动态创建类
  • C/C++ stackful 有栈协同程式的一些缺点。
  • django电商易购系统-计算机设计毕业源码61059
  • JAVA通过AOP自定义注解记录日志
  • 100种算法【Python版】第38篇—— Tarjan算法
  • 智能推荐系统介绍
  • 【人工智能-初级】练习题:matplotlib基础练习30例