HelloWorldBurst–C# version examples of iText in Action

Because this example need java version iText tools, so we need write a C# version Burst.

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

namespace chapter01
{
	/// 
	/// this example ported by Steven Lee
	/// http://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 'Hello World' PDF file, then cuts it into single pages.
	/// 
	public class HelloWorldBurst
	{
		/// 
		/// Generates a PDF file, then cuts it into single pages..
		/// 
		public static void Main()
		{
			System.Console.WriteLine("Chapter 1: example HelloWorldBurst");
			System.Console.WriteLine("-> Creates a PDF file, then 'bursts' it");
			System.Console.WriteLine("-> jars needed: iText.jar");
			System.Console.WriteLine("-> files generated in /results subdirectory:");
			System.Console.WriteLine("   HelloWorldRead.pdf (will be used as input)");
			System.Console.WriteLine("   HelloWorldRead_1.pdf (first page of HelloWorld.pdf)");
			System.Console.WriteLine("   HelloWorldRead_2.pdf (second page of HelloWorld.pdf)");
			System.Console.WriteLine("   HelloWorldRead_3.pdf (third page of HelloWorld.pdf)");
			// we create a PDF file
			CreatePdf("HelloWorldRead.pdf");
			string[] arg = {"HelloWorldRead.pdf"};
			Burst.Main(arg);
		}

		/**
		 * Generates a PDF file with bookmarks.
		 * @param filename the filename of the PDF file.
		 */
		private static void CreatePdf(string filename)
		{
			// we create a document with multiple pages and bookmarks
			Document document = new Document();
			try
			{
				PdfWriter writer = PdfWriter.GetInstance(document,	new FileStream(filename,FileMode.Create));
				document.Open();
				Paragraph hello = new Paragraph("(English:) hello, " +
					"(Esperanto:) he, alo, saluton, (Latin:) heu, ave, " +
					"(French:) all? (Italian:) ciao, (German:) hallo, he, heda, holla, " +
					"(Portuguese:) al? ol? hei, psiu, bom d韆, (Dutch:) hallo, dag, " +
					"(Spanish:) ola, eh, (Catalan:) au, bah, eh, ep, " +
					"(Swedish:) hej, hejsan(Danish:) hallo, dav, davs, goddag, hej, " +
					"(Norwegian:) hei; morn, (Papiamento:) halo; hallo; k?tal, " +
					"(Faeroese:) hall? hoyr, (Turkish:) alo, merhaba, (Albanian:) tungjatjeta");
				Chapter universe = new Chapter("To the Universe:", 1);
				Section section;
				section = universe.AddSection("to the World:");
				section.Add(hello);
				section = universe.AddSection("to the Sun:");
				section.Add(hello);
				section = universe.AddSection("to the Moon:");
				section.Add(hello);
				section = universe.AddSection("to the Stars:");
				section.Add(hello);
				document.Add(universe);
				Chapter people = new Chapter("To the People:", 2);
				section = people.AddSection("to mothers and fathers:");
				section.Add(hello);
				section = people.AddSection("to brothers and sisters:");
				section.Add(hello);
				section = people.AddSection("to wives and husbands:");
				section.Add(hello);
				section = people.AddSection("to sons and daughters:");
				section.Add(hello);
				section = people.AddSection("to complete strangers:");
				section.Add(hello);
				document.Add(people);
				document.SetPageSize(PageSize.A4.Rotate());
				Chapter animals = new Chapter("To the Animals:", 3);
				section = animals.AddSection("to cats and dogs:");
				section.Add(hello);
				section = animals.AddSection("to birds and bees:");
				section.Add(hello);
				section = animals.AddSection("to farm animals and wild animals:");
				section.Add(hello);
				section = animals.AddSection("to bugs and beatles:");
				section.Add(hello);
				section = animals.AddSection("to fish and shellfish:");
				section.Add(hello);
				document.Add(animals);
			}
			catch (DocumentException de)
			{
				System.Console.Error.WriteLine(de.Message);
			}
			catch (IOException ioe)
			{
				System.Console.Error.WriteLine(ioe.Message);
			}
			document.Close();
		}
	}
}

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

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • BlinkList
  • blogmarks
  • blogtercimlap
  • connotea
  • DotNetKicks
  • Fark
  • Fleck
  • Gwar
  • Haohao
  • IndianPad
  • Internetmedia
  • LinkaGoGo
  • MyShare
  • Netscape
  • NewsVine
  • Rec6
  • Reddit
  • Scoopeo
  • Slashdot
  • StumbleUpon
  • Technorati
  • Webride

One Response to “HelloWorldBurst–C# version examples of iText in Action

Leave a Reply

You must be logged in to post a comment.