|
|
|
เอา Code ASP.Net มาฝาก export pdf ไทย + page number ด้วย iTextSharp |
|
|
|
|
|
|
|
เป็นการทำ export pdf ไทย + page number
- 1. โหลด lib ของ iTextSharp
- 2. โหลดฟ้อนสารบัญ จาก sipa
ผิดพลาดประการใดขอโทดด้วยคาบ
code ทั้งหมดนี้อยู่ใน page(aspx.cs) เดียวกันหมดเลยนะ มีทั้งหมด 3 class
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;
public partial class pm_Default2 : System.Web.UI.Page
{
protected void btnExportPdf_Click(object sender, EventArgs e)
{
//set font ในกรณีที่เป็นภาษาไทนแนะนำให้ใช้ เป็น font Sarabun ของ sipa
BaseFont bf = BaseFont.CreateFont(Server.MapPath("~/Fonts/THSarabunNew.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font defaultFont = new Font(bf, 12);
Font header = new Font(bf, 14, Font.BOLD);
Font headTB = new Font(bf, 12, Font.BOLD);
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=Report.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//this.Page.RenderControl(hw); แปลงข้อมูลทั้งหน้าให้เป็น pdf
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 25f, 25f, 25f, 15f); // left , right , top , bottom
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
//PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
// calling PDFFooter class to Include in document
writer.PageEvent = new PDFFooter();
// เอาข้อมูลใน GridView1 มาใส่ใน PdfTable
PdfPTable PdfTable = new PdfPTable(GridView1.Columns.Count);
PdfPCell PdfPCell = null;
// Write Header ที่ PdfCell ใน PdfTable
for (int column = 0; column < GridView1.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(GridView1.Columns[column].HeaderText.ToString(), headTB)));
PdfPCell.Column.Alignment = Element.ALIGN_CENTER;
PdfTable.AddCell(PdfPCell);
}
// Write Data ที่ PdfCell ใน PdfTable
for (int row = 0; row < GridView1.Rows.Count; row++)
{
for (int col = 0; col < GridView1.Columns.Count; col++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(GridView1.Rows[row].Cells[col].Text, defaultFont)));
PdfTable.AddCell(PdfPCell);
}
}
PdfTable.TotalWidth = 530f; // ความยาวของตาราง
PdfTable.LockedWidth = true;
pdfDoc.Open();
Paragraph head1 = new Paragraph(lblHeader.Text, header); //สร้างหัวข้อใหม่
head1.Alignment = Element.ALIGN_CENTER; // เซ็ทให้อยู่ตรงกลาง
pdfDoc.Add(head1); //เพิ่มลงใน pdf
pdfDoc.Add(PdfTable); //เพิ่ม table ที่เอามาจาก gridview ลงใน pdf
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
}
// ******** ส่วน 2 class ด้านล่างเป็นการทำ page number , header , footer (ถ้าไม่อยากแก้อะไรมาแค่ copy วาง จบ) **********//
public class PDFFooter : PdfPageEventHelper
{
// หัวข้อเฉพาะหน้าแรก
public override void OnOpenDocument(PdfWriter writer, Document document)
{
base.OnOpenDocument(writer, document);
/*
PdfPTable tabFot = new PdfPTable(new float[] { 1F });
tabFot.SpacingAfter = 10F;
PdfPCell cell;
tabFot.TotalWidth = 300F;
int pageN = writer.PageNumber;
String text = "Page " + pageN + " of ";
cell = new PdfPCell(new Phrase(text));
cell.Border = 0;
cell.HorizontalAlignment = Element.ALIGN_RIGHT;
tabFot.DefaultCell.Border = 0;
tabFot.AddCell(cell);
tabFot.WriteSelectedRows(0, -1, 150, document.Top, writer.DirectContent);
*/
}
// write on start of each page
public override void OnStartPage(PdfWriter writer, Document document)
{
base.OnStartPage(writer, document);
}
// write on end of each page
public override void OnEndPage(PdfWriter writer, Document document)
{
/*---- set font ----*/
BaseFont bf = BaseFont.CreateFont(
BaseFont.TIMES_ROMAN,
BaseFont.CP1252,
BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
/*---- set page number ----*/
base.OnEndPage(writer, document);
PdfPTable tabFot = new PdfPTable(new float[] { 1F });
tabFot.TotalWidth = 300F;
int pageN = writer.PageNumber;
String text = "Page : " + pageN ;
PdfPCell cell = new PdfPCell(new Phrase(text, font));
cell.Border = 0;
cell.HorizontalAlignment = Element.ALIGN_RIGHT;
Rectangle pageSize = document.PageSize;
tabFot.DefaultCell.Border = 0;
tabFot.AddCell(cell);
document.SetMargins(25f, 25f, 35f, 15f);
tabFot.WriteSelectedRows(0, -1, pageSize.GetLeft(280), pageSize.GetTop(10), writer.DirectContent);
/*---- set footer----*/
}
//write on close of document
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
}
}
public class PageEventHelper : PdfPageEventHelper
{
public class TwoColumnHeaderFooter : PdfPageEventHelper
{
// This is the contentbyte object of the writer
PdfContentByte cb;
// we will put the final number of pages in a template
PdfTemplate template;
// this is the BaseFont we are going to use for the header / footer
BaseFont bf = null;
// This keeps track of the creation time
DateTime PrintTime = DateTime.Now;
#region Properties
private string _Title;
public string Title
{
get { return _Title; }
set { _Title = value; }
}
private string _HeaderLeft;
public string HeaderLeft
{
get { return _HeaderLeft; }
set { _HeaderLeft = value; }
}
private string _HeaderRight;
public string HeaderRight
{
get { return _HeaderRight; }
set { _HeaderRight = value; }
}
private Font _HeaderFont;
public Font HeaderFont
{
get { return _HeaderFont; }
set { _HeaderFont = value; }
}
private Font _FooterFont;
public Font FooterFont
{
get { return _FooterFont; }
set { _FooterFont = value; }
}
#endregion
// we override the onOpenDocument method
public override void OnOpenDocument(PdfWriter writer, Document document)
{
try
{
PrintTime = DateTime.Now;
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb = writer.DirectContent;
template = cb.CreateTemplate(50, 50);
}
catch (DocumentException de)
{
}
catch (System.IO.IOException ioe)
{
}
}
public override void OnStartPage(PdfWriter writer, Document document)
{
base.OnStartPage(writer, document);
Rectangle pageSize = document.PageSize;
if (Title != string.Empty)
{
cb.BeginText();
cb.SetFontAndSize(bf, 15);
cb.SetRGBColorFill(50, 50, 200);
cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40));
cb.ShowText(Title);
cb.EndText();
}
if (HeaderLeft + HeaderRight != string.Empty)
{
PdfPTable HeaderTable = new PdfPTable(2);
HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
HeaderTable.TotalWidth = pageSize.Width - 80;
HeaderTable.SetWidthPercentage(new float[] { 45, 45 }, pageSize);
PdfPCell HeaderLeftCell = new PdfPCell(new Phrase(8, HeaderLeft, HeaderFont));
HeaderLeftCell.Padding = 5;
HeaderLeftCell.PaddingBottom = 8;
HeaderLeftCell.BorderWidthRight = 0;
HeaderTable.AddCell(HeaderLeftCell);
PdfPCell HeaderRightCell = new PdfPCell(new Phrase(8, HeaderRight, HeaderFont));
HeaderRightCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
HeaderRightCell.Padding = 5;
HeaderRightCell.PaddingBottom = 8;
HeaderRightCell.BorderWidthLeft = 0;
HeaderTable.AddCell(HeaderRightCell);
cb.SetRGBColorFill(0, 0, 0);
HeaderTable.WriteSelectedRows(0, -1, pageSize.GetLeft(40), pageSize.GetTop(50), cb);
}
}
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
int pageN = writer.PageNumber;
String text = "Page " + pageN + " of ";
float len = bf.GetWidthPoint(text, 8);
Rectangle pageSize = document.PageSize;
cb.SetRGBColorFill(100, 100, 100);
cb.BeginText();
cb.SetFontAndSize(bf, 8);
cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
cb.ShowText(text);
cb.EndText();
cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));
cb.BeginText();
cb.SetFontAndSize(bf, 8);
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
"Printed On " + PrintTime.ToString(),
pageSize.GetRight(40),
pageSize.GetBottom(30), 0);
cb.EndText();
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
template.BeginText();
template.SetFontAndSize(bf, 8);
template.SetTextMatrix(0, 0);
template.ShowText("" + (writer.PageNumber - 1));
template.EndText();
}
}
}
Tag : ASP.NET Web (ASP.NET), C#
|
ประวัติการแก้ไข 2014-02-07 17:59:06
|
|
|
|
|
Date :
2014-02-07 17:57:07 |
By :
thewinner55 |
View :
5968 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จัดไปครับ มีภาพประกอบด้วยจะดีมาก
|
|
|
|
|
Date :
2014-02-08 07:18:06 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อันนี้คือดึงข้อมูลทั้งหน้ามา export เป็น pdf เลย ไม่ได้ผ่าน crystal report ใช่ไหมครับ?
|
|
|
|
|
Date :
2014-02-10 09:16:42 |
By :
Mr.T-Bag |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช่ ครับ
|
|
|
|
|
Date :
2014-02-12 22:29:31 |
By :
thewinner55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|