欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > pdf 添加页眉页脚,获取前五页

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

2024/11/7 21:57:36 来源:https://blog.csdn.net/tian__c/article/details/143479752  浏览:    关键词: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();}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com