Archive for October 11th, 2006

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

Burst.cs–split a PDF in several separate PDF files (1 per page)

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

namespace chapter01
{
	/// 
	/// This example was written by Steven Lee.
	/// http://blog.rubypdf.com
	/// This tool lets you split a PDF in several separate PDF files (1 per page).
	/// 
	public class Burst
	{
		/// 
		/// split a PDF in several separate PDF files (1 per page)
		/// 
		public static void Main(string[] args)
		{

			try
			{
				FileInfo file=new FileInfo(args[0]);
				string name =file.Name.Substring(0,file.Name.LastIndexOf("."));
				// we create a reader for a certain document
				PdfReader reader = new PdfReader(args[0]);
				// we retrieve the total number of pages
				int n=reader.NumberOfPages;
				int digits = 1 + (n / 10);
				System.Console.WriteLine("There are " + n + " pages in the original file.");
				Document document;
				int pagenumber;
				string filename;

				for (int i = 0; i < n; i++)
				{
					pagenumber = i + 1;
					filename =pagenumber.ToString();
					while (filename.Length< digits) filename = "0" + filename;
					filename = "_" + filename + ".pdf";
					// step 1: creation of a document-object
					document = new Document(reader.GetPageSizeWithRotation(pagenumber));
					// step 2: we create a writer that listens to the document
					PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(name+filename,FileMode.Create));
					// step 3: we open the document
					document.Open();
					PdfContentByte cb = writer.DirectContent;
					PdfImportedPage page = writer.GetImportedPage(reader, pagenumber);
					int rotation = reader.GetPageRotation(pagenumber);
					if (rotation == 90 || rotation == 270)
					{
						cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagenumber).Height);
					}
					else
					{
						cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
					}
					// step 5: we close the document
					document.Close();
				}
			}
			catch (DocumentException de)
			{
				System.Console.Error.WriteLine(de.Message);
			}
			catch (IOException ioe)
			{
				System.Console.Error.WriteLine(ioe.Message);
			}
		}
	}
}
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

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
	/// 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 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

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