Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7080765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:50:07+00:00 2026-05-28T06:50:07+00:00

Requirment: To generate invoice in pdf format on company template and send it in

  • 0

Requirment: To generate invoice in pdf format on company template and send it in email.

Approach I used:

  1. Placed the company template at path: ~Content/InvoiceTemplate/
  2. Using iTextsharp Pdf stamper, generated pdf, saved it at path: ~/Content/reports/
  3. In email module, picked the file generated above and attached to email to be sent

Problem: Every invoice generated is being stored on application server, making application heavier day by day.

Question: What is the other way out to send the generated in voice in email, without saving it on application server?

Code:

    public static void WriteInTemplate(List<Models.Statement> statementList)
    {
        try
        {
            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)
                    {
                        do
                        {
                            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 up
                                        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 tax = Convert.ToDouble(statementList.FirstOrDefault().Tax);
                                        pdfFields.SetField("Tax", statementList.FirstOrDefault().Tax.ToString("0.00").Trim());

                                    }
                                    pdfStamper.FormFlattening = true; // generate a flat PDF 

                                }
                                pdfReader = new PdfReader(memoryStream.ToArray());
                                smartCopy.AddPage(smartCopy.GetImportedPage(pdfReader, 1));

                            }
                            remainingItems = remainingItems - maxItemsPerPage;

                        } while (remainingItems > 0);
                    }
                }
            }

            emailController.CreateMessageWithAttachment(invoiceNumber);
        }
        catch (Exception e)
        {
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:50:07+00:00Added an answer on May 28, 2026 at 6:50 am

    You can try to attach the file from a memory stream. You can search Google for “C# Attach file from memory stream”.

    Here is a sample snippet:

     mail.Attachments.Add(new Attachment(memoryStream, "example.txt", "text/plain"));
    

    Or:

    email attachment from the 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement to auto generate a poster in PDF format based on
I want to save a screenshot or generate a pdf from UIWebView text content.
I have a requirement to dynamically generate and compress large batches of PDF files.
I have very strange requirement. I have to generate notification (email) when user installs
I have a requirement to generate an email to administrator whenever a user sign
I used Google chart api to generate the chart using webview in android. Now
I am working on a webapplication and i have a requirment to generate log
I have a particular requirement to generate a PDF document with some dynamic data
My requirement is to generate PDF file using iText, I use below code to
I am a beginner in MVC and have a requirement to generate PDF of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.