Generate pdf on the fly and email with webmatrix

2019-08-09 13:06发布

问题:

I have a web application which at the moment generates certificates using a template. Since I original wrote it in php, I used the str_replace built in function to replace values in my template with values from the query.

Now I have changed to asp.net web pages and my aim is to generate certificates in pdf and mail them.

I am using iTextSharp and webmatrix.

Below is part of my code :

 var sql = "SELECT CustomerID, CompanyName, ContactName, Address, City, Country, Phone FROM Customers WHERE CustomerID = 'ALFKI'";
var data = db.Query(sql);

  foreach(var item in data){ var companyname = item.CompanyName;}

PdfPCell certify1 = new PdfPCell(new Phrase("companyname"));
certify1.Colspan = 2;
certify1.Border = 0;
certify1.PaddingTop = 40f;
certify1.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
table.AddCell(certify1);

From this code I am trying to show the data from database table. The code above is not working. I am trying to grab the query values and have then in certify1 cell. How can I do this?

回答1:

Try this:

var sql = "SELECT CustomerID, CompanyName, ContactName, Address, City, Country, Phone FROM Customers WHERE CustomerID = 'ALFKI'";
var data = db.Query(sql);

foreach(var item in data){ 
    var companyname = item.CompanyName;
    PdfPCell certify1 = new PdfPCell(new Phrase(companyname));
    certify1.Colspan = 2;
    certify1.Border = 0;
    certify1.PaddingTop = 40f;
    certify1.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
    table.AddCell(certify1);
}