Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello EveryOne,


I am new to MVC, I am doing a Module, where I have a object which contains data, I want that data to be sent to PDF file, basically I want to get the object value into PDF file.

Here is my ActionResult method code :



C#
public ActionResult Print(int id)
       {
            ListOfPatientsModel _Lst = db.Patients.Find(id);
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
           pdfDoc.Add(new Paragraph("Bill Number :" + _Lst.BillNo));
           pdfDoc.Add(new Paragraph("Patient Name:" + _Lst.PatientName));
           pdfDoc.Add(new Paragraph("MRN Number:" + _Lst.MRN_No));
           pdfDoc.Add(new Paragraph("Status:" + _Lst.Status));
           pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;" +"filename=sample.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
          return View();
       }

here the Object "_Lst" holds the data which I needed, I want that data to be stored in "pdfDoc" so that I can view it in PDF file.


Note:
->As if now I am able to achieve what i needed, by those 4 lines(bolded), but I want to know is there any other way rather than this.
->"ListOfPatientsModel" is Model Class file.

Regards
Manjuboyz
Posted
Updated 10-Feb-14 19:59pm
v2
Comments
LLLLGGGG 11-Feb-14 3:05am    
What about using XML serialization and save results into the PDF file?
You can write an overload of ToString() with the fields you're interested in, then, in your main method, split it into an array (use \n character for example) and foreach element in the array, add a new paragraph.
Manjuboyz 11-Feb-14 5:19am    
Hey jymmy097

Thanks for your reply,I got what you are saying,have to use "serialization" method and get all the values from object then bind that object into xmlserializer show it on Array, then load them into xmldocument? rite, if so
That makes No difference as above code I believe?, what I am expecting is can I send that object value to "View" then there I can arrange nice Layout for displaying the contents or Can I do the same in Controller? or do I need to use Crystal Report for design.

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