Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C++
Article

Create and manipulate PDF documents - 100% .NET

1 Dec 2003CPOL5 min read 571.4K   199   176
TallPDF.NET and PDFKit.NET are 100% .NET components written entirely in C# for creating, manipulating and reading PDF documents and PDF forms.The focus is to ease the task of integrating our component in a larger application.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

This is a showcase review for our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

TallPDF.NET and PDFKit.NET are 100% .NET components written entirely in C# for creating, manipulating and reading PDF documents and PDF forms.The focus is to ease the task of integrating our component in a larger application. Th is is done by providing a highly intuitive object model and supporting main stream technologies such XML/XSL and ADO.NET.

Download evaluations: This link is outdated and has been removed

View demos online: This link is outdated and has been removed

TallPDF.NET 2.0

TallPDF.NET is a 100% managed .NET component for dynamically creating documents from scratch. This component is typically deployed as part of an ASP.NET application that generates dynamic PDF from a data source or user input. However, it is just as well possible to integrate TallPDF.NET in a desktop application. Central to TallPDF.NET is a highly intuitive object model consisting of classes like Document, Section, Paragraph, Table, Footer, etc.

You can create documents either programmatically or from XML. When generating PDF from XML, classes are represented by elements and properties by attributes or child elements. You typically write an XSL file to transate your own specific XML documents to the TallPDF.NET XML format. Using the non-cached forward-only characteristics of the XmlReader class, you can pipeline these steps and achieve extremely low memory consumption, completely independent of document size. This makes TallPDF.NET an excellent solution for generating both small and very large documents.

Image 1

Features

PDF Featuresbookmarks; document properties; internal cross-references; external cross-references
Text Formattingtext color; single and double strike-through; single and double underline; sub- and superscript; left, right and center alignment; justification; hyperlinking
Page Layoutvertical alignment; relative positioning (flow layout); absolute positioning (grid layout); mix grid and flow layout; keep-with-next constraints; do-not-break constraints
Tablescolspan; vertical and horizontal cell alignment; nest tables; fit-to-content; borders; backgrounds; repeat one or more header rows
Generationdeclarative or event-driven; flash-out pages while generating; from XML using the forward-only XmlReader class; from ADO.NET using a forward-only database cursor
ImagesBMP, GIF, JPEG, TIFF, PNG, DCT (JPG) and Flate compressiom; multipage TIFF; from file, memory or System.Drawing.Bitmap
Document Layoutdivide a document into sections; assign headers and footer to each section; standard page size; distinguish between first, middle, last, odd and even headers/footers
Fonts14 standard PDF fonts; TrueType subset embedding; Unicode; support for Japanese, Cyrillic, Hebrew, Arabic, etc.
Encryptionweak and strong passwords; restrict privileges such as allow print/copy
Drawinglines, rectangles, ellipses, pies, arcs, images, text, brushes and pens,draw inside relatively positioned boxes,draw on entire page as a canvas

Generate PDF programmatically

The following code sample shows the basics of generating PDF programmatically from an ASP.NET page.

ASP.NET
<%@Page Language="C#"%>
<%@Import Namespace="TallComponents.PDF.Layout"%>
<%
// create a new instance of the TallPDF.NET document
Document doc = new Document();

// add a section to the document
Section section = new Section();
doc.Sections.Add( section );

// add a text paragraph to the section
TextParagraph textParagraph = new TextParagraph();
section.Paragraphs.Add( textParagraph );

// add a piece of text to the text paragraph
Fragment fragment = new Fragment();
textParagraph.Fragments.Add( fragment );
fragment.Text = "Hello world!";

// stream out the PDF directly to the browser
doc.Write( Response );
%>      

Generate PDF from XML

The central idea of TallPDF.NET is that you can build an instance of the TallPDF DOM and that you can save that instance to PDF. Instead of building a DOM instance programmatically, you can also load it from XML. There is no difference between a DOM instance loaded from XML and one that has been constructed programmatically.

The following code sample show a typical XML file and how it is converted to PDF.

XML
<document author="Jim">
   <section pagesize="Letter">
      <!-- content goes here... -->
   </section>
</document>

// create an XmlDocument and load it
XmlDocument xml = new XmlDocument();
xml.Load( "topdf.xml" );

// create a PDF document and initialize it from XML
Document document =
 new  Document(); document
.Read( xml.DocumentElement );

// stream out the PDF to file
FileStream file = new FileStream( "out.pdf", FileMode.Create );
document.Write( file );

Use XSL to transform any XML document to PDF

The following code sample shows how the use an XSL to transform an existing XML to PDF:

C#
// load the XML
XmlDocument xml = new XmlDocument();
xml.Load( "data.xml" );

