使用iTextSharp的打印与多个表的PDF [英] Using iTextSharp to print a PDF with multiple tables
本文介绍了使用iTextSharp的打印与多个表的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个详细的账单打印,拉了来自不同部门的帐单明细。我需要他们能够在一个PDF打印出来。但我用大量的选择程序,因为我有,所以我需要code,我可以重复使用,显示了很多表。
I have a detailed bill to print that pulls up billing details from various departments. I need them to be printed on a PDF. But I use plenty of select procedures as I have to display a lot of tables so I need code that I can reuse.
推荐答案
我使用的命名空间:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
的打印
按钮单击事件生成一个PDF:
The Print
button click event that generates a PDF:
protected void btnPrintToPDF_Click(object sender, EventArgs e)
{
//Default Settings for your PDF File
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.pdf", "PDFExport"));
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Initialize a StringSwriter
StringWriter stringWrite = new StringWriter();
DataTable dt = new DataTable();
//Info line in PDF
stringWrite.WriteLine("Clothing department");
//Fetch DataTable
dt = getDataTablebyProcedure("SelectFromClothingDepartment");
//Append it to String Writer
stringWrite = getStringWriterbyDataTable(dt, stringWrite);
//Info line in PDF
stringWrite.WriteLine("Food Department");
//Fetch DataTable
dt = getDataTablebyProcedure("SelectFromFoodDepartment");
//Append it to Stwing Writer
stringWrite = (getStringWriterbyDataTable(dt, stringWrite));
//As many procedures as you want to go here
//Finally Write the writer into a reader
StringReader sr = new StringReader(stringWrite.ToString());
//Generate the pdf
Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 10f, 0f);
pdfDoc.SetPageSize(PageSize.A4.Rotate());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
}
过程返回你的选择
程序的数据表
protected DataTable getDataTablebyProcedure(string ProcedureName)
{
SqlDataAdapter da= new SqlDataAdapter();
DataTable dt=new DataTable();
try
{//open a connection to your DB
da.Fill(dt);
}
catch (Exception ex)
{//handle Exception
}
finally
{//Close Connection
}
return dt;
}
这追加到数据表功能的的StringWriter
Function that appends datatables to your StringWriter
protected static StringWriter getStringWriterbyDataTable(DataTable dt, StringWriter stringWrite1 )
{
//System.IO.StringWriter stringWrite1 = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWrite1);
DataGrid myDataGrid1 = new DataGrid();
myDataGrid1.DataSource = dt;
myDataGrid1.DataBind();
myDataGrid1.RenderControl(htmlWrite1);
return stringWrite1;
}
那么,这个工作真的很适合我,所以我希望它可以帮助别人一天。
Well, this worked really well for me, so I'm hoping it helps someone someday.
这篇关于使用iTextSharp的打印与多个表的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文