java如何到处pdf文件
今天学了java如何到处pdf文件
本文将向您展示如何使用 Free Spire.PDF for Java 将数据从Microsoft Access数据库导出到PDF表格。 此外,本文还解决了一些其他的相关问题。
- 如果表格超过一页,如何对表格进行分页?
- 如何格式化表格,例如,如何设置交替行颜色,以及如何在单元格中对齐文本?
- 如何检测到表格底部,以便我们可以在下面添加其他内容?
注:JDBC-ODBC Bridge已从Java SE 8中删除,不能再使用。要使用Java连接到Access数据库,可以使用Java SE 7或更低版本,或者需要使用UCanAccess,代替JDBC-ODBC Bridge。此示例使用JDK 6没有任何问题。
使用的代码
import com.spire.pdf.graphics.*;
import com.spire.pdf.tables.*;
import com.spire.pdf.tables.table.*;
import com.spire.pdf.tables.table.common.JdbcAdapter;
import java.awt.geom.*;
import java.awt.*;
import java.sql.*;
public class ExportDataToPdf {
public static void main(String[] args) {
//创建一个PDF文档
PdfDocument doc = new PdfDocument();
//设置页面边距
doc.getPageSettings().setMargins(30f, 30f, 30f, 30f);
//添加一个页面
PdfPageBase page = doc.getPages().add();
//初始化Y轴
float y = 0;
//创建一个笔刷
PdfBrush brush = PdfBrushes.getBlack();
//创建三种字体类型
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 16));
PdfTrueTypeFont tableFont = new PdfTrueTypeFont(new Font("Arial", 0, 10));
PdfTrueTypeFont headerFont = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 11));
PdfTrueTypeFont textFont = new PdfTrueTypeFont(new Font("Arial", 0, 12));
//在页面中心绘制题目
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
page.getCanvas().drawString("Employee Table", titleFont, brush, page.getCanvas().getClientSize().getWidth() / 2, y, format);
//计算坐标
y = y + (float) titleFont.measureString("Employee Table", format).getHeight();
y = y + 5;
//创建一个PdfTable实例
PdfTable table = new PdfTable();
//设置默认单元格格式和行格式
table.getStyle().setCellPadding(2);
table.getStyle().setBorderPen(new PdfPen(brush, 0.75f));
table.getStyle().getDefaultStyle().setBackgroundBrush(PdfBrushes.getWhite());
table.getStyle().getDefaultStyle().setFont(tableFont);
table.getStyle().getDefaultStyle().setStringFormat(new PdfStringFormat(PdfTextAlignment.Center));
table.getStyle().getAlternateStyle().setBackgroundBrush(PdfBrushes.getLightGray());
table.getStyle().getAlternateStyle().setFont(tableFont);
table.getStyle().getAlternateStyle().setStringFormat(new PdfStringFormat(PdfTextAlignment.Center));
//设置页眉格式
table.getStyle().setHeaderSource(PdfHeaderSource.Column_Captions);
table.getStyle().getHeaderStyle().setBackgroundBrush(PdfBrushes.getPurple());
table.getStyle().getHeaderStyle().setFont(headerFont);
table.getStyle().getHeaderStyle().setTextBrush(PdfBrushes.getWhite());
table.getStyle().getHeaderStyle().setStringFormat(new PdfStringFormat(PdfTextAlignment.Center));
//在每一页添加页眉
table.getStyle().setShowHeader(true);
//连接到数据库
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + "C:\\Users\\Administrator\\Documents\\data.mdb";
DataTable dataTable = new DataTable();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
try {
Connection conn = DriverManager.getConnection(url);
Statement sta = conn.createStatement();
ResultSet resultSet = sta.executeQuery("select * from employee ");
JdbcAdapter jdbcAdapter = new JdbcAdapter();
//从数据库中提取数据到datatable
jdbcAdapter.fillDataTable(dataTable, resultSet);
table.setDataSourceType(PdfTableDataSourceType.Table_Direct);
//用datatable填充表格
table.setDataSource(dataTable);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//分页
PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
tableLayout.setLayout(PdfLayoutType.Paginate);
//在指定的X轴和Y轴坐标系中绘制表格
PdfLayoutResult result = table.draw(page, new Point2D.Float(0, y), tableLayout);
//计算坐标
y = (float) result.getBounds().getHeight() + 5;
//在表格下撰写文本
result.getPage().getCanvas().drawString(String.format("* %1$s employees in the list.", table.getRows().getCount()), textFont, brush, 5, y);
//保存PDF文件。
doc.saveToFile("ExportDataToPdf.pdf");
}
}转自:https://www.jianshu.com/p/bc299659c4cc
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署