RubyPDF Blog .NET,iText in Action,iTextSharp(iText#),PDF,tutorial Using the iTextSharp library in Delphi .NET

Using the iTextSharp library in Delphi .NET

from http://www.noliturbare.com/iTextDelphi.php


This example, taken from from the iText tutorial,
demonstrates how easy the iTextSharp library can be used in Delphi .NET,
using the free (!) Explorer Edition of Turbo Delphi .NET, to create or manipulate PDF’s.
Note that there is also a difference between the original example and the Delpi one:
this C# example is a command line application and the Delphi application is a Windows application.
When run, this simple application creates a PDF file containing just the text “Hello World”.


C# Hello World example


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

using iTextSharp.text.pdf;

namespace iTextSharp.tutorial.Chap01
{
public class Chap0101
{
public Chap0101()
{
Console.WriteLine("Chapter 1 example 1: Hello World");
// step 1: creation of a document-object
Document document = new Document();
try
{
// 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"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document

document.Close();
}
}
}

Turbo Delphi .NET Hello World example


{Create a new VCL Forms Application. Add a reference to the iTextSharp.dll. Add a button to the form and apply the following code to the button's onclick event}

uses
Windows, Messages, SysUtils, ...., iTextSharp.text, iTextSharp.text.pdf;
....
....

procedure TForm1.Button1Click(Sender: TObject);
var
MyDoc : iTextSharp.text.Document;
Writer : iTextSharp.text.pdf.PdfWriter;
begin
// step 1: creation of a document-object
MyDoc := Document.Create;
try
// step 2: we create a writer that listens to the
// document and directs a PDF-stream to a file

Writer := PdfWriter.GetInstance(MyDoc, TFileStream.Create('C:\Chap0101.pdf', fmCreate));
// step 3: we open the document
MyDoc.Open();
// step 4: we Add a paragraph to the document
MyDoc.Add(Paragraph.Create('Hello World'));
except
ShowMessage('Oops... exception');
end;
// step 5: we close the document

MyDoc.Close();
end;

{Note that the file "Chap0101.pdf" is written to the root of the C: disk; change this as you like}

Leave a Reply

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