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.*;
public class PdfUtil {public static void two (String in) throws IOException {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();}document.save("D:\\test\\pdf\\22.pdf");document.close();}public static void WatermarkPDF(String in) throws IOException {PDDocument document = PDDocument.load(new File(in));PDType0Font font = PDType0Font.load(document, new ClassPathResource("/static/MC.ttf").getInputStream());
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();}document.save("D:\\test\\2\\GB16.pdf");document.close();}public static void SetPdfPassword (String in){try {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);document.save("D:\\test\\pdf\\2.pdf");document.close();} catch (IOException e) {e.printStackTrace();}}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();}}
}