Aug 16 2011
Hello World Example in iText
iText is a .NET library for creating and manipulating PDF’s. It was originally written in Java but it was also ported to .NET. The book, “iText in Action”, has examples in Java only which will only be useful if a .NET developer knows this language. C# examples can be found at iText.NET site. The code needs to be slightly modified for it to compile with Visual Studio 2010.
The first thing that need to change is the library references. It uses a Java style code which may have been used in an older version of iText for .NET.
using com.lowagie.text; using com.lowagie.text.pdf;
should be written as:
using iTextSharp.text; using iTextSharp.text.pdf;
If you cut and paste the code from the examples to VS, you will see multiple errors. Change getInstance to GetInstance, open to Open, add to Add, close to Close. If your not sure about the function being used, delete it (include the period), type a period and the available functions will be listed.
The code will then compile correction in VS 2010. I recommend purchasing iText in Action because it will save you time in learning how to use iText. A knowledge of Java is beneficial, but not necessary to port the examples to .NET.
The following is a simple example of using iText with C#.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace Chap0101
{
class Program
{
static void Main (string [] args)
{
Console.WriteLine ("Chapter 1 example 1: Hello World");
// step 1: creation of a document-object
Document document = new Document ();
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.GetInstance (document, new FileStream ("Chap0101.pdf", FileMode.Create));
// step 3: we open the document
document.Open ();
// step 4: we add a paragraph to the document
document.Add (new Paragraph ("Hello World"));
// step 5: we close the document
document.Close ();
}
}
}
Jan 11, 2013 @ 09:14:59
I am not a regular developer in C#, that is why i faced the issue compiling. This article simply solved my problem.
Thankyou so much.