如何使用itextsharp页面添加页脚 [英] how to add footer using itextsharp page

查看:112
本文介绍了如何使用itextsharp页面添加页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void ExportToPDF(GridView gvReport,bool LandScape)

{

int noOfColumns = 0,noOfRows = 0;

DataTable tbl = null;



if(gvReport.AutoGenerateColumns)

{

tbl = gvReport.DataSource as DataTable; //获取GridView控件的DataSource。

noOfColumns = tbl.Columns.Count;

noOfRows = tbl.Rows.Count;

}

其他

{

noOfColumns = gvReport.Columns.Count;

noOfRows = gvReport.Rows.Count ;

}







float HeaderTextSize = 8;

float ReportNameSize = 10;

float ReportTextSize = 8;

float ApplicationNameSize = 7;



//创建PDF文档

文档文档= null;

if(LandScape == true)

{

//将文档设置为A4大小并旋转它以使页面的方向为横向。

document = new Document(PageSize.A4.Rotate(),0,0,15 ,5);

}

否则

{

document = new Document(PageSize.A4,36,72,108,180);

}



//创建一个PdfPTable,其表的列数等于gridview或gridview数据源的列数。

iTextSharp.text.pdf.PdfPTable mainTable = new iTextSharp.text.pdf.PdfPTable(noOfColumns);

mainTable.TotalWidth = 500f;

mainTable.LockedWidth = true;

浮动[] widths = new float [] {50f,300f,100f,50f};

mainTable.SetWidths(widths);

//设置前4行table作为标题行,将在所有页面中重复。

mainTable.HeaderRows = 4;



//创建一个PdfPTable 2列用于保存导出的PDF中的标题。

iTextSharp.text.pdf.PdfPTable headerTable = new iTextSharp.text.pdf.PdfPTable(2);





//创建一个短语来保存标题左侧的应用程序名称。

Phrase phApplicationName = new Phrase(Sample Application,FontFactory.GetFont( Arial,ApplicationNameSize,iTextSharp.text.Font.NORMAL));



//创建一个接受短语作为参数的PdfPCell。

PdfPCell clApplicationName = new PdfPCell(phApplicationName);

//将单元格的边框设置为零。

clApplicationName.Border = PdfPCell.NO_BORDER;

//将PdfPCell的水平对齐设置为左。

clApplicationName.Horizo​​ntalAlignment = Element.ALIGN_LEFT;





//创建一个包含文件名的短语。

Phrase phHeader = new Phrase(Sample Export,FontFactory.GetFont(Arial,ReportNameSize,iTextSharp.text .Font.BOLD));

PdfPCell clHeader = new PdfP单元格(phHeader);

clHeader.Colspan = noOfColumns;

clHeader.Border = PdfPCell.NO_BORDER;

clHeader.Horizo​​ntalAlignment = Element.ALIGN_CENTER ;

mainTable.AddCell(clHeader);



//为新行创建一个短语。

短语phSpace =新词组(\ n);

PdfPCell clSpace =新PdfPCell(phSpace);

clSpace.Border = PdfPCell.NO_BORDER;

clSpace.Colspan = noOfColumns;

mainTable.AddCell(clSpace);

mainTable.DefaultCell.BorderWidth = 0;

mainTable .DefaultCell.BorderWidthBottom = 1;

//将gridview列名设置为表头。

for(int i = 0;我< noOfColumns; i ++)

{

Phrase ph = null;



if(gvReport.AutoGenerateColumns)

{

ph = new Phrase(tbl.Columns [i] .ColumnName,FontFactory.GetFont(Arial,HeaderTextSize,iTextSharp.text.Font.BOLD));

}

其他

{

ph =新词组(gvReport.Columns [i] .HeaderText,FontFactory.GetFont( Arial,HeaderTextSize,iTextSharp.text.Font.BOLD));

}



mainTable.AddCell(ph);

mainTable.DefaultCell.BorderWidth = 0;

mainTable.DefaultCell.BorderWidthBottom = 1;

}



//读取gridview行并将它们添加到mainTable

for(int rowNo = 0; rowNo< noOfRows; rowNo ++)

{

for(int columnNo = 0; columnNo < noOfColumns;如果(gvReport.AutoGenerateColumns)

{

string s = gvReport.Rows [rowNo} ] .Cells [columnNo] .Text.Trim();

Phrase ph = new Phrase(s,FontFactory.GetFont(Arial,ReportTextSize,iTextSharp.text.Font.NORMAL));

mainTable.AddCell(ph);

}

else

{

if (gvReport.Columns [columnNo]是TemplateField)

{

DataBoundLiteralControl lc = gvReport.Rows [rowNo] .Cells [columnNo] .Controls [0] as DataBoundLiteralControl;

string s = lc.Text.Replace(
,\\\\ n);

s = s.Trim();

Phrase ph = new Phrase(s,FontFactory.GetFont(Arial,ReportTextSize,iText) Sharp.text.Font.NORMAL));

mainTable.AddCell(ph);

}

else

{

string s = gvReport.Rows [rowNo] .Cells [columnNo] .Text.Trim();

Phrase ph = new Phrase(s,FontFactory.GetFont (Arial,ReportTextSize,iTextSharp.text.Font.NORMAL));

mainTable.AddCell(ph);

}

}

}



