Tuesday, June 2, 2009

Create PDF Files in C#.Net

Creating PDF files in C# is very simple. But you should download the library called “itextsharp.dll” and add a reference to your project . You can download “itextsharp.dll” by clicking following link

Download itextsharp.dll

Following Code is a sample to use it

Document myDocument = new Document(PageSize.A4.Rotate());
SavePDFDialog.ShowDialog();
if (SavePDFDialog.FileName != "")
{
try
{
PdfWriter.GetInstance(myDocument, new FileStream(SavePDFDialog.FileName, FileMode.Create));
myDocument.Open();
myDocument.Add(new Paragraph(txtInput.Text));
myDocument.AddAuthor("Tharindu Sanjeewa Rathnathunga");
myDocument.AddTitle("tsr.edu@live.com");
}
catch (DocumentException de)
{
MessageBox.Show(de.Message);
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
}
finally
{
myDocument.Close();
}
}

NOTE: Your project should

  • Have a SaveFileDialog called SavePDFDialog and TextBox called txtInput
  • You should use following using statements
  • using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;

    To download the source project click here.

No comments:

Post a Comment