Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public void pdf()
{
MemoryStream workStream = new MemoryStream();
Document document = new Document();
PdfWriter.GetInstance(document, workStream).CloseStream = false;

document.Open();

document.Add(new Paragraph("msg"));
document.Add(new Paragraph(DateTime.Now.ToString()));
document.Close();

byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
Response.Buffer = true;

Response.AddHeader("Content-Disposition", "attachment; filename= " + Server.HtmlEncode("doc.pdf"));
Response.ContentType = "APPLICATION/pdf";
Response.BinaryWrite(byteInfo);
//return new FileStreamResult(workStream, "application/pdf");
}

What I have tried:

when i try this...its download pdf and show in that pdf "msg".... but i want to show
employee table in that pdf.... how can i do that?
Posted
Updated 5-Aug-16 4:14am
Comments
F-ES Sitecore 7-Jul-16 3:57am    
Consult the documentation for whatever pdf writer you are using. How can anyone help if you're using third party software but don't say what that software is? Also the title of the thread is misleading, it is help with building the PDF you need, not downloading it.

1 solution

Hi,
  Check this code, It will work according to your requirement.
The "PdfPTable" object is instantiated as a five column table then use "AddCell" to add header and values.

C#
public void pdf()
        {
 
            Employees emp = new Employees();

            MemoryStream workStream = new MemoryStream();
            Document document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            document.Open();

            document.Add(new Paragraph("msg"));
            document.Add(new Paragraph(DateTime.Now.ToString()));

            
            
            PdfPTable table = new PdfPTable(5); 
            // To show data in table.
            // "5" total number of columns.


            // Here I have set width of every columns.
            float[] widths = new float[] { 100f, 80f, 100f, 90f, 130f };
            table.SetWidths(widths);

            // Name of the header for each column.
            table.AddCell("Employee Name");
            table.AddCell("Experience in Months");
            table.AddCell("Band");
            table.AddCell("Designation");
            table.AddCell("Current Project");

            // Adding value in the table.
            // If you are getting list values the use FOREACH loop to show all data in                                                     table  
            table.AddCell(emp.Name);
            table.AddCell(emp.Experience.ToString());
            table.AddCell(emp.Band);
            table.AddCell(emp.Designation);
            table.AddCell(emp.Project);
            
            // Here I am adding table to the document.
            document.Add(table);
            document.Close();

            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;
            Response.Buffer = true;

            Response.AddHeader("Content-Disposition", "attachment; filename= " + Server.HtmlEncode("doc.pdf"));
            Response.ContentType = "APPLICATION/pdf";
            Response.BinaryWrite(byteInfo);
        }


This is the class where I am retrieving data from database. Here I have done hard coding if you want you can get data from database using Sqlconnection and all.

C#
public class Employees
    {
        // Here I have done hard coding if you want, you can retrieve data from database. 
        public string Name = "Smith";
        public int Experience = 0;
        public string Band = "B4";
        public string Designation = "Software Engineer";
        public string Project = "XYZ";
    }
 
Share this answer
 
v3
Comments
Prateek Dalbehera 5-Aug-16 14:06pm    
Simple & straight forward solution...
RAVI RANJAN OJHA 28-Nov-16 1:53am    
Thank you Prateek for feedback.

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