//告诉mainTable即使任何单元格不完整也要完成该行。

mainTable.CompleteRow();

}



//获取创建文档的实例并将其写入Response的输出流对象。

PdfWriter.GetInstance(document,Response.OutputStream);



//// Crea测试PDF文档的页脚。

// HeaderFooter pdfFooter = new HeaderFooter(new Phrase(),true);

//pdfFooter.Alignment = Element.ALIGN_CENTER;

//pdfFooter.Border = iTextSharp.text.Rectangle.NO_BORDER;

////将文档页脚设置为pdfFooter。

/ /document.Footer = pdfFooter;

//打开文档。

document.Open();

//将mainTable添加到文件。

段落标题=新段落(会计科目表,新字体(Font.FontFamily.HELVETICA,22f,Font.BOLD));

heading.SpacingAfter = 18f;

heading.Alignment = Element.ALIGN_LEFT;

document.Add(heading);

段para = new Paragraph();

para.Alignment = Element.ALIGN_CENTER;







//创建一个短语以显示当前日期标题的右侧。

短语phDate =新短语(DateTime.Now.Date.ToString(dd / MM / yyyy),FontFactory.GetFont(Arial,ApplicationNameSize,iTextSharp .text.Font.NORMAL));



//创建一个接受日期短语作为参数的PdfPCell。

PdfPCell clDate = new PdfPCell(phDate);

//将PdfPCell的水平对齐设置为右。

clDate.Horizo​​ntalAlignment = Element.ALIGN_RIGHT;

//将单元格的边框设置为零。

clDate.Border = PdfPCell.NO_BORDER;



//添加保留的单元格headerTable的应用程序名称。

headerTable.AddCell(clApplicationName);

//将保存日期的单元格添加到headerTable。

headerTable.AddCell(clDate);

//将headerTable的边框设置为零。

headerT able.DefaultCell.Border = PdfPCell.NO_BORDER;



//创建一个接受headerTable作为参数的PdfPCell,然后将该单元格添加到主PdfPTable中。 />
PdfPCell cellHeader = new PdfPCell(headerTable);

cellHeader.Border = PdfPCell.NO_BORDER;

//将标题单元格的列范围设置为noOfColumns。

cellHeader.Colspan = noOfColumns;

//将上面的标题单元格添加到表格中。

mainTable.AddCell(cellHeader);

document.Add(para);

document.Add(mainTable);







// borderWidthBottom =0borderWidthLeft =0borderWidthRight =0



//关闭文件。

document.Close();



