java:mysql数据库据转换pdf格式并打印机输出
Posted 杨治中
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java:mysql数据库据转换pdf格式并打印机输出相关的知识,希望对你有一定的参考价值。
使用itext实现把数据库中查到的数据转换成pdf
1.导入依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<!-- pdf输出中文要用的jar -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- 设置pdf的打开密码要用的jar 【暂时没有用到】 -->
<!--<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.54</version>
</dependency>-->
2.代码实现
package com.wmh.test;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.wmh.pojo.User;
import com.wmh.service.UserService;
import com.wmh.util.PageUtil;
import com.wmh.util.PdfFUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/test")
public class pdf
@Autowired
private UserService userService;
@RequestMapping("/pdf")
public List<User> getUser()
List<User> list =userService.getUser();
int total =list.size();
try
//TODO 1.新建document对象
Document document = new Document(PageSize.A4.rotate());// 建立一个Document对象
//TODO 2.建立一个书写器(Writer)与document对象关联
File file = new File("D:\\\\BXD.pdf"); //修改你要生成PDF的位置路径
file.createNewFile();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
//TODO 3.打开文档
document.open();
//todo 标题
Paragraph paragraph = new Paragraph("学生表", titlefont_16);
paragraph.setAlignment(1); //设置文字居中 0靠左 1,居中 2,靠右
paragraph.setIndentationLeft(12); //设置左缩进
paragraph.setIndentationRight(12); //设置右缩进
paragraph.setFirstLineIndent(24); //设置首行缩进
paragraph.setLeading(20f); //行间距
paragraph.setSpacingBefore(5f); //设置段落上空白
paragraph.setSpacingAfter(10f); //设置段落下空白
PdfPTable table_item = tbPublic();
document.add(paragraph); // 标题
int pn=1;
int ps=34;
for(int j=0;j<(total/ps)+1;j++)
PageUtil pageUtil = new PageUtil();
List<User> listPage = pageUtil.startListPage(list, pn, ps);
// 表格
PdfPTable table = PdfFUtil.createTable(new float[]75, 110, 75, 140);
for (int i = 0; i < listPage.size(); i++)
table.addCell(PdfFUtil.createCell(listPage.get(i).getId(), textfont_10));
table.addCell(PdfFUtil.createCell(listPage.get(i).getName(), textfont_10));
table.addCell(PdfFUtil.createCell(listPage.get(i).getAge(), textfont_10));
table.addCell(PdfFUtil.createCell(listPage.get(i).getSex(), textfont_10));
document.add(table_item);
document.add(table); //表格数据
PdfFUtil.onEndPage(writer,document);
pn++;
ps = 36;
//TODO 5.关闭文档
document.close();
catch (Exception e)
e.printStackTrace();
return null;
/* 表格头部公共的一行 */
public PdfPTable tbPublic()
PdfPTable table = PdfFUtil.createTable(new float[] 75, 110, 75, 140 );
table.addCell(PdfFUtil.createCell("ID", textfont_10));
table.addCell(PdfFUtil.createCell("姓名", textfont_10));
table.addCell(PdfFUtil.createCell("年龄", textfont_10));
table.addCell(PdfFUtil.createCell("性别", textfont_10));
return table;
/**
* 全局变量
*/
// 定义全局的字体静态变量
private static Font titlefont_16;
private static Font titlefontnormal_16;
private static Font headfont_14;
private static Font headfontnormal_14;
private static Font headfont_12;
private static Font headfontnormal_12;
private static Font keyfont_10;
private static Font textfont_10;
private static Font underlinefont_10;
// 静态代码块
static
try
// 不同字体(这里定义为同一种字体:包含不同字号、不同style)
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titlefont_16 = new Font(bfChinese, 16, Font.BOLD);
headfont_14 = new Font(bfChinese, 14, Font.BOLD);
headfont_12 = new Font(bfChinese, 12, Font.BOLD);
keyfont_10 = new Font(bfChinese, 10, Font.BOLD);
titlefontnormal_16 = new Font(bfChinese, 16, Font.NORMAL);
headfontnormal_14 = new Font(bfChinese, 14, Font.NORMAL);
headfontnormal_12 = new Font(bfChinese, 12, Font.NORMAL);
textfont_10 = new Font(bfChinese, 10, Font.NORMAL);
underlinefont_10 = new Font(bfChinese, 10, Font.UNDERLINE);
catch (Exception e)
e.printStackTrace();
3.拓展工具类
import java.io.IOException;
public class PdfFUtil
// 最大宽度
private static int maxWidth = 520;
/**------------------------创建表格单元格的方法start----------------------------*/
/**
* 创建单元格(指定字体)
*
* @param value
* @param font
* @return
*/
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));
return cell;
/**
* 创建单元格(指定字体、设置单元格高度)
*
* @param value
* @param font
* @return 申请事由——这行使用的方法
*/
public static PdfPCell createCell(String value, Font font, float f)
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value, font));
cell.setFixedHeight(f); // 设置表格中的单行高度
return cell;
/**
* 创建单元格(指定字体、水平局左/中/右)
*
* @param value
* @param font
* @param align
* @return
*/
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));
return cell;
/**
* 创建单元格(指定字体、水平局左/中/右、单元格跨x列合并)
*
* @param value
* @param font
* @param align
* @param colspan
* @return
*/
public PdfPCell createCell(String value, Font font, int align, int colspan)
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
cell.setHorizontalAlignment(align); //水平居中
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
return cell;
/**
* 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距)
*
* @param value
* @param font
* @param align
* @param colspan
* @param boderFlag
* @return
*/
public static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag)
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(3.0f);
if (!boderFlag)
cell.setBorder(0);
/*cell.setPaddingTop(15.0f);
cell.setPaddingBottom(8.0f);*/
cell.setPaddingTop(10.0f);
cell.setPaddingBottom(7.0f);
else if (boderFlag)
cell.setBorder(0);
cell.setPaddingTop(0.0f);
cell.setPaddingBottom(15.0f);
return cell;
/**
* 创建单元格(指定字体、水平..、边框宽度:0表示无边框、内边距)
*
* @param value
* @param font
* @param align
* @param borderWidth
* @param paddingSize
* @param flag
* @return
*/
public static PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag)
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
cell.setBorderWidthLeft(borderWidth[0]);
cell.setBorderWidthRight(borderWidth[1]);
cell.setBorderWidthTop(borderWidth[2]);
cell.setBorderWidthBottom(borderWidth[3]);
cell.setPaddingTop(paddingSize[0]);
cell.setPaddingBottom(paddingSize[1]);
if (flag)
cell.setColspan(2);
return cell;
/**------------------------创建表格单元格的方法end----------------------------*/
/**--------------------------创建表格的方法start----------------------------*/
/**
* 创建默认列宽,指定列数、水平(居中、右、左)的表格
*
* @param colNumber
* @param align
* @return
*/
public PdfPTable createTable(int colNumber, int align)
PdfPTable table = new PdfPTable(colNumber);
try
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(align);
table.getDefaultCell().setBorder(1);
catch (Exception e)
e.printStackTrace();
return table;
/**
* 创建指定列宽、列数的表格
*
* @param widths
* @return
*/
public static PdfPTable createTable(float[] widths)
PdfPTable table = new PdfPTable(widths);
try
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
catch (Exception e)
e.printStackTrace();
return table;
/**
* 创建空白的表格
*
* @return
*/
public PdfPTable createBlankTable() throws IOException, DocumentException
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font keyfont = new Font(bfChinese, 10, Font.BOLD);
PdfPTable table = new PdfPTable(1);
table.getDefaultCell().setBorder(0);
table.addCell(createCell("", keyfont));
table.setSpacingAfter(20.0f);
table.setSpacingBefore(20.0f);
return table;
/**--------------------------创建表格的方法end----------------------------*//** --------------------------页码方法start----------------------------*/
public static void onEndPage(PdfWriter writer, Document document) throws IOException, DocumentException
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tpl; // 页码模板用来固定显示数据
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
tpl = writer.getDirectContent().createTemplate(100, 100);
cb.saveState();
String text = "第" + writer.getPageNumber() + "页";
cb.beginText();
cb.setFontAndSize(bfChinese, 8);
cb.setTextMatrix(480, 35);//定位“第x页” 在具体的页面调试时候需要更改这xy的坐标
cb.showText(text);
cb.endText();
//** 创建以及固定显示总页数的位置
cb.addTemplate(tpl, 283, 10);//定位“y页” 在具体的页面调试时候需要更改这xy的坐标
cb.stroke();
cb.restoreState();
cb.closePath();
/**--------------------------页码方法end----------------------------*/
4.扩展示例(1)
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class PdfReport_BX
public static void pdf() throws Exception
try
//TODO 1.新建document对象
Document document = new Document(PageSize.A5.rotate());// 建立一个Document对象
//TODO 2.建立一个书写器(Writer)与document对象关联
File file = new File("D:\\\\BXD.pdf"); //修改你要生成PDF的位置路径
file.createNewFile();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
//writer.setPageEvent(new Watermark("HELLO WORLD"));// 水印
//TODO 3.打开文档
document.open();
//TODO 4.向文档中添加内容
new PdfReport_BX().generatePDF(document,writer); // 内容添加的方法
//TODO 5.关闭文档
document.close();
catch (Exception e)
e.printStackTrace();
// 生成PDF文件
public void generatePDF(Document document,PdfWriter writer) throws Exception
// 添加图片
Image image1 = Image.getInstance("D:/pdf/logo.png");//修改你自己本地的logo图片
image1.setAlignment(Image.ALIGN_LEFT);
image1.scalePercent(85); //依照比例缩放
PdfContentByte cd = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setCodeType(code128.CODE128);
code128.setCode("12312312312");
code128.setBarHeight(28);
code128.setFont(null); //设置为空,条形码下方的数字就没有了
Image image2 = code128.createImageWithBarcode(cd,null,null);
image2.setAlignment(Image.ALIGN_RIGHT);
image2.scalePercent(90); //依照比例缩放
image2.setAbsolutePosition(435,340);// x、y轴坐标
//todo 标题
Paragraph paragraph = new Paragraph("境内差旅报销", titlefont_16);
paragraph.setAlignment(1); //设置文字居中 0靠左 1,居中 2,靠右
paragraph.setIndentationLeft(12); //设置左缩进
paragraph.setIndentationRight(12); //设置右缩进
paragraph.setFirstLineIndent(24); //设置首行缩进
paragraph.setLeading(20f); //行间距
paragraph.setSpacingBefore(5f); //设置段落上空白
paragraph.setSpacingAfter(10f); //设置段落下空白
//todo 第一行
PdfPTable table_0 = PdfFUtil.createTable(new float[] 75, 110, 75, 140 );
table_0.addCell(PdfFUtil.createCell("xxxxxx:", textfont_10, Element.ALIGN_CENTER, 1, false));
table_0.addCell(PdfFUtil.createCell("xxxxxx", underlinefont_10, Element.ALIGN_LEFT, 1, false));
table_0.addCell(PdfFUtil.createCell("xxxxxx:", textfont_10, Element.ALIGN_CENTER, 1, false));
table_0.addCell(PdfFUtil.createCell("xxxxxx ", underlinefont_10, Element.ALIGN_LEFT, 1, false));
PdfPTable table_1 = PdfFUtil.createTable(new float[] 75, 110, 75, 140 );
table_1.addCell(PdfFUtil.createCell("xxxxxx: ", textfont_10, Element.ALIGN_CENTER, 1, false));
table_1.addCell(PdfFUtil.createCell("xxxxxx", underlinefont_10, Element.ALIGN_LEFT, 1, false));
table_1.addCell(PdfFUtil.createCell("", textfont_10, Element.ALIGN_CENTER, 1, false));
table_1.addCell(PdfFUtil.createCell("", textfont_10, Element.ALIGN_LEFT, 1, false));
// 表格
PdfPTable table = PdfFUtil.createTable(new float[] 75, 110, 75, 140 );
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx, textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
table.addCell(PdfFUtil.createCell("xxxxxx", textfont_10));
PdfPTable table_2 = PdfFUtil.createTable(new float[] 75, 325);
table_2.addCell(PdfFUtil.createCell("申请事由", textfont_10,40f));
table_2.addCell(PdfFUtil.createCell("xxxxxx", textfont_10,40f));
//表格下的落款——xxxxxx、打印时间
PdfPTable table_3 = PdfFUtil.createTable(new float[] 75, 325);
table_3.addCell(PdfFUtil.createCell("xxxxxx", textfont_10, Element.ALIGN_LEFT, 1, false));
String sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
table_3.addCell(PdfFUtil.createCell("打印时间: "+sdf, textfont_10, Element.ALIGN_RIGHT, 1, false));
document.add(image1); // logo
document.add(image2); // 条形码
document.add(paragraph); // 标题
document.add(table_0); // 显示第一行
document.add(table_1); // 显示第二行
document.add(table); //表格数据
document.add(table_2); //表格的最后一行
document.add(table_3); //表格下面的落款
/************************* 全局变量 start *************************/
// 定义全局的字体静态变量
private static Font titlefont_16;
private static Font titlefontnormal_16;
private static Font headfont_14;
private static Font headfontnormal_14;
private static Font headfont_12;
private static Font headfontnormal_12;
private static Font keyfont_10;
private static Font textfont_10;
private static Font underlinefont_10;
// 静态代码块
static
try
// 不同字体(这里定义为同一种字体:包含不同字号、不同style)
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titlefont_16 = new Font(bfChinese, 16, Font.BOLD);
headfont_14 = new Font(bfChinese, 14, Font.BOLD);
headfont_12 = new Font(bfChinese, 12, Font.BOLD);
keyfont_10 = new Font(bfChinese, 10, Font.BOLD); titlefontnormal_16 = new Font(bfChinese, 16, Font.NORMAL);
headfontnormal_14 = new Font(bfChinese, 14, Font.NORMAL);
headfontnormal_12 = new Font(bfChinese, 12, Font.NORMAL);
textfont_10 = new Font(bfChinese, 10, Font.NORMAL);
underlinefont_10 = new Font(bfChinese, 10, Font.UNDERLINE);
catch (Exception e)
e.printStackTrace();
/************************* 全局变量 end *************************/
153
扩展示例(2)
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class PdfReport_BXSP
public static void pdf(List<User> dataList) throws Exception
try
// 1.新建document对象
Document document = new Document(PageSize.A5.rotate());// 建立一个Document对象
// 2.建立一个书写器(Writer)与document对象关联
File file = new File("D:\\\\BXDSP.pdf");
file.createNewFile();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
//writer.setPageEvent(new Watermark("HELLO WORLD"));// 水印
// 3.打开文档
document.open();
// 4.向文档中添加内容
new PdfReport_BXSP().generatePDF(document,writer,dataList);
// 5.关闭文档
document.close();
catch (Exception e)
e.printStackTrace();
// 生成PDF文件
public void generatePDF(Document document,PdfWriter writer,List<User> dataList) throws Exception
int j = 0;
String sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//分批处理
if (null != dataList && dataList.size() > 0)
int pointsDataLimit = 9;//限制条数(每页显示条数)
Integer size = dataList.size();
//判断是否有必要分批
if (pointsDataLimit < size)
int part = size / pointsDataLimit;//分批数(分几页)
for (int i = 0; i < part; i++)
document.newPage();
/* 引入头部 */
header(document, writer);
//todo 表格内容填充
PdfPTable table = tbPublic();
List<User> listPage = dataList.subList(0, pointsDataLimit);
for (User user : listPage)
table.addCell(PdfFUtil.createCell(user.getNickname(), textfont_10, Element.ALIGN_LEFT));
table.addCell(PdfFUtil.createCell(user.getUsername(), textfont_10, Element.ALIGN_LEFT));
table.addCell(PdfFUtil.createCell(user.getRemarks(), textfont_10, Element.ALIGN_LEFT));
table.addCell(PdfFUtil.createCell(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss") .format(user.getCreateTime()), textfont_10, Element.ALIGN_LEFT));
//剔除
dataList.subList(0, pointsDataLimit).clear();
document.add(table); //表格数据
/* 引入底部 */
footer(document,sdf);
PdfFUtil.onEndPage(writer,document); //当前页码
if (!dataList.isEmpty())
List<User> listPage = dataList.subList(0, dataList.size());
document.newPage();
/* 引入头部 */
header(document, writer);
PdfPTable table = tbPublic();
for (User user : listPage) //表示最后剩下的数据
table.addCell(PdfFUtil.createCell(user.getNickname(), textfont_10, Element.ALIGN_LEFT));
table.addCell(PdfFUtil.createCell(user.getUsername(), textfont_10, Element.ALIGN_LEFT));
table.addCell(PdfFUtil.createCell(user.getRemarks(), textfont_10, Element.ALIGN_LEFT));
table.addCell(PdfFUtil.createCell(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss") .format(user.getCreateTime()), textfont_10, Element.ALIGN_LEFT));
document.add(table); //表格数据
/* 引入底部 */
footer(document,sdf);
PdfFUtil.onEndPage(writer,document); //当前页码
else
List<User> listPage = dataList.subList(0, dataList.size());
document.newPage();
/* 引入头部 */
header(document, writer);
PdfPTable table = tbPublic();
for (User user : listPage) //表示最后剩下的数据
table.addCell(PdfFUtil.createCell(user.getNickname(), textfont_10, Element.ALIGN_LEFT));
table.addCell(PdfFUtil.createCell(user.getUsername(), textfont_10, Element.ALIGN_LEFT));
table.addCell(PdfFUtil.createCell(user.getRemarks(), textfont_10, Element.ALIGN_LEFT));
table.addCell(PdfFUtil.createCell(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss") .format(user.getCreateTime()), textfont_10, Element.ALIGN_LEFT));
document.add(table); //表格数据
/* 引入底部 */
footer(document,sdf);
PdfFUtil.onEndPage(writer,document); //当前页码
else
System.out.println("没有数据!!!"); // 此处根据自己的业务调整
/************************** 业务封装 start ******************************/
/* 表格头部公共的一行 */
public PdfPTable tbPublic()
PdfPTable table_new = PdfFUtil.createTable(new float[] 75, 110, 75, 140 );
table_new.addCell(PdfFUtil.createCell("审批节点", headfontnormal_12));
table_new.addCell(PdfFUtil.createCell("审批人", headfontnormal_12));
table_new.addCell(PdfFUtil.createCell("审批意见", headfontnormal_12));
table_new.addCell(PdfFUtil.createCell("审批时间", headfontnormal_12));
return table_new;
/* 底部公共部分 */
public void footer(Document document, String sdf) throws DocumentException
//表格下的落款——xxxxxx、打印时间
PdfPTable table_3 = PdfFUtil.createTable(new float[] 75, 325);
table_3.addCell(PdfFUtil.createCell("xxxxxx", textfont_10, Element.ALIGN_LEFT, 1, false));
table_3.addCell(PdfFUtil.createCell("打印时间: "+sdf, textfont_10, Element.ALIGN_RIGHT, 1, false));
document.add(table_3); //表格下面的落款
/* 公共头部 */
public void header(Document document,PdfWriter writer) throws DocumentException, IOException
//todo logo图片
Image image1 = Image.getInstance("D:/pdf/logo.png");
image1.setAlignment(Image.ALIGN_LEFT);
image1.scalePercent(85); //依照比例缩放
//todo 条形码
PdfContentByte cd = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setCodeType(code128.CODE128);
code128.setCode("12312312312");
code128.setBarHeight(28);
code128.setFont(null); //设置为空,条形码下方的数字就没有了
Image image2 = code128.createImageWithBarcode(cd,null,null);
image2.setAlignment(Image.ALIGN_RIGHT);
image2.scalePercent(90); //依照比例缩放
image2.setAbsolutePosition(435,340);// x、y轴坐标
//todo 标题
Paragraph paragraph = new Paragraph("xxxxx", titlefontnormal_16);
paragraph.setAlignment(1); //设置文字居中 0靠左 1,居中 2,靠右
paragraph.setIndentationLeft(12); //设置左缩进
paragraph.setIndentationRight(12); //设置右缩进
paragraph.setFirstLineIndent(24); //设置首行缩进
paragraph.setLeading(20f); //行间距
paragraph.setSpacingBefore(5f); //设置段落上空白
paragraph.setSpacingAfter(10f); //设置段落下空白
//todo 第一、二行
PdfPTable table_0 = PdfFUtil.createTable(new float[] 75, 110, 75, 140 );
table_0.addCell(PdfFUtil.createCell("xxxxxx:", textfont_10, Element.ALIGN_CENTER, 1, false));
table_0.addCell(PdfFUtil.createCell("xxxxxx", underlinefont_10, Element.ALIGN_LEFT, 1, false));
table_0.addCell(PdfFUtil.createCell("xxxxxx:", textfont_10, Element.ALIGN_CENTER, 1, false));
table_0.addCell(PdfFUtil.createCell("xxxxxx", underlinefont_10, Element.ALIGN_LEFT, 1, false));
PdfPTable table_1 = PdfFUtil.createTable(new float[] 75, 110, 75, 140 );
table_1.addCell(PdfFUtil.createCell("xxxxxx: ", textfont_10, Element.ALIGN_CENTER, 1, false));
table_1.addCell(PdfFUtil.createCell("xxxxxx", underlinefont_10, Element.ALIGN_LEFT, 1, false));
table_1.addCell(PdfFUtil.createCell("", textfont_10, Element.ALIGN_CENTER, 1, false));
table_1.addCell(PdfFUtil.createCell("", textfont_10, Element.ALIGN_LEFT, 1, false));
document.add(image1); // logo
document.add(image2); // 条形码
document.add(paragraph); // 标题
document.add(table_0); // 显示第一行
document.add(table_1); // 显示第二行
/************************** 业务封装 end ******************************/
/************************** 全局变量 start ******************************/
// 定义全局的字体静态变量
private static Font titlefont_16;
private static Font titlefontnormal_16;
private static Font headfont_14;
private static Font headfontnormal_14;
private static Font headfont_12;
private static Font headfontnormal_12;
private static Font keyfont_10;
private static Font textfont_10;
private static Font underlinefont_10;
// 静态代码块
static
try
// 不同字体(这里定义为同一种字体:包含不同字号、不同style)
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titlefont_16 = new Font(bfChinese, 16, Font.BOLD);
headfont_14 = new Font(bfChinese, 14, Font.BOLD);
headfont_12 = new Font(bfChinese, 12, Font.BOLD);
keyfont_10 = new Font(bfChinese, 10, Font.BOLD);
titlefontnormal_16 = new Font(bfChinese, 16, Font.NORMAL);
headfontnormal_14 = new Font(bfChinese, 14, Font.NORMAL);
headfontnormal_12 = new Font(bfChinese, 12, Font.NORMAL);
textfont_10 = new Font(bfChinese, 10, Font.NORMAL);
underlinefont_10 = new Font(bfChinese, 10, Font.UNDERLINE);
catch (Exception e)
e.printStackTrace();
/************************** 全局变量 end ******************************/
log.csdn.net/weixin_46421519/article/details/123280513
以上是关于java:mysql数据库据转换pdf格式并打印机输出的主要内容,如果未能解决你的问题,请参考以下文章