Reputation: 138
I want to create a pdf in which i have labels. eg: Name:_, City:__; and i want to fill blanks dynamically as per my studentID using fields.SetField("name", Convert.ToString(dsstudent.Tables[0].Rows[0]["Name"].ToString())); with asp.net c#.
Upvotes: 1
Views: 1363
Reputation: 1206
The best option is iTextSharp, by far the easiest way to render pdf. You can use html templates and dinamically replace values or just render any webcontrol into a html string and save it as pdf.
You can have something like this
string html = "<h1>[PAGE_TITLE]<h1/>[STUDENTS]";
//get your values here
...
html = html.Replace("[PAGE_TITLE]", MyPageTitle);
html = html.Replace("[STUDENTS]", MyStudentsTableHtml);
Then with iTextSharp (Taken from https://web.archive.org/web/20211020001758/https://www.4guysfromrolla.com/articles/030911-1.aspx)
// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(html), null);
foreach (var htmlElement in parsedHtmlElements)
{
document.Add(htmlElement as IElement);
}
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=students{0}.pdf", YourStudendsPrintingId));
Response.BinaryWrite(output.ToArray());
As you can see, with little tweaking from your side, you could create the PDF you need. One thing they don't say, iTextSharp is very picky with the html, it it finds some error on the html or don't like some old tag (say <HR>) it throws a pretty nasty exception that doesn't point to that kind of problem!!
Upvotes: 1