<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<!--中文乱码-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13</version>
</dependency>
<!-- pdf转图片 -->
<dependency>
<groupId>org.icepdf.os</groupId>
<artifactId>icepdf-core</artifactId>
<version>6.1.2</version>
</dependency>
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
package mirror.utils;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import mirror.file.FileList;
import org.icepdf.core.util.GraphicsRenderingHints;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.*;
import java.util.ArrayList;
public class PDFUtil {
public static void main(String[] args) throws Exception {
}
public static boolean waterMarkText(String inputFile,String outputFile, String waterMarkName) {
try {
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
stamper.setEncryption(null, "123".getBytes(), PdfWriter.ALLOW_ASSEMBLY, false);
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
int total = reader.getNumberOfPages() + 1;
PdfContentByte under;
Rectangle pageRect = null;
for (int i = 1; i < total; i++) {
pageRect = stamper.getReader().
getPageSizeWithRotation(i);
float x = 290;
float y = 400;
under = stamper.getOverContent(i);
under.saveState();
PdfGState gs = new PdfGState();
gs.setFillOpacity(0.3f);
under.setGState(gs);
under.beginText();
under.setFontAndSize(base, 35);
under.setColorFill(BaseColor.BLACK);
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, x, y, 45);
under.endText();
under.setLineWidth(1f);
under.stroke();
}
stamper.close();
reader.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean waterMarkPicture(String inputFile,String outputFile, String imagePath) {
try {
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
stamper.setEncryption(null, "123".getBytes(), PdfWriter.ALLOW_ASSEMBLY, false);
int total = reader.getNumberOfPages() + 1;
PdfContentByte under;
Rectangle pageRect = null;
for (int i = 1; i < total; i++) {
pageRect = stamper.getReader().
getPageSizeWithRotation(i);
float x = 290;
float y = 400;
under = stamper.getOverContent(i);
under.saveState();
PdfGState gs = new PdfGState();
gs.setFillOpacity(0.3f);
under.setGState(gs);
under.beginText();
Image image = Image.getInstance(imagePath);
image.setAbsolutePosition(200, 300);
image.setRotation(30);
image.setRotationDegrees(45);
image.scalePercent(90);
under.setGState(gs);
under.addImage(image);
under.setGState(gs);
under.endText();
under.stroke();
}
stamper.close();
reader.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static File Pdf(ArrayList<String> imageUrllist, String mOutputPdfFileName) {
Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
try {
PdfWriter
.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
doc.open();
for (int i = 0; i < imageUrllist.size(); i++) {
doc.newPage();
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
Paragraph paragraph = new Paragraph();
paragraph.setFont(new Font(base));
paragraph.add("简单使用iText哈哈哈oasdf");
doc.add(paragraph);
Image png1 = Image.getInstance(imageUrllist.get(i));
float heigth = png1.getHeight();
float width = png1.getWidth();
int percent = getPercent2(heigth, width);
png1.setAlignment(Image.MIDDLE);
png1.scalePercent(percent+3);
doc.add(png1);
}
doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File mOutputPdfFile = new File(mOutputPdfFileName);
if (!mOutputPdfFile.exists()) {
mOutputPdfFile.deleteOnExit();
return null;
}
return mOutputPdfFile;
}
public static int getPercent(float h, float w) {
int p = 0;
float p2 = 0.0f;
if (h > w) {
p2 = 297 / h * 100;
} else {
p2 = 210 / w * 100;
}
p = Math.round(p2);
return p;
}
public static int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.round(p2);
return p;
}
public static void pdf2Pic(String pdfPath, String path, String fileName) throws Exception {
org.icepdf.core.pobjects.Document document = new org.icepdf.core.pobjects.Document();
document.setFile(pdfPath);
float scale = 2.5f;
float rotation = 0f;
for (int i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage)
document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
RenderedImage rendImage = image;
try {
File fileDir = new File(path);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
String imgName = fileName + i + ".png";
File file = new File(path + imgName);
ImageIO.write(rendImage, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
image.flush();
}
document.dispose();
}
}