导出ptf

1:  需要的jar包

<!--pdf导出-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--pdf导出-->

2:  具体代码

++++++++++++++++++++++开始+++++++++++++++++++++

package ptf;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

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.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


public class TestPtf {

/**
* 测试导出ptf
*
* @return
* @throws Exception
*/
public static String createHdfaPDF(Map<String,String> data) throws Exception {


// 输出路径 文件名
String outPath = "测试导出.pdf";
FileOutputStream out = new FileOutputStream("d:\"+outPath);


// 设置纸张
Rectangle rect = new Rectangle(PageSize.A4);

// 创建文档实例
Document doc = new Document(rect);

// 添加中文字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

// 设置字体样式
Font textFont = new Font(bfChinese, 11, Font.NORMAL); // 正常
Font boldFont = new Font(bfChinese, 11, Font.BOLD); // 加粗
Font firsetTitleFont = new Font(bfChinese, 22, Font.BOLD); // 一级标题

// 创建输出流
PdfWriter.getInstance(doc,out);

doc.open();
doc.newPage();

// 段落
Paragraph p1 = new Paragraph(data.get("title"), firsetTitleFont);
p1.setLeading(50);
p1.setAlignment(Element.ALIGN_CENTER);
doc.add(p1);

p1 = new Paragraph(" ");
p1.setLeading(30);
doc.add(p1);

// 创建一个有4列的表格
PdfPTable table = new PdfPTable(4);
table.setTotalWidth(new float[]{105, 170, 105, 170}); // 设置列宽
table.setLockedWidth(true); // 锁定列宽

PdfPCell cell;
cell = new PdfPCell(new Phrase("xxxxx", boldFont));
cell.setMinimumHeight(30); // 设置单元格高度
cell.setUseAscender(true); // 设置可以居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(data.get("hdmc"), textFont));
cell.setUseAscender(true); // 设置可以居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
cell.setColspan(3);
table.addCell(cell);

cell = new PdfPCell(new Phrase("xxxxxx", boldFont));
cell.setMinimumHeight(40); // 设置单元格高度
cell.setUseAscender(true); // 设置可以居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(data.get("hdmb"), textFont));
cell.setUseAscender(true); // 设置可以居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
cell.setColspan(3);
table.addCell(cell);


cell = new PdfPCell(new Phrase("xxxxxxxxxx", boldFont));
cell.setMinimumHeight(30); // 设置单元格高度
cell.setUseAscender(true); // 设置可以居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase("xxxxxxxxxxx", textFont));
cell.setUseAscender(true); // 设置可以居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase("xxxxxxxxx", boldFont));
cell.setUseAscender(true); // 设置可以居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase("xxxxxxxxx", textFont));
cell.setUseAscender(true); // 设置可以居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);




cell = new PdfPCell(new Phrase("xxxxxx", boldFont));
cell.setMinimumHeight(40); // 设置单元格高度
cell.setUseAscender(true); // 设置可以居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(data.get("zzmc"), textFont));
cell.setUseAscender(true); // 设置可以居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
cell.setColspan(3);
table.addCell(cell);

cell = new PdfPCell(new Phrase("xxxxxx", boldFont));
cell.setMinimumHeight(30); // 设置单元格高度
cell.setUseAscender(true); // 设置可以居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(data.get("hdsj"), textFont));
cell.setUseAscender(true); // 设置可以居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
cell.setColspan(3);
table.addCell(cell);

cell = new PdfPCell(new Phrase("xxxxxx", boldFont));
cell.setMinimumHeight(60); // 设置单元格高度
cell.setUseAscender(true); // 设置可以居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(data.get("hdnr"), textFont));
cell.setUseAscender(true); // 设置可以居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
cell.setColspan(3);
table.addCell(cell);

cell = new PdfPCell(new Phrase("xxxxxx", boldFont));
cell.setMinimumHeight(60); // 设置单元格高度
cell.setUseAscender(true); // 设置可以居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(data.get("hdbz"), textFont));
cell.setUseAscender(true); // 设置可以居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
cell.setColspan(3);
table.addCell(cell);

cell = new PdfPCell(new Phrase("xxxxxx", boldFont));
cell.setMinimumHeight(60); // 设置单元格高度
cell.setUseAscender(true); // 设置可以居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase(data.get("xgyq"), textFont));
cell.setUseAscender(true); // 设置可以居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 设置垂直居中
cell.setColspan(3);
table.addCell(cell);

doc.add(table);
doc.close();

return outPath;
}

public static void main(String[] args) throws Exception {
Map<String,String> data = new HashMap<String,String>();
data.put("title","xxxxxxxxxxx");
data.put("hdmc","xxxxxxxxxxxxxxxx");
data.put("hdmb","xxxxxxxxxxxxxxxxxx");
data.put("zzmc","xxxxxxxxxxxxxxxxxxxx");
data.put("hdsj","xxxx年xx月--xxxx年xx月");
data.put("hdnr","xxxxxxxxxxxx");
data.put("hdbz","xxxxxxxxxxxxx");
data.put("xgyq","xxxxxxxxxxxxxx");
createHdfaPDF(data);
System.out.println("导出成功!!!!!!!!");
}
}

+++++++++++++++++++++++结束++++++++++++++++++++++

3:  效果

原文地址:https://www.cnblogs.com/xueershewang/p/12965315.html