Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I need to generate an invoice & print it as a pdf file.

public FileStreamResult PrintPdf(int id)
        {


            // Set up the document and the MS to write it to and create the PDF writer instance
            var ms = new MemoryStream();
            var document = new Document(PageSize.A4, 0, 0, 0, 0);
            PdfWriter writer = PdfWriter.GetInstance(document, ms);
            //var obj = new simphiwe();
            var obj = new DataContext();

            // Open the PDF document
            document.Open();

            // Set up fonts used in the document
            Font font_heading_3 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD, BaseColor.RED);
            Font font_body = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 9, BaseColor.BLUE);

            // Create the heading paragraph with the headig font
            Paragraph paragraph;
            paragraph = new Paragraph("Fuze Events Managers", font_heading_3);
            Paragraph paraslip;
            paraslip = new Paragraph("**********************Invoice Details***********************");

            // Add image to pdf    
            // Create the heading paragraph with the headig font
            var info = from x in obj.Payments
                       where x.PayId == (id)
                       select x;

            foreach (var q in info)
            {
                //paragraph;
                // Add a horizontal line below the headig text and add it to the paragraph
                iTextSharp.text.pdf.draw.VerticalPositionMark seperator = new iTextSharp.text.pdf.draw.LineSeparator();
                seperator.Offset = -6f;

                var table1 = new PdfPTable(1);
                var table = new PdfPTable(1);
                var table3 = new PdfPTable(1);
                var table7 = new PdfPTable(1);

                table.WidthPercentage = 80;
                table3.SetWidths(new float[] { 100 });
                table3.WidthPercentage = 80;
                table7.SetWidths(new float[] { 100 });
                table7.WidthPercentage = 80;
                var cell = new PdfPCell(new Phrase(""));
                cell.Colspan = 3;
                table1.AddCell(cell);
                table7.AddCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + "Fuze Events Managers" + "\n\n" +
                  "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + "**********************Fuze Events Managerst***********************");


                table.AddCell("Payment Id: " + q.PayId);
                table.AddCell("Name   : " + q.ClientName);
                table.AddCell("Payment Type : " + q.PaymentType);
                table.AddCell("Event Type         : " + q.EventType);
                table.AddCell("Eevnt Start Date: " + q.StartDate);
                table.AddCell("End Date:       :" + q.EndDate);
                table.AddCell("Amount Deposited : " + q.AmountDeposited);
                table.AddCell("Balance    : " + q.AmountOwing);
                


                table.AddCell("Date Issued : " + DateTime.Today.Day + "/" + DateTime.Today.Month + "/" + DateTime.Today.Year);
                table.AddCell(cell);
                document.Add(table3);
                document.Add(table7);
                document.Add(table1);
                document.Add(table);
                document.Close();
            }
            byte[] file = ms.ToArray();
            var output = new MemoryStream();
            output.Write(file, 0, file.Length);
            output.Position = 0;
            var f = new FileStreamResult(output, "application/pdf");
            return f; //File(output, "application/pdf"); //new FileStreamResult(output, "application/pdf");
        }
Posted
Comments
ZurdoDev 28-Jul-14 14:18pm    
What's your question?
Pikoh 29-Jul-14 6:30am    
Not really a solution,but an advice. Instead of generating the pdf that way, i'd rather create a report, fill it with the invoice data,and then generate the pdf.

1 solution

You Forgot To Reference, try using the following:

C#
using iTextSharp.text;
using iTextSharp.text.pdf
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900