- using iTextSharp.text
- using iTextSharp.text.pdf;
- ..
- /// <summary>
- /// Print a table to a Pdf file in tabular format,
- /// with header image and text on every page
- /// </summary>
- /// <param name="PdfFileName">This is pyhsical file name
- /// of Pdf document that you wanto write to</param>
- /// <param name="dt">Datatable that contain the data to print</param>
- /// <param name="DocTitle">This is the title of the
- /// document to be printed once on first page</param>
- /// <param name="PageHeader">Header text to go
- /// with header image on every page</param>
- /// <param name="HeaderImgPath">the physical file path of a image
- /// that you want to print out on every page header</param>
- /// <returns></returns>
- public static bool ExportPdfTable(string PdfFileName, DataTable dt,
- string DocTitle,string PageHeader,string HeaderImgPath)
- {
- Document doc = new Document(); //iTextSharp document
- try
- {
- string physicalFile = PdfFileName;
- PdfWriter pw=PdfWriter.GetInstance(doc,
- new FileStream(physicalFile, FileMode.Create));
- //prepare for inserting header on every page, using Pageevent
- //DocumentEevent e = new DocumentEevent(HeaderImgPath, PageHeader);
- //pw.PageEvent = e;
- doc.Open();
- if (PageHeader.Length > 0)
- doc.Add(new Paragraph(new Phrase("")));
- if (DocTitle.Length > 0)
- doc.Add(new Phrase(DocTitle));
- if (dt.Columns.Count == 0) return false;
- int cols = dt.Columns.Count;
- int rows = dt.Rows.Count;
- //prepare the table object
- PdfPTable t = new PdfPTable(cols);
- t.WidthPercentage = 100;
- //cell object
- PdfPCell c;
- //Use BaseFont to load unicode fonts like Simplified Chinese font
- string fontpath = System.web.HttpContext.Current.Request.PhysicalApplicationPath +
- "\\\\includes\\\\fonts\\\\simsun.ttf";
- //"simsun.ttf" file was downloaded from web and placed in the folder
- BaseFont bf = BaseFont.CreateFont(fontpath,BaseFont.IDENTITY_H,
- BaseFont.EMBEDDED);
- //create new font based on BaseFont
- Font fontContent = new Font(bf, 11);
- Font fontHeader = new Font(bf, 12);
- //write header
- for (int j = 0; j < cols; j++)
- {
- Phrase pr=new Phrase((dt.Columns[j].Caption != null &&
- dt.Columns[j].Caption.Length > 0) ? dt.Columns[j].Caption :
- dt.Columns[j].ColumnName,fontHeader);
- c = new PdfPCell(pr);
- c.PaddingBottom = 5f;
- c.PaddingTop = 5f;
- c.PaddingLeft = 8f;
- c.PaddingRight = 8f;
- t.AddCell(c);
- }
- //write table content
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- //c = new Cell(dt.Rows[i][j].ToString());
- //c.Header = false;
- c = new PdfPCell(new Phrase(dt.Rows[i][j].ToString(),fontContent));
- c.PaddingBottom = 5f;
- c.PaddingTop = 5f;
- c.PaddingLeft = 8f;
- c.PaddingRight = 8f;
- t.AddCell(c);
- }
- }
- return doc.Add(t);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- return false;
- }
- finally
- {
- doc.Close();
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/170720134652.html
来源: http://www.codesnippet.cn/detail/170720134652.html