主要还是用到了iTextSharp,遇到的问题就是列一但多了怎么办,只能自定义宽度,
所以其中的一个关键就是把DataGridView的所有列宽(像素)转成pdf(磅). 每页高度是死的,这里按A4的高度,即842磅,
代码如下:
private void buttonPDF_Click(object sender, EventArgs e)
{
YZDataGrid dgvInfo = FindDataGrid();
DataTable dt = (DataTable)dgvInfo.DataSource;
if (dt == null || dt.Rows.Count == 0)
return;
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = this.title;
dlg.DefaultExt = ".pdf";
dlg.Filter = "Text documents (.pdf)|*.pdf";
if (dlg.ShowDialog() == DialogResult.OK)
{
string fileName = dlg.FileName;
int columnCount = dgvInfo.Columns.Count;
if (dgvInfo.Group != null)
{
columnCount--;
}
float[] columnWidth = this.exportPDFCalcWidth(dgvInfo, columnCount);

本文介绍了如何使用C#和iTextSharp库将DataGridView中的数据导出为PDF文件。关键步骤包括将DataGridView的列宽转换为PDF的磅单位,设置页面大小,创建PDF表格,并填充数据。代码示例详细展示了转换和导出的过程。
最低0.47元/天 解锁文章
1万+





