Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to generate pdf file using C#. Please help me out.

What I have tried:

i have no idea of solution. Please give me relevant answer to solve the issue.
Posted
Updated 8-Jan-20 17:48pm

try this method for generate pdf using c#.
first Install-Package iTextSharp after used this method.
protected void GeneratePDF(object sender, System.EventArgs e)
     {
         using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
         {
             Document document = new Document(PageSize.A4, 10, 10, 10, 10);

             PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
             document.Open();

             Chunk chunk = new Chunk("This is from chunk. ");
             document.Add(chunk);

             Phrase phrase = new Phrase("This is from Phrase.");
             document.Add(phrase);

             Paragraph para = new Paragraph("This is from paragraph.");
             document.Add(para);

             string text = @"you are successfully created PDF file.";
             Paragraph paragraph = new Paragraph();
             paragraph.SpacingBefore = 10;
             paragraph.SpacingAfter = 10;
             paragraph.Alignment = Element.ALIGN_LEFT;
             paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN);
             paragraph.Add(text);
             document.Add(paragraph);

             document.Close();
             byte[] bytes = memoryStream.ToArray();
             memoryStream.Close();
             Response.Clear();
             Response.ContentType = "application/pdf";

             string pdfName = "User";
             Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf");
             Response.ContentType = "application/pdf";
             Response.Buffer = true;
             Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
             Response.BinaryWrite(bytes);
             Response.End();
             Response.Close();
         }
     }
 
Share this answer
 
v2
There are two ways you could do this. The first is to study the PDF specification and use that to build the documents that way. That, however, is a silly way to do this.

The second way is to use a component that writes the PDF for you, based on the calls you make. There are a number of commercial or free PDF generation libraries available to you, each having their own strengths and weaknesses. You might want to look at something like PDFSharp[^] or the ever popular iTextSharp[^].
 
Share this answer
 
I have a good experience with PDFsharp and MigraDoc[^].

It's free and there are number of examples on their site and on the internet.

Good luck!
 
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