Response.ContentType =application / pdf;

回复.AddHeader(内容 - 性格,附件; filename = SampleExport.pdf);

Response.End();

}

解决方案

请参考以下链接

点击此处

protected void ExportToPDF(GridView gvReport, bool LandScape)
{
int noOfColumns = 0, noOfRows = 0;
DataTable tbl = null;

if (gvReport.AutoGenerateColumns)
{
tbl = gvReport.DataSource as DataTable; // Gets the DataSource of the GridView Control.
noOfColumns = tbl.Columns.Count;
noOfRows = tbl.Rows.Count;
}
else
{
noOfColumns = gvReport.Columns.Count;
noOfRows = gvReport.Rows.Count;
}



float HeaderTextSize = 8;
float ReportNameSize = 10;
float ReportTextSize = 8;
float ApplicationNameSize = 7;

// Creates a PDF document
Document document = null;
if (LandScape == true)
{
// Sets the document to A4 size and rotates it so that the orientation of the page is Landscape.
document = new Document(PageSize.A4.Rotate(), 0, 0, 15, 5);
}
else
{
document = new Document(PageSize.A4, 36, 72, 108, 180);
}

// Creates a PdfPTable with column count of the table equal to no of columns of the gridview or gridview datasource.
iTextSharp.text.pdf.PdfPTable mainTable = new iTextSharp.text.pdf.PdfPTable(noOfColumns);
mainTable.TotalWidth = 500f;
mainTable.LockedWidth = true;
float[] widths = new float[] { 50f, 300f, 100f, 50f };
mainTable.SetWidths(widths);
// Sets the first 4 rows of the table as the header rows which will be repeated in all the pages.
mainTable.HeaderRows = 4;

// Creates a PdfPTable with 2 columns to hold the header in the exported PDF.
iTextSharp.text.pdf.PdfPTable headerTable = new iTextSharp.text.pdf.PdfPTable(2);


// Creates a phrase to hold the application name at the left hand side of the header.
Phrase phApplicationName = new Phrase("Sample Application", FontFactory.GetFont("Arial", ApplicationNameSize, iTextSharp.text.Font.NORMAL));

// Creates a PdfPCell which accepts a phrase as a parameter.
PdfPCell clApplicationName = new PdfPCell(phApplicationName);
// Sets the border of the cell to zero.
clApplicationName.Border = PdfPCell.NO_BORDER;
// Sets the Horizontal Alignment of the PdfPCell to left.
clApplicationName.HorizontalAlignment = Element.ALIGN_LEFT;


// Creates a phrase which holds the file name.
Phrase phHeader = new Phrase("Sample Export", FontFactory.GetFont("Arial", ReportNameSize, iTextSharp.text.Font.BOLD));
PdfPCell clHeader = new PdfPCell(phHeader);
clHeader.Colspan = noOfColumns;
clHeader.Border = PdfPCell.NO_BORDER;
clHeader.HorizontalAlignment = Element.ALIGN_CENTER;
mainTable.AddCell(clHeader);

// Creates a phrase for a new line.
Phrase phSpace = new Phrase("\n");
PdfPCell clSpace = new PdfPCell(phSpace);
clSpace.Border = PdfPCell.NO_BORDER;
clSpace.Colspan = noOfColumns;
mainTable.AddCell(clSpace);
mainTable.DefaultCell.BorderWidth = 0;
mainTable.DefaultCell.BorderWidthBottom = 1;
// Sets the gridview column names as table headers.
for (int i = 0; i < noOfColumns; i++)
{
Phrase ph = null;

if (gvReport.AutoGenerateColumns)
{
ph = new Phrase(tbl.Columns[i].ColumnName, FontFactory.GetFont("Arial", HeaderTextSize, iTextSharp.text.Font.BOLD));
}
else
{
ph = new Phrase(gvReport.Columns[i].HeaderText, FontFactory.GetFont("Arial", HeaderTextSize, iTextSharp.text.Font.BOLD));
}

mainTable.AddCell(ph);
mainTable.DefaultCell.BorderWidth = 0;
mainTable.DefaultCell.BorderWidthBottom = 1;
}

// Reads the gridview rows and adds them to the mainTable
for (int rowNo = 0; rowNo < noOfRows; rowNo++)
{
for (int columnNo = 0; columnNo < noOfColumns; columnNo++)
{
if (gvReport.AutoGenerateColumns)
{
string s = gvReport.Rows[rowNo].Cells[columnNo].Text.Trim();
Phrase ph = new Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL));
mainTable.AddCell(ph);
}
else
{
if (gvReport.Columns[columnNo] is TemplateField)
{
DataBoundLiteralControl lc = gvReport.Rows[rowNo].Cells[columnNo].Controls[0] as DataBoundLiteralControl;
string s = lc.Text.Replace("
", "\r\n");
s = s.Trim();
Phrase ph = new Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL));
mainTable.AddCell(ph);
}
else
{
string s = gvReport.Rows[rowNo].Cells[columnNo].Text.Trim();
Phrase ph = new Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL));
mainTable.AddCell(ph);
}
}
}

