Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am using the bold marked code to generate pdf but I want to save that pdf

C#
public ActionResult ReportSample(tbbooking tbbook)
        {
            var uemail = Convert.ToString(Session["uemail"]);
            var bookingid = tbbook.bookingid;
            var getticket = db.tbbookings.Where(a => a.bookingid == tbbook.bookingid).ToList();
            return new RazorPDF.PdfResult(getticket, "ReportSample");

            //var sb = new System.Text.StringBuilder();
            //TextWriter tw = new System.IO.StringWriter(sb);
            //myResult.View.Render(getticket, tw);
            //var resultCache = sb.ToString();
            //XmlParser parser;
            //using (var reader = GetXmlReader(resultCache))
            //{
            //    while (reader.Read() && reader.NodeType != XmlNodeType.Element)
            //    {
            //        // no-op
            //    }

            //    if (reader.NodeType == XmlNodeType.Element && reader.Name == "itext")
            //        parser = new XmlParser();
            //    else
            //        parser = new HtmlParser();
            //}

            //// Create a document processing context
            //var document = new Document();
            //document.Open();

            //// Associate output with response stream
            //var pdfWriter = PdfWriter.GetInstance(document, viewContext.HttpContext.Response.OutputStream);
            //pdfWriter.CloseStream = false;

            //// Create the pdf file in the directory system
            //var fileStream = new FileStream("~/Content/pdf", FileMode.Create);
            //var pdfWriter2 = PdfWriter.GetInstance(document, fileStream);

            //// this is as close as we can get to being "success" before writing output
            //// so set the content type now
            //viewContext.HttpContext.Response.ContentType = "application/pdf";

            //// parse memory through document into output
            //using (var reader = GetXmlReader(resultCache))
            //{
            //    parser.Go(document, reader);
            //}

            //fileStream.Close();
            //pdfWriter.Close();
            //pdfWriter2.Close();
        }
Posted
Comments
OriginalGriff 26-Mar-15 10:05am    
What have you tried?
Where are you stuck?
What help do you need?
Nadeem_Ahmed786 27-Mar-15 3:52am    
Thanks for the reply Sir,
Actually I downloaded a demo where the commented codes are used, I want to use this code to save my pdf which I :
return new RazorPDF.PdfResult(getticket, "ReportSample");
I am getting only the generated pdf but I want to save that pdf in a folder.
Sergey Alexandrovich Kryukov 26-Mar-15 12:28pm    
So, RazorPDF or iTextSharp? Why not asking Al Nyveldt?
Why showing all that commented-out code?
—SA
Nadeem_Ahmed786 27-Mar-15 3:50am    
Thanks for the reply Sir,
Actually I downloaded a demo where the commented codes are used, I want to use this code to save my pdf which I :
return new RazorPDF.PdfResult(getticket, "ReportSample");
I am getting only the generated pdf but I want to save that pdf in a folder.

1 solution

"Actually I downloaded a demo where the commented codes are used, I want to use this code to save my pdf which I :
return new RazorPDF.PdfResult(getticket, "ReportSample");
I am getting only the generated pdf but I want to save that pdf in a folder."


You can't just "download a demo" and expect it to do exactly what you want, any more than you can walk into a supermarket and buy "some food" and expect it to be a gourmet meal, hot and ready to eat.

When you get code form the internet, you have to look at it, read it, understand it, and then (and only then) start changing it to fit your task. You will not find code that solves your problem without some changes to it, or your code to support it.

And if you do download code and just "throw it into your app" then you are being extremely silly: what does the code do? Does it do what it says it does, or does it encrypt your HDD and demand money to encrypt it as some trojan software does? Does it find your credit card details and send it to the author? Unless you read and understand the code, you will not know - until it is too late.

So stop "finding code" and "adding it to your app" and start thinking: "How does this work?" because when you have that, it will:
1) Be faster next time
2) Work better when you have to change your app
3) Possibly save you from a lot of grief.
4) In this case, save you a lot of time: that is trivial code which shouldn't take you more than half an hour to understand if you only have little experience. You could have solved this for yourself by now...
 
Share this answer
 
Comments
Nadeem_Ahmed786 27-Mar-15 4:49am    
Yes Sir I know that I added that commented code just to post question I am not using that, I just wanted to get answer from seniors that how to use that similar code in my project
OriginalGriff 27-Mar-15 5:29am    
So what have *you* actually tried?

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