document.add();
关闭文档
document.close();
段落Paragraph
Paragraph pt=new Paragraph(name,headfont);//设置字体样式
pt.setAlignment(1);//设置文字居中 0靠左 1,居中 2,靠右
pt.setIndentationLeft(12);// 左缩进
pt.setIndentationRight(12);// 右缩进
pt.setFirstLineIndent(24);// 首行缩进
paragraph.setLeading(20f); //行间距
paragraph.setSpacingBefore(5f); //设置段落上空白
paragraph.setSpacingAfter(10f); //设置段落下空白
表格table
Table table =new Table(4);//括号参数表示列
int width[] = {10,40,40};//设置每列宽度比例
table.setWidths(width);
table.setWidth(60);//占页面宽度比例
table.setAlignment(Element.ALIGN_LEFT);//居左
table.setAutoFillEmptyCells(true);//自动填满
table.setBorderWidth((float)0.1);//表格边框线条宽度
table.setPadding(1);//边距:单元格的边线与单元格内容的边距
table.setSpacing(0);//间距:单元格与单元格之间的距离
table.addCell(new Paragraph(“name”),“单元格”));//添加单元格内容
table.endHeaders();//每页都会显示表头
实例
–
生成一个只有标题的pdf
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfJava
{
public void pdfTest(String filePath) throws Exception
{
Document document = new Document(PageSize.A4, 36, 36, 36, 36);
PdfWriter.getInstance(document, new FileOutputStream(filePath));
document.open();
//中文字体,解决中文不能显示问题
BaseFont bfChinese = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.EMBEDDED);
Font cusTitleFont = new Font(bfChinese);
cusTitleFont.setSize(16);
Font cusFont = new Font(bfChinese);
cusFont.setSize(9);
//添加标题开始
Font redFont = new Font(bfChinese);
redFont.setColor(BaseColor.RED);//设置颜色
redFont.setSize(20);//设置字体大小
Paragraph chapterTitle = new Paragraph(“java生成pdf文件”, redFont);//设置内容
chapterTitle.setAlignment(1);
Chapter chapter1 = new Chapter(chapterTitle, 1);
chapter1.setNumberDepth(0);
document.add(chapter1);
//添加标题结束
//关闭文档
document.close();
}
/**
- @param args
*/
public static void main(String[] args)
{
try
{
String filePath=“d:\test1.pdf”;
new PdfJava().pdfTest(filePath);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
文件如下:
生成table内容
import java.io.FileOutputStream;
import java.util.List;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPRow;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfJava
{
public void pdfTest(String filePath) throws Exception
{
Document document = new Document(PageSize.A4, 36, 36, 36, 36);
PdfWriter.getInstance(document, new FileOutputStream(filePath));
document.open();
//中文字体,解决中文不能显示问题
BaseFont bfChinese = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.EMBEDDED);
Font cusTitleFont = new Font(bfChinese);
cusTitleFont.setSize(16);
Font cusFont = new Font(bfChinese);
cusFont.setSize(9);
//添加标题开始
Font redFont = new Font(bfChinese);
redFont.setColor(BaseColor.RED);//设置颜色
redFont.setSize(20);//设置字体大小
Paragraph chapterTitle = new Paragraph(“java生成pdf文件”, redFont);//设置内容
chapterTitle.setAlignment(1);
Chapter chapter1 = new Chapter(chapterTitle, 1);
chapter1.setNumberDepth(0);
document.add(chapter1);
//添加标题结束
//添加一个内容段落
document.add(new Paragraph(“表格信息”, cusTitleFont));
// 表格.
PdfPTable table = new PdfPTable(6);
table.setWidthPercentage(100); // 宽度100%填充
table.setSpacingBefore(10f); // 前间距
table.setSpacingAfter(10f); // 后间距
List listRow = table.getRows();
//设置列宽
float[] columnWidths = { 2f, 2f, 2f ,2f, 2f, 2f};
table.setWidths(columnWidths);
//行
PdfPCell cells1[] = new PdfPCell[6];
PdfPRow row1 = new PdfPRow(cells1);
//单元格
cells1[0] = new PdfPCell(new Paragraph(“第一行第1列:”, cusFont));//单元格内容
cells1[0].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右
cells1[1] = new PdfPCell(new Paragraph(“第一行第1列值”, cusFont));
cells1[1].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[2] = new PdfPCell(new Paragraph(“第一行第2列:”, cusFont));//单元格内容
cells1[2].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右
cells1[3] = new PdfPCell(new Paragraph(“第一行第2列值”, cusFont));
cells1[3].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[4] = new PdfPCell(new Paragraph(“第一行第3列:”, cusFont));//单元格内容
cells1[4].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右
cells1[5] = new PdfPCell(new Paragraph(“第一行第3列值”, cusFont));
cells1[5].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
//把第一行添加到集合
listRow.add(row1);
//行
PdfPCell cells2[] = new PdfPCell[6];
PdfPRow row2 = new PdfPRow(cells2);
//单元格
cells2[0] = new PdfPCell(new Paragraph(“第二行第1列:”, cusFont));//单元格内容
cells2[0].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右
cells2[1] = new PdfPCell(new Paragraph(“第二行第1列值”, cusFont));
cells2[1].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells2[2] = new PdfPCell(new Paragraph(“第二行第2列:”, cusFont));//单元格内容
cells2[2].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-oRGhm87G-1715442700749)]
[外链图片转存中…(img-WJ59OM3P-1715442700749)]
[外链图片转存中…(img-2FE5oP72-1715442700750)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!