// Tells the mainTable to complete the row even if any cell is left incomplete.
mainTable.CompleteRow();
}

// Gets the instance of the document created and writes it to the output stream of the Response object.
PdfWriter.GetInstance(document, Response.OutputStream);

//// Creates a footer for the PDF document.
//HeaderFooter pdfFooter = new HeaderFooter(new Phrase(), true);
//pdfFooter.Alignment = Element.ALIGN_CENTER;
//pdfFooter.Border = iTextSharp.text.Rectangle.NO_BORDER;
//// Sets the document footer to pdfFooter.
//document.Footer = pdfFooter;
// Opens the document.
document.Open();
// Adds the mainTable to the document.
Paragraph heading = new Paragraph("Chart of Accounts", new Font(Font.FontFamily.HELVETICA, 22f, Font.BOLD));
heading.SpacingAfter = 18f;
heading.Alignment = Element.ALIGN_LEFT;
document.Add(heading);
Paragraph para = new Paragraph();
para.Alignment = Element.ALIGN_CENTER;



// Creates a phrase to show the current date at the right hand side of the header.
Phrase phDate = new Phrase(DateTime.Now.Date.ToString("dd/MM/yyyy"), FontFactory.GetFont("Arial", ApplicationNameSize, iTextSharp.text.Font.NORMAL));

// Creates a PdfPCell which accepts the date phrase as a parameter.
PdfPCell clDate = new PdfPCell(phDate);
// Sets the Horizontal Alignment of the PdfPCell to right.
clDate.HorizontalAlignment = Element.ALIGN_RIGHT;
// Sets the border of the cell to zero.
clDate.Border = PdfPCell.NO_BORDER;

// Adds the cell which holds the application name to the headerTable.
headerTable.AddCell(clApplicationName);
// Adds the cell which holds the date to the headerTable.
headerTable.AddCell(clDate);
// Sets the border of the headerTable to zero.
headerTable.DefaultCell.Border = PdfPCell.NO_BORDER;

// Creates a PdfPCell that accepts the headerTable as a parameter and then adds that cell to the main PdfPTable.
PdfPCell cellHeader = new PdfPCell(headerTable);
cellHeader.Border = PdfPCell.NO_BORDER;
// Sets the column span of the header cell to noOfColumns.
cellHeader.Colspan = noOfColumns;
// Adds the above header cell to the table.
mainTable.AddCell(cellHeader);
document.Add(para);
document.Add(mainTable);



// borderWidthBottom="0" borderWidthLeft="0" borderWidthRight="0"

// Closes the document.
document.Close();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename= SampleExport.pdf");
Response.End();
}

解决方案

please refer below link
Click Here


这篇关于如何使用itextsharp页面添加页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