Windows Forms: How to Create a PDF document file in C#

How to Create a PDF document file using iTextSharp in C#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "MakePdf" and then click OK

c# pdfStep 2: Right click on your project select Manage NuGet Packages -> Search itextsharp -> Install

c# itextsharpiText is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documents in the Portable Document Format (PDF), allowing you to add PDF functionality to your software projects with ease.  We even have documentation to help you get coding.

Step 3: Design your form as below

c# create pdf file

Step 4: Add code to button click event handler as below

private void btnSave_Click(object sender, EventArgs e)
{
    using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "PDF file|*.pdf", ValidateNames = true })
    {
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4.Rotate());
            try
            {
                //Save pdf file
                PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
                doc.Open();
                doc.Add(new iTextSharp.text.Paragraph(richTextBox.Text));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                doc.Close();
            }
        }
    }
}

VIDEO TUTORIALS