1.pdf预览
2.maven
3.代码
import com.bestvike.linq.Linq;
import com.itextpdf.text.*;
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.LineSeparator;
import java.io.FileOutputStream;
public static void main(String[] args) throws Exception {
public static void Demo() throws Exception {
Document document = new Document(PageSize.A4, 50, 50, 30, 30);
PdfWriter.getInstance(document, new FileOutputStream("E:\\develop\\tmp\\test" + System.currentTimeMillis() + ".pdf"));
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font keyfont = new Font(bfChinese, 10, Font.BOLD);
Font textfont = new Font(bfChinese, 10, Font.NORMAL);
Paragraph wrap = new Paragraph("\r\n", textfont);
Paragraph paragraph = new Paragraph("知之愈明,则行之愈笃;行之愈笃,则知之益明。——朱熹(宋)", textfont);
Paragraph line = new Paragraph();
line.add(new Chunk(new LineSeparator()));
Anchor link = new Anchor("点击跳转到百度", textfont);
link.setReference("www.baidu.com");
Image image = Image.getInstance("E:\\develop\\tmp\\install.png");
image.setAlignment(Image.ALIGN_CENTER);
PdfPTable table = createTable(new float[]{80, 80, 80, 80, 80, 80});
table.addCell(createCell("早上9:00", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("中午11:00", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("中午13:00", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("下午15:00", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("下午17:00", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("晚上19:00", keyfont, Element.ALIGN_CENTER));
for (int i = 0; i < 5; i++) {
table.addCell(createCell("起床", textfont));
table.addCell(createCell("吃午饭", textfont));
table.addCell(createCell("午休", textfont));
table.addCell(createCell("下午茶", textfont));
table.addCell(createCell("回家", textfont));
table.addCell(createCell("吃晚饭", textfont));
table.addCell(createCell("总计", textfont, Element.ALIGN_CENTER));
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorderWidthTop(0);
cell.setBorderWidthLeft(0);
cell.setPhrase(new Phrase("5天", textfont));
public static PdfPTable createTable(float[] widths) {
PdfPTable table = new PdfPTable(widths);
table.setTotalWidth(Linq.of(widths).sumFloat());
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
public static PdfPCell createCell(String value, Font font) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value, font));
public static PdfPCell createCell(String value, Font font, int align) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));