📄 createpdfdocument.java
字号:
package com.aptech.demo.itext.web;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class CreatePDFDocument {
private List<Book> bookList = null;
private ByteArrayOutputStream baos;
public CreatePDFDocument(List<Book> bookList) {
this.bookList = bookList;
baos = new ByteArrayOutputStream();
}
public ByteArrayOutputStream createPDFDocument() throws DocumentException,
IOException {
Rectangle rec = new Rectangle(PageSize.A4);
Document doc = new Document(rec);
PdfWriter.getInstance(doc, baos);
doc.open();
doc.add(addContent());
doc.close();
return baos;
}
private PdfPTable addContent() throws DocumentException, IOException {
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
Font fontHead = new Font(bf, 12, Font.BOLD, Color.blue);
PdfPTable table = new PdfPTable(4);
table.setWidths(new float[] { 35, 40, 20, 10 });
table.setWidthPercentage(100); // 宽度百分比
table.getDefaultCell().setPadding(5);
table.getDefaultCell().setBorderWidth(2);
table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
table.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
if (bookList.isEmpty()) {
PdfPCell cellHead = new PdfPCell(new Paragraph("没有符合条件的图书!",
fontHead));
cellHead.setColspan(4);
cellHead.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
table.addCell(cellHead);
table.setHeaderRows(0);
table.getDefaultCell().setBorderWidth(2);
table.getDefaultCell().setPadding(3);
return table;
}
PdfPCell bookName = new PdfPCell(new Paragraph("图书名称", fontHead));
bookName.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
PdfPCell author = new PdfPCell(new Paragraph("作者", fontHead));
author.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
PdfPCell publisher = new PdfPCell(new Paragraph("出版社", fontHead));
publisher.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
PdfPCell price = new PdfPCell(new Paragraph("价格", fontHead));
price.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
table.addCell(bookName);
table.addCell(author);
table.addCell(publisher);
table.addCell(price);
Font font = new Font(bf, 10, Font.NORMAL);
for (int i = 0; i < bookList.size(); i++) {
Book book = bookList.get(i);
PdfPCell cell1 = new PdfPCell(new Paragraph(book.getBookName(),
font));
cell1.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
cell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
PdfPCell cell2 = new PdfPCell(new Paragraph(book.getAuthor(), font));
cell2.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
cell2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
PdfPCell cell3 = new PdfPCell(new Paragraph(book.getPublisher(),
font));
cell3.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
cell3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
PdfPCell cell4 = new PdfPCell(new Paragraph(book.getPrice()
.toString(), font));
cell4.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
cell4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.setHeaderRows(0);
table.getDefaultCell().setBorderWidth(2);
table.getDefaultCell().setPadding(3);
}
return table;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -