图片分页

This commit is contained in:
lijiazhuo
2026-01-08 11:11:28 +08:00
parent 8ad406b5ee
commit e750e92a77
4 changed files with 519 additions and 0 deletions

View File

@ -0,0 +1,274 @@
package com.example.sso.dao;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class Photo {
private static PdfDocument pdfDoc = null;
private static Document document = null;
private static String currentPdfPath = null;
/**
* 开始创建PDF
*/
public static void startPDF(String pdfPath) throws IOException {
if (pdfDoc != null) {
closePDF();
}
/* currentPdfPath = pdfPath;
PdfWriter writer = new PdfWriter(pdfPath);
pdfDoc = new PdfDocument(writer);
// document = new Document(pdfDoc);
// 创建文档时设置0边距
document = new Document(pdfDoc, PageSize.A4, false);
// 设置页边距为0
document.setMargins(0, 0, 0, 0);
// 或者使用具体的值
// document.setMargins(0f, 0f, 0f, 0f);
// 移除默认的页眉页脚
pdfDoc.getDefaultPageSize();*/
currentPdfPath = pdfPath;
PdfWriter writer = new PdfWriter(pdfPath);
pdfDoc = new PdfDocument(writer);
// A4纸的标准宽度是595点约210mm高度自定义
// PageSize.A4.getWidth() 获取A4宽度
PageSize customPageSize = new PageSize(PageSize.A4.getWidth(), 842*20);
document = new Document(pdfDoc, customPageSize, false);
document.setMargins(0, 0, 0, 0);
System.out.println("开始创建PDF: " + pdfPath);
}
/**
* 添加图片章节
*/
public static void appendImageSection(String imagePath) throws IOException {
if (document == null) {
throw new IllegalStateException("请先调用 startPDF() 方法");
}
// 添加章节标题
// document.add(new Paragraph("图片部分")
// .setFontSize(20)
// .setBold()
// .setMarginTop(20));
// 添加图片
if (imagePath != null && new File(imagePath).exists()) {
ImageData imageData = ImageDataFactory.create(imagePath);
Image image = new Image(imageData);
// image.setAutoScale(true);
image.setHorizontalAlignment(com.itextpdf.layout.properties.HorizontalAlignment.CENTER);
image.setMarginTop(5);
image.setMarginBottom(5);
image.setHeight(200);
image.setWidth(200);
document.add(image);
// document.add(new Paragraph("图片说明")
// .setFontSize(10)
// .setItalic()
// .setTextAlignment(com.itextpdf.layout.properties.TextAlignment.CENTER));
}
System.out.println("已添加图片章节");
}
/**
* 添加文本章节
*/
public static void appendTextSection(String text) throws IOException {
if (document == null) {
throw new IllegalStateException("请先调用 startPDF() 方法");
}
PdfFont chineseFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H");
// 添加章节标题
document.add(new Paragraph(text)
.setFont(chineseFont)
.setFontSize(20)
.setBold()
.setMarginTop(20));
/* // 添加文本
if (text != null && !text.trim().isEmpty()) {
String[] paragraphs = text.split("\n");
for (String para : paragraphs) {
document.add(new Paragraph(para)
.setFontSize(12)
.setMarginBottom(10));
}
}*/
System.out.println("已添加文本章节");
}
public static void TextAlignment(String text) throws IOException {
if (document == null) {
throw new IllegalStateException("请先调用 startPDF() 方法");
}
PdfFont chineseFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H");
// 创建段落并设置居中对齐
Paragraph paragraph = new Paragraph(text)
.setFont(chineseFont)
.setFontSize(20)
.setBold()
.setMarginTop(20)
.setTextAlignment(TextAlignment.CENTER); // 添加这行实现居中
document.add(paragraph);
System.out.println("已添加文本章节");
}
public static void appendTextSections(String text) throws IOException {
if (document == null) {
throw new IllegalStateException("请先调用 startPDF() 方法");
}
PdfFont chineseFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H");
// 添加章节标题
// 设置绝对位置在页面底部
Paragraph paragraph = new Paragraph(text);
float pageHeight = PageSize.A4.getHeight(); // 842点
float bottomMargin = 50; // 距离底部50点
float leftMargin = 400; // 左边距
paragraph.setFixedPosition(leftMargin, bottomMargin, PageSize.A4.getWidth() - 40).setFont(chineseFont);
document.add(paragraph);
/* // 添加文本
if (text != null && !text.trim().isEmpty()) {
String[] paragraphs = text.split("\n");
for (String para : paragraphs) {
document.add(new Paragraph(para)
.setFontSize(12)
.setMarginBottom(10));
}
}*/
System.out.println("已添加文本章节");
}
/**
* 添加分页符
*/
public static void addPageBreak() {
if (document != null) {
document.add(new com.itextpdf.layout.element.AreaBreak());
System.out.println("已添加分页符");
}
}
/**
* 关闭并保存PDF
*/
public static void closePDF() {
if (document != null) {
document.close();
document = null;
}
if (pdfDoc != null) {
pdfDoc.close();
pdfDoc = null;
}
System.out.println("PDF已保存: " + currentPdfPath);
currentPdfPath = null;
}
/**
* 快速使用示例
*/
public static void createPDFWithMultipleSections(String pdfPath) throws IOException {
startPDF(pdfPath);
// 添加各种内容
appendTextSection("这是文档的第一部分\n包含一些介绍性的文字");
appendImageSection("image1.jpg");
appendTextSection("这是图片1的描述文字");
appendImageSection("image2.png");
appendTextSection("这是图片2的描述文字");
// 关闭并保存
closePDF();
}
public static void createSimpleChinesePDF() throws IOException {
// 1. 创建PDF
PdfWriter writer = new PdfWriter("D:\\下载模板\\a.pdf");
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc);
// 2. 加载中文字体必须要有font-asian依赖
PdfFont chineseFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H");
// 3. 添加中文内容
Paragraph title = new Paragraph("中文标题")
.setFont(chineseFont)
.setFontSize(20)
.setBold();
document.add(title);
Paragraph content = new Paragraph("这是中文内容,可以正常显示。")
.setFont(chineseFont)
.setFontSize(12);
document.add(content);
// 4. 关闭文档
document.close();
System.out.println("PDF创建完成");
}
public static void main(String[] args) {
try {
createSimpleChinesePDF();
} catch (IOException e) {
System.out.println("错误: " + e.getMessage());
System.out.println("请确保添加了 font-asian 依赖");
}
}
}