package com.qf.staff_manage.controller;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("candidate")
public class dd {
@RequestMapping("/dd")
public void createpdf(HttpServletResponse response) throws IOException {
try {
response.reset();
String fileName = new String(("信用承诺失信行为查询.pdf").getBytes("gb2312"), "ISO8859-1");
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(baos);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf, PageSize.A4);
// 创建字体
PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", true); // 设置加粗和字体大小
PdfFont font1 = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", true);
// 创建表格
Table table = new Table(5); // 3列的表格
//添加标题
table.addHeaderCell(new Cell(1, 5).add(new Paragraph("信用承诺失信行为查询").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold().setFontSize(18));
// 添加表头
table.addCell(new Cell().add(new Paragraph("序号").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold());
table.addCell(new Cell().add(new Paragraph("单位名称").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold());
table.addCell(new Cell().add(new Paragraph("是/否信用承诺失信行为").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold());
table.addCell(new Cell().add(new Paragraph("查询人").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold());
table.addCell(new Cell().add(new Paragraph("查询时间").setFont(font).setTextAlignment(TextAlignment.CENTER)).setBold());
//查询出表格数据
// 在文档中添加表格
document.add(table);
// 关闭文档
document.close();
byte[] pdfBytes = baos.toByteArray();
// 将PDF内容写入响应输出流
response.setContentLength(pdfBytes.length);
response.getOutputStream().write(pdfBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
直接复制以上代码即可完成,但是也要引入一些依赖文章来源:https://www.toymoban.com/news/detail-694350.html
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.0.3</version>
</dependency>
引入以后修修补补增增改改就可以了文章来源地址https://www.toymoban.com/news/detail-694350.html
到了这里,关于java生成pdf表格并支持下载可选另存为的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!