c# - send pdf statement without saving on application server -



c# - send pdf statement without saving on application server -

requirment: generate invoice in pdf format on company template , send in email.

approach used:

placed company template @ path: ~content/invoicetemplate/ using itextsharp pdf stamper, generated pdf, saved @ path: ~/content/reports/ in email module, picked file generated above , attached email sent

problem: every invoice generated beingness stored on application server, making application heavier day day.

question: other way out send generated in voice in email, without saving on application server?

code:

public static void writeintemplate(list<models.statement> statementlist) { seek { string invoicenumber = statementlist.firstordefault().invoice.tostring().trim(); using (document document = new document()) { filestream filestream = new filestream(hostingenvironment.mappath("~/content/reports/" + invoicenumber + ".pdf"), filemode.create); using (pdfsmartcopy smartcopy = new pdfsmartcopy(document, filestream)) { document.open(); int statementcounter = 0; int numberofitems = statementlist.count(); int remainingitems = numberofitems; int maxitemsperpage = 17; if (remainingitems > 0) { { if (remainingitems < maxitemsperpage) maxitemsperpage = remainingitems; pdfreader pdfreader = new pdfreader(hostingenvironment.mappath("~/content/invoicetemplate/invoicetemplate.pdf")); using (var memorystream = new memorystream()) { using (pdfstamper pdfstamper = new pdfstamper(pdfreader, memorystream)) { string month = null; string day = null; string year = null; acrofields pdffields = pdfstamper.acrofields; {//billing address pdffields.setfield("billtocompany", statementlist.firstordefault().billtocompany.tostring().trim().toupper()); pdffields.setfield("billtocontact", statementlist.firstordefault().billtocontact.tostring().trim().toupper()); } //---------------------snip------------------------------// //---------------------snip------------------------------// } {//invoice sum double subtotal = convert.todouble(statementlist.firstordefault().subtotal); pdffields.setfield("subtotal", statementlist.firstordefault().subtotal.tostring("0.00").trim()); double misc = convert.todouble(statementlist.firstordefault().misc); pdffields.setfield("misc", statementlist.firstordefault().misc.tostring("0.00").trim()); double taxation = convert.todouble(statementlist.firstordefault().tax); pdffields.setfield("tax", statementlist.firstordefault().tax.tostring("0.00").trim()); } pdfstamper.formflattening = true; // generate flat pdf } pdfreader = new pdfreader(memorystream.toarray()); smartcopy.addpage(smartcopy.getimportedpage(pdfreader, 1)); } remainingitems = remainingitems - maxitemsperpage; } while (remainingitems > 0); } } } emailcontroller.createmessagewithattachment(invoicenumber); } grab (exception e) { } }

you can seek attach file memory stream. can search google "c# attach file memory stream".

here sample snippet:

mail.attachments.add(new attachment(memorystream, "example.txt", "text/plain"));

or:

email attachment memorystream comes empty

http://social.msdn.microsoft.com/forums/en-us/netfxbcl/thread/049420de-7e93-4fcb-9920-0c1cdf4ca420/

http://www.codeproject.com/kb/ip/inmemorymailattachment.aspx

c# .net asp.net-mvc pdf-generation asp.net-4.0

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -