<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;
import java.io.File;
import java.io.FileOutputStream;
public class PdfExport {
public static void main(String[] args) {
try {
Document document = new Document(PageSize.A4);
File file = new File("D:\\Test.pdf");
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.setPageEvent(new Watermark("HELLO ITEXTPDF"));
writer.setPageEvent(new MyHeaderFooter());
document.open();
Paragraph paragraph_1 = new Paragraph("HELLO PDF!", titlefont);
paragraph_1.setAlignment(Element.ALIGN_CENTER);
paragraph_1.setIndentationLeft(12);
paragraph_1.setIndentationRight(12);
paragraph_1.setFirstLineIndent(24);
paragraph_1.setLeading(20f);
paragraph_1.setSpacingBefore(5f);
paragraph_1.setSpacingAfter(10f);
Paragraph paragraph_2 = new Paragraph();
paragraph_2.add(new Chunk(new LineSeparator()));
Paragraph paragraph_3 = new Paragraph();
paragraph_3.add(new Chunk(new DottedLineSeparator()));
Anchor link = new Anchor("baidu");
link.setReference("www.baidu.com");
Anchor goTop = new Anchor("goTop");
goTop.setReference("#top");
Image image = Image.getInstance("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1200524016,3386333024&fm=26&gp=0.jpg");
image.setAlignment(Image.ALIGN_CENTER);
image.scalePercent(40);
PdfPTable table_1 = createTable(new float[]{50, 50});
table_1.addCell(createCell("TEST", textfont, Element.ALIGN_LEFT));
table_1.addCell(createCell("TEST", textfont, Element.ALIGN_LEFT));
table_1.addCell(createCell("测试", textfont, Element.ALIGN_LEFT));
table_1.addCell(createCell("测试", textfont, Element.ALIGN_LEFT));
PdfPTable table_2 = createTable(new float[]{100});
table_2.addCell(createCell("TEST", textfont, Element.ALIGN_LEFT));
table_2.addCell(createCell("测试", textfont, Element.ALIGN_LEFT));
Paragraph paragraph_4 = new Paragraph();
paragraph_4.add(new Chunk(new LineSeparator()));
paragraph_4.setLeading(20f);
paragraph_4.setSpacingBefore(50f);
paragraph_4.setSpacingAfter(50f);
PdfPTable table_3 = createTable(new float[]{50, 50, 50, 50});
table_3.addCell(createCellWithSpan("HEAD", textfont, Element.ALIGN_CENTER, 1, 4));
table_3.addCell(createCell("AA", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("AA", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("AA", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("AA", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCellWithSpan("SPAN", textfont, Element.ALIGN_LEFT, 2, 2));
table_3.addCell(createCell("BB", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("BB", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("CC", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("CC", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("EE", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("EE", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("EE", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("EE", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("FF", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCellForImage(image, textfont, Element.ALIGN_LEFT, 2, 2));
table_3.addCell(createCell("FF", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("GG", textfont, Element.ALIGN_LEFT));
table_3.addCell(createCell("GG", textfont, Element.ALIGN_LEFT));
document.add(paragraph_1);
document.add(paragraph_2);
document.add(paragraph_3);
document.add(link);
document.add(image);
document.add(table_1);
document.add(table_2);
document.add(paragraph_4);
document.add(table_3);
document.add(goTop);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static Font titlefont;
private static Font headfont;
private static Font keyfont;
private static Font textfont;
private static int maxWidth = 520;
static {
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titlefont = new Font(bfChinese, 16, Font.BOLD);
headfont = new Font(bfChinese, 14, Font.BOLD);
keyfont = new Font(bfChinese, 10, Font.BOLD);
textfont = new Font(bfChinese, 10, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
}
public static PdfPTable createTable(float[] widths) {
PdfPTable table = new PdfPTable(widths);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
public static PdfPCell createCell(String value, Font font, int horizontalAlignment) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(horizontalAlignment);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public static PdfPCell createCellWithSpan(String value, Font font, int horizontalAlignment, int rowspan, int colspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(horizontalAlignment);
cell.setRowspan(rowspan);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public static PdfPCell createCellForImage(Image image, Font font, int horizontalAlignment, int colspan, int rowspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(horizontalAlignment);
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setImage(image);
return cell;
}
}
点击查看代码
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.IOException;
public class MyHeaderFooter extends PdfPageEventHelper {
PdfTemplate totalPage;
Font hfFont;
{
try {
hfFont = new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.NORMAL);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
totalPage = cb.createTemplate(30, 15);
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable table = new PdfPTable(2);
try {
table.setTotalWidth(PageSize.A4.getWidth());
table.setWidths(new int[]{(int) (PageSize.A4.getWidth() / 2), (int) (PageSize.A4.getWidth() / 2)});
table.setLockedWidth(true);
PdfPCell cell_1 = new PdfPCell(new Paragraph("第" + writer.getPageNumber() + "页/", hfFont));
cell_1.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell_1.setBorder(Rectangle.NO_BORDER);
table.addCell(cell_1);
PdfPCell cell_2 = new PdfPCell(Image.getInstance(totalPage));
cell_2.setBorder(Rectangle.NO_BORDER);
table.addCell(cell_2);
table.writeSelectedRows(0, -1, 0, 20, writer.getDirectContent());
} catch (Exception de) {
throw new ExceptionConverter(de);
}
}
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
String text = "总" + (writer.getPageNumber()) + "页";
ColumnText.showTextAligned(totalPage, Element.ALIGN_LEFT, new Paragraph(text, hfFont), 0, 5, 0);
}
}
点击查看代码
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
@Controller
public class PdfController {
@GetMapping("/downloadPdf")
public void downloadPdf(HttpServletResponse response) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, baos);
document.open();
Paragraph paragraph = new Paragraph("hello world");
document.add(paragraph);
document.close();
String fileName = "MyPdfTest";
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf");
response.setContentLength(baos.size());
OutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
点击查看代码
^
|
|
Y
|
+----------+
| |
| pdf |
| |
| 文 件 |
| |
| |
---+----------+----X-->
O|
|
|
public static void main(String[] args) {
Document document = new Document(PageSize.A4);
System.err.println("rectangle:" + document.getPageSize());
System.err.println("top:" + document.getPageSize().getTop());
System.err.println("bottom:" + document.getPageSize().getBottom());
System.err.println("left:" + document.getPageSize().getLeft());
System.err.println("right:" + document.getPageSize().getRight());
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了