// load the XSL
XslTransform xsl = new XslTransform();
xsl.Load( "transform.xsl" );

// transform the XML using the XSL
XmlReader reader = xsl.Transform( xml, null );

// create a new PDF document and initialize if from the transformed XML
Document document = new Document();
document.Read( reader );

// stream out the PDF to file
FileStream file = new FileStream( "out.pdf", FileMode.Create );
document.Write( file );

PDFKit.NET 1.0

PDFKit.NET allows you to split, append, stamp, encrypt PDF documents and fill PDF forms. PDFKit.NET is a 100% managed .NET component for manipulating PDF documents and forms on the fly. This component is typically deployed as part of an ASP.NET application, however, it is just as well possible to integrate PDFKit.NET in a desktop application. Central to PDF.NET is a highly intuitive object model consisting of classes like Pages, Page, Overlay, Underlay, Shapes and Fields.

Image 2

Features

Split and Appendcreate a new document; get single or multiple pages from an existing PDF document; combine existing and new page to cretae a new document
Form filling(multiline) text fields, check boxes, radio button, list boxes and combo boxes, read position and size of fields, flatten, remove or preserve fields
Security / Encryption40 and 128 bit encryption; consume encrypted documents; set user and owner password; set user privileges such as allow print/copy
Stampingmultiline text (alignment, justification, multiple fonts, etc); hyperlinks; primitive shapes such as Line, Pie, Rectangle, Image; position, rotate and scale shapes
Generalwrite to disk, memory or directly to an HTTP response; read from disk or memory;constant, low memory consumption, independent of document size

Filling PDF forms

All form fields of a PDF form can be accessed through the object model of PDFKit.NET. You can enumerate the fields either at document or at page level. You can also index fields either by position or by name.

A field is represented by the Field class. You can query the field for its type (text box, radio button, etc), its position and size and various attributes such as 'multiline' and 'read-only'. If a field represents a selection control such as a combo box or radio button group, you can also enumerate the options. Finally you can fill a form field by setting its Value property and flatten it by setting the Flatten property.

C#
// open an existing PDF form
FileStream file = new FileStream( "form.pdf", FileMode.Open, FileAccess.Read );
Document document = new Document( new BinaryReader( file ) );

// retrieve field
Page page = document.Pages[0];
Field field = page.Fields["LastName"]; 

/// fill out field and flatten it
field.Value = "John Smith";
field.Flatten = true;

// stream the filled PDF form directly to the browser
Response.ContentType = "application/pdf";
document.Write( new BinaryWriter( Response.OutputStream ) );

Stamping PDF documents

PDFKit.NET allows you to open an existing PDF document and retrieve each page by index. A page is represented by a Page class. This class exposes 2 layers: an overlay and an underlay. You can draw on each of these layers by adding shapes to it. PDFKit.NET provides a large set of primitive shapes such as Line, Pie, Rectangle, Text, Image (BMP, JPEG, etc.). Each shape can be located anywhere on the page, rotated by any angle and scaled.

C#
// open an existing PDF document
FileStream file = new FileStream( "in.pdf", FileMode.Open, FileAccess.Read );
Document document = new Document( new BinaryReader( file ) );

// stamp blue verdana text over existing content
TextShape text = new TextShape( 10, 300, "John Smith", 
                         Font.TrueType( "verdana.ttf" ), 10, Color.Blue ); 
page.Overlay.Add( text );

// stream the filled PDF form directly to the browser
Response.ContentType = "application/pdf";
document.Write( new BinaryWriter( Response.OutputStream ) );

Splitting and combining PDF documents/pages

After you have opened a PDF document you can select pages from it and use them to build a new document. You can append pages that originate from different documents.

C#
// open existing documents
FileStream file1 = new FileStream( "x.pdf", FileMode.Open, FileAccess.Read );
Document source1 = new Document( new BinaryReader( file1 ) );

FileStream file2 = new FileStream( "y.pdf", FileMode.Open, FileAccess.Read );
Document source2 = new Document( new BinaryReader( file2 ) );

// create a new, empty document
Document target = new Document();

// append pages from the existing documents
target.Pages.Append( source1.Pages[4] );
target.Pages.Append( source2.Pages[30] );
target.Pages.Append( source1.Pages[45] );
target.Pages.Append( source2.Pages[230] );

// stream the new PDF document directly to the browser
Response.ContentType = "application/pdf";
target.Write( new BinaryWriter( Response.OutputStream ) );

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
Worked for some years as a software engineer, architect and project leader for different software companies. Works at TallComponents, vendor of class libraries for creating, manipulating and rendering PDF documents.

Comments and Discussions