1、添加依赖XML
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
2、实现代码java
package com.shucha.deveiface.biz.test;
import java.io.File;
import java.io.FileOutputStream;
import com.itextpdf.text.*;
import org.apache.commons.io.FileUtils;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
* @author tqf
* @Description
* @Version 1.0
* @since 2022-05-25 10:59
*/
public class TestExportPdf {
public static void main(String[] args) throws Exception {
exportPdf();
}
public static void exportPdf() throws Exception {
FileOutputStream fos = new FileOutputStream("D:/123/报表.pdf");
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
document.setPageSize(PageSize.A4);
document.open();
float[] widths = {144, 113, 191};
PdfPTable table = new PdfPTable(widths);
table.setLockedWidth(true);
table.setTotalWidth(458);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
Object[][] datas = {{"区域产品销售额"},{"区域", "总销售额(万元)", "总利润(万元)简单的表格"}, {"江苏省" , 9045, 2256}, {"广东省", 3000, 690}};
for(int i = 0; i < datas.length; i++) {
for(int j = 0; j < datas[i].length; j++) {
PdfPCell pdfCell = new PdfPCell();
pdfCell.setMinimumHeight(30);
pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
pdfCell.setBackgroundColor(new BaseColor(0xdd7e6b));
pdfCell.setBorder(0);
pdfCell.setBorderWidthTop(0.1f);
pdfCell.setBorderWidthBottom(0.1f);
pdfCell.setBorderWidthLeft(0.1f);
pdfCell.setBorderWidthRight(0.1f);
pdfCell.setBorderColorBottom(new BaseColor(0x674ea7));
pdfCell.setBorderColorLeft(new BaseColor(0x674ea7));
pdfCell.setBorderColorRight(new BaseColor(0x674ea7));
pdfCell.setBorderColorTop(new BaseColor(0x674ea7));
Font font = getPdfChineseFont();
if(i == datas.length - 1 && j == 3 - 1) {
font.setColor(new BaseColor(0xff0000));
font.setSize(16);
font.setStyle("bold");
font.setStyle("italic");
font.setStyle("underline");
}
if(i == 0 && j == 0) {
pdfCell.setRowspan(1);
pdfCell.setColspan(3);
}
Paragraph paragraph = new Paragraph(datas[i][j].toString(), font);
pdfCell.setPhrase(paragraph);
table.addCell(pdfCell);
}
}
byte[] bt = FileUtils.readFileToByteArray(new File("D:/123/1.png"));
PdfPCell pdfCell = new PdfPCell();
pdfCell.setImage(Image.getInstance(bt));
table.addCell(pdfCell);
pdfCell = new PdfPCell();
pdfCell.setRowspan(1);
pdfCell.setColspan(2);
PdfPTable suTtable = new PdfPTable(new float[]{100, 100});
PdfPCell subPdfCell = new PdfPCell();
subPdfCell.setPhrase(new Paragraph("sub1", getPdfChineseFont()));
suTtable.addCell(subPdfCell);
subPdfCell = new PdfPCell();
subPdfCell.setPhrase(new Paragraph("sub2", getPdfChineseFont()));
suTtable.addCell(subPdfCell);
pdfCell.addElement(suTtable);
table.addCell(pdfCell);
document.add(table);
Image image = Image.getInstance(bt);
int x = 30;
int y = 230;
image.setAbsolutePosition(x + document.leftMargin(), PageSize.A4.getHeight() - y -
image.getHeight() - document.topMargin());
document.close();
}
* 设置字体
* @return
* @throws Exception
*/
public static Font getPdfChineseFont() throws Exception {
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
return fontChinese;
}
}
转载来源:https://juejin.cn/post/7125692493064044574