C# iTextSharp导出PDF DEMO

1、预期导出pdf效果如下,用到了行合并,列合并,垂直,水平居中

 2、代码如下

//默认页面大小
Document document = new Document();
document.SetPageSize(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFFile, FileMode.OpenOrCreate));
document.Open();//设置字体,支持中文
BaseFont bfChinese = BaseFont.CreateFont("C:\WINDOWS\Fonts\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font fontHead = new iTextSharp.text.Font(bfChinese, 18, iTextSharp.text.Font.BOLD, new BaseColor(25, 25, 25));
iTextSharp.text.Font fontLabel = new iTextSharp.text.Font(bfChinese, 12, iTextSharp.text.Font.BOLD, new BaseColor(25, 25, 25));
iTextSharp.text.Font fontValue = new iTextSharp.text.Font(bfChinese, 12, iTextSharp.text.Font.NORMAL, new BaseColor(55, 55, 55));

int columnCount = 4;
PdfPTable table = new PdfPTable(columnCount);
table.WidthPercentage = 100; // percentage          
table.DefaultCell.Padding = 1;
table.DefaultCell.BorderWidth = 1;
table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
//table.DefaultCell.UseAscender = true; 这里写垂直居中对Para不起作用
//table.DefaultCell.UseDescender = true;
//table.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;

PdfPCell cell = new PdfPCell();


Paragraph para = new Paragraph(strHeaderText);
para.Alignment = Element.ALIGN_CENTER;
para.Font = fontHead;
cell.AddElement(para);
cell.Colspan=4;
cell.HorizontalAlignment = Element.ALIGN_CENTER;

cell.UseAscender=true;

cell.UseDescender = true;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);

//表单数据
for (int i = 0; i < dtSource.Rows.Count; i++)
{
    for (int j = 0; j < dtSource.Columns.Count; j += 2)
    {
        table.AddCell(new Phrase(dtSource.Rows[i][j].ToString(), fontLabel));
        table.AddCell(new Phrase(dtSource.Rows[i][j + 1].ToString(), fontValue));
    }
}

//审批数据
//审批意见标签
PdfPCell cellShenPiYiJianLabel = new PdfPCell();
Paragraph paraShenPiYiJianLabel = new Paragraph("审批意见");
paraShenPiYiJianLabel.Alignment = Element.ALIGN_CENTER;
paraShenPiYiJianLabel.Font = fontLabel;
cellShenPiYiJianLabel.AddElement(paraShenPiYiJianLabel);
cellShenPiYiJianLabel.Rowspan = 3;
cellShenPiYiJianLabel.HorizontalAlignment = Element.ALIGN_CENTER;
cellShenPiYiJianLabel.UseAscender = true;
cellShenPiYiJianLabel.UseDescender = true;
cellShenPiYiJianLabel.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cellShenPiYiJianLabel);

for (int j = 0; j < 3; j++)
{
     Paragraph paraShenPiYiJianContent = new Paragraph("审批内容");
     paraShenPiYiJianContent.Alignment = Element.ALIGN_CENTER;
     paraShenPiYiJianContent.Font = fontValue;

     PdfPCell cellShenPiYiJianContent = new PdfPCell();
     cellShenPiYiJianContent.Colspan = 3;
     cellShenPiYiJianContent.AddElement(paraShenPiYiJianContent);
     cellShenPiYiJianContent.UseAscender = true;
     cellShenPiYiJianContent.UseDescender = true;
     cellShenPiYiJianContent.VerticalAlignment = Element.ALIGN_MIDDLE;
     table.AddCell(cellShenPiYiJianContent);
}


document.Add(table);
document.Close();
writer.Close();
return;
原文地址:https://www.cnblogs.com/zhaogaojian/p/13451889.html