Subscribe Now

ABC, 123, Ruby, C#, SAS, SQL, TDD, VB.NET, XYZ

Tuesday, November 6, 2007

Test Driving TallPDF.NET 3.0

I am a big fan of PDF documents. The only thing about them that is a drag is creating them. I usually create them from a source document of some kind using CutePDF Writer. As the website says, "FREE for personal and commercial use! No watermarks! No Popup Web Ads!" What could be better!? It installs as a printer driver, so you can create a PDF out of just about any kind of file.

For example, I can take this carefully drawn self-portrait in mspaint.exe and make a PDF using CutePDF Writer in no time flat.

self portrait in mspaint

Hit the File | Print... menu...

CutePDF Writer

Select CutePDF Writer, press Print, et voila! It's a PDF...

PDF of self portrait

What could be better than that? Well, I'll tell you...programmatically creating PDFs!

This is where TallPDF.NET 3.0 comes in.

After downloading this .NET component (I really just need the DLL, although the documentation that comes with it is nice, too), I just reference it in any old .NET application. I will use a C# console application.

Here's all I need in the way of code and references.

Visual Studio C# console program to programmatically create a PDF

I actually don't need System.Data or System.Xml for this example, but System.Drawing, System.Web, and of course TallComponents.PDF.Layout are all required. As you can see from the using statements, TallPDF is organized into multiple namespaces. Here I only reference the three required by this example. The object model is pretty self-explanatory: we have a document object, with one or more section objects, each containing paragraph objects which may be text or shapes or images. At the end of the code, we're just pumping PDF bits through a file stream using the Document.Write method.

The PDF produced looks like this.

self portrait PDF

Here's a slightly more complex example.

using System;
using System.Drawing;
using System.IO;
using TallComponents.PDF.Layout;
using F = TallComponents.PDF.Layout.Fonts;
using TallComponents.PDF.Layout.Paragraphs;
using TallComponents.PDF.Layout.Navigation;
using TallComponents.PDF.Layout.Shapes;
using B = TallComponents.PDF.Layout.Brushes;
using P = TallComponents.PDF.Layout.Pens;
using FLD = TallComponents.PDF.Layout.Shapes.Fields;

namespace TestTallPdf2
class Program
static void Main(string[] args)
string lorem1 = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit...";
string lorem2 = "Cras suscipit. Aliquam hendrerit. Vivamus aliquam. Vestibulum...";
Document doc = new Document();
Section sec = new Section();

Header hdr = new Header();
TextParagraph phdr = new TextParagraph();
phdr.Fragments.Add(new Fragment("My Lorem Ipsum Document", F.Font.HelveticaBoldOblique, 12));
hdr.TopMargin = new Unit(0.5, UnitType.Inch);
sec.Header = hdr;

Heading hd1 = new Heading(0);
hd1.SpacingBefore = 10;
hd1.SpacingAfter = 5;
hd1.Fragments.Add(new Fragment("Introduction", F.Font.Helvetica, 11));

TextParagraph psec = new TextParagraph();
psec.Fragments.Add(new Fragment(lorem1, F.Font.TimesRoman, 10));
psec.SpacingAfter = 10;
psec.LineSpacing = 3;

Heading hd2 = new Heading(0);
hd2.SpacingBefore = 10;
hd2.SpacingAfter = 5;
hd2.Fragments.Add(new Fragment("About the Author", F.Font.Helvetica, 11));

TextParagraph psec2 = new TextParagraph();
psec2.Justified = true;
psec2.Fragments.Add(new Fragment(lorem2, F.Font.TimesRoman, 10));
psec2.LineSpacing = 3;

Heading hd3 = new Heading(0);
hd3.SpacingBefore = 10;
hd3.SpacingAfter = 5;
hd3.Fragments.Add(new Fragment("1/4 of a Pie", F.Font.Helvetica, 11));

Drawing drawing = new Drawing(60, 60);
PieShape pie = new PieShape();
pie.Start = 0;
pie.Sweep = 90;
pie.Pen = new P.Pen(System.Drawing.Color.Red, 2);
pie.Brush = new B.SolidBrush(System.Drawing.Color.Blue);

Heading hd4 = new Heading(0);
hd4.SpacingBefore = 10;
hd4.SpacingAfter = 5;
hd4.Fragments.Add(new Fragment("Lorem Ipsum Bracelet", F.Font.Helvetica, 11));

Drawing drawing2 = new Drawing(180, 180);
ImageShape img = new ImageShape(@"C:\my_folder\bracelet.jpg");

Heading hd5 = new Heading(0);
hd5.SpacingBefore = 10;
hd5.SpacingAfter = 5;
hd5.Fragments.Add(new Fragment("Enter your name", F.Font.Helvetica, 11));

Drawing drawing3 = new Drawing(200, 30);
FLD.TextFieldShape field = new FLD.TextFieldShape(200, 30);
field.FullName = "txtName";

ViewerPreferences vp = new ViewerPreferences();
vp.ZoomFactor = 0.75; //75%
doc.ViewerPreferences = vp;
using (FileStream fs = new FileStream(@"C:\my_folder\lorem.pdf",
FileMode.Create, FileAccess.Write))

This produces a PDF looking like this.

Lorem Ipsum PDF

Pretty nice, eh!

It works in ASP.NET, too.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TallComponents.PDF.Layout;
using TallComponents.PDF.Layout.Paragraphs;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
Document doc = new Document();
Section sec = new Section();
TextParagraph par = new TextParagraph();
par.Fragments.Add(new Fragment("Hello from ASP.NET!"));

ASP.NET example

No comments: