Resources » Code Snippets » Visual Studio

Create PDF document using C#


Posted Date: 29-Jul-2008  Last Updated:   Category: Visual Studio    
Author: Member Level: Gold    Points: 1



This resource shows how to create a PDF document using C#.

Step 1: First create an instance of document object

Document myDocument= new Document(PageSize.A4.Rotate());

Step 2: Now create a writer that listens to this doucment and writes the document to desired Stream.

PdfWriter.GetInstance(myDocument, new FileStream("Salman.pdf", FileMode.Create));

Step 3: Open the document now using

myDocument.Open();

Step 4: Now add some contents to the document

myDocument.add( new Paragraph ( "First Pdf File made by Salman using iText"));

Step 5: Remember to close the documnet

myDocument.close();




// Code

using System;
using System.IO;
using System.Diagnostics;

using iTextSharp.text;
using iTextSharp.text.pdf;

public class iTextDemo
{
public static void Main()
{
Console.WriteLine("iText Demo");

// step 1: creation of a document-object
Document myDocument = new Document(PageSize.A4.Rotate());

try
{

// step 2:
// Now create a writer that listens to this doucment and writes the document to desired Stream.

PdfWriter.GetInstance(myDocument, new FileStream("Salman.pdf", FileMode.Create));

// step 3: Open the document now using
myDocument.Open();

// step 4: Now add some contents to the document
myDocument.Add(new Paragraph("First Pdf File made by Salman using iText"));

}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}

// step 5: Remember to close the documnet

myDocument.Close();
}
}


Did you like this resource? Share it with your friends and show your love!

Responses to "Create PDF document using C#"
Author: Gaurav Kumar Arora    09 Aug 2008Member Level: Gold   Points : 2
Hi Divya,
This is a good one shot using I/O Text but it works only when you have PDF utility installed on your system.

What about if one hasn't installed the utility on his/her system.

I have gone through the same the Code is quite interesting and bug free but it has some miscrepancies or say dependencies.

Thanks & regards,
Gaurav Arora



Author: Gaurav Kumar Arora    09 Aug 2008Member Level: Gold   Points : 2
Hi Divya,
This is a good one shot using I/O Text but it works only when you have PDF utility installed on your system.

What about if one hasn't installed the utility on his/her system.

I have gone through the same the Code is quite interesting and bug free but it has some miscrepancies or say dependencies.

Thanks & regards,
Gaurav Arora



Author: Himasagar Kutikuppala    12 Aug 2008Member Level: Silver   Points : 2
Hi Gaurav
Yes, iTextSharp DLL required to create the PDF file.
This is open source library only, so you can download from the following link and enjoy..
http://downloads.sourceforge.net/itextsharp/itextsharp-4.0.2-dll.zip?modtime=1173357588&big_mirror=0

For more details about iTextSharp & PDF file creation, Go through the following link
http://www.codeproject.com/KB/graphics/iTextSharpTutorial.aspx

Regards
Himasagar Kutikuppala



Author: Jaiswar Vipin Kumar R.    28 Aug 2008Member Level: Bronze   Points : 2
This application is really grate but as you said the it is open source. But there no provision to download the source code.
Because this open source (as you said) DLL may breaking security of the i.e. if I run application by using this DLL, so may be this DLL sending use information via mail internally to unwanted users.

May I am wring, Hoping I am wrong on this discussion, but if it is really true then where is source code?

Thanks & Regards
Jaiswar Vipin Kumar R.



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    hothorasman
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India