RubyPDF Blog .NET,English,iText in Action,iTextSharp(iText#),PDF HelloWorld–C# version examples of iText in Action

HelloWorld–C# version examples of iText in Action

Create a PDF in Five steps

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace chapter01
{
	/// 
	/// this example ported by Steven Lee 
	/// https://blog.rubypdf.com
	/// This example of java version was written by Bruno Lowagie.
	/// It is part of the book 'iText in Action' by Manning Publications.
	/// ISBN: 1932394796
	/// http://itext.ugent.be/itext-in-action/
	/// http://www.manning.com/lowagie/
	/// This example generates a simple 'Hello World' PDF file.
	/// 
	public class HelloWorld
	{
		/// 
		/// Generates a PDF file with the text 'Hello World'
		/// 
		public static void Main() 
		{
			System.Console.WriteLine("Chapter 1: example HelloWorld");
			System.Console.WriteLine("-> Creates a PDF file with the text 'Hello World';");
			System.Console.WriteLine("   Default values are used for PageSize and margins.");
			System.Console.WriteLine("-> jars needed: iText.jar");
			System.Console.WriteLine("-> files generated in /results subdirectory:");
			System.Console.WriteLine("   HelloWorld.pdf");
			// step 1: creation of a document-object
			Document document = new Document();
			try 
			{
				// step 2:
				// we create a writer
				PdfWriter.GetInstance(
					// that listens to the document
					document,
					// and directs a PDF-stream to a file
					new FileStream("HelloWorld.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"));
			} 
			catch (DocumentException de) 
			{
				System.Console.Error.WriteLine(de.Message);
			} 
			catch (IOException ioe) 
			{
				System.Console.Error.WriteLine(ioe.Message);
			}

			// step 5: we close the document
			document.Close();
		}
	}
}

the java version can be found at:http://itext.ugent.be/itext-in-action/
or from the book of iText in Action

3 thoughts on “HelloWorld–C# version examples of iText in Action”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.