![]() |
Languages »
C# »
General
Intermediate
Creating PDF Tables using C# (.NET 2.0) and iTextSharpBy victorbosCreating PDF Tables using C# (.NET 2.0) and iTextSharp |
C#, Windows, .NETVS2005, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
One of the challenges in creating PDFs, of any kind and not just tabular data, is digesting its many options. These options are described in great detail in free documents available from Adobe, the owner of the PDF formats. (The latest version is over 1,300 pages long!) The picture below shows some of the more common ones.
iTextSharp does a great deal to ease the burden of creating PDFs using C# (.NET 2.0). It largely obviates the need to understand all the minute details of the PDF specifications. However, despite the many examples available, creating non-trivial PDFs with iTextSharp can be bit of a steep climb. A recently released book, iText in Action by the author, is half as long as Adobe's latest specification document. The book is invaluable if you plan to seriously rely on iTextSharp, but you will still have to do some of the heavy lifting. You not only have to glean the concepts underlying Adobe's PDF specifications, but also figure out how iTextSharp is organized to address them. For example, the simple exercise of numbering pages requires you to create an event handler and then make sure you construct and write the appropriate string on the right canvas/layer.
Some additional useful links:
Tabular data are stored in databases of all kinds (SQL Server, Oracle, Access, MySQL, etc.), in proprietary binaries, in spreadsheets, in text files (tab-delimited, XML), etc. So, no one solution, at least not a simple one like mine, will work for all. Put another way, until some bright soul comes along to save us, we will have to build our own solutions for each variant (of data source and PDF layout) we need to address.
The net result of these complications is -- it may be useful to have working code that meets a few simple objectives
This article attempts to do just that.
Creating simple PDFs is easy once you grasp iTextSharp. But, creating useful, practical, decent-looking PDFs can be harder than it looks at first.
| ReleaseDate | Track | Title | Artist | Album |
|---|---|---|---|---|
| 11/22/1968 | 29 | Revolution 9 | Beatles | The Beatles [White Album] |
| 1960 | 6 | Fools Rush In | Frank Sinatra | Nice 'N' Easy |
| 11/11/1971 | 1 | One of These Days | Pink Floyd | Meddle |
| 1988 | 7 | Where Is My Mind? | Pixies | Surfer Rosa |
| 5/1981 | 9 | Can't Find My Mind | Cramps | Psychedelic Jungle |
| 6/10/2003 | 13 | Scatterbrain. (As Dead As Leaves.) | Radiohead | Hail to the Thief |
| 6/30/1992 | 3 | Dress | P J Harvey | Dry |
Such a table can come from a database, spreadsheet, DataGridView, DataGrid, GridView, etc. It is simply a collections of rows of records and columns of fields.
As XML is widely understood and requires no special databases, this article uses two simple types of XML data sources to represent the data table shown above.In the XML shown below, each row (record) is represented in a single node called song and each column (field) in an attribute of the node.
Notice that there is only one level of nodes below the root, i.e., they do not have any child nodes.
<?xml version="1.0" encoding="utf-8"?>
<root>
<song ReleaseDate="11/22/1968" Track="29" Title="Revolution 9"
Artist="Beatles" Album="The Beatles [White Album]" />
<song ReleaseDate="1960" Track="6" Title="Fools Rush In"
Artist="Frank Sinatra" Album="Nice 'N' Easy" />
<song ReleaseDate="11/11/1971" Track="1" Title="One of These Days"
Artist="Pink Floyd" Album="Meddle" />
<song ReleaseDate="1988" Track="7" Title="Where Is My Mind?"
Artist="Pixies" Album="Surfer Rosa" />
<song ReleaseDate="5/1981" Track="9" Title="Can't Find My Mind"
Artist="Cramps" Album="Psychedelic Jungle" />
<song ReleaseDate="6/10/2003" Track="13" Title="Scatterbrain. (As Dead As Leaves.)"
Artist="Radiohead" Album="Hail to the Thief" />
<song ReleaseDate="6/30/1992" Track="3" Title="Dress"
Artist="P J Harvey" Album="Dry" />
</root>
The XmlStore file shown below contains exactly the same data as the plain vanilla XML above, but with a few differences:
root is named xmlStore schema schema has one field node for each attribute of the data row (record), e.g. song field node contains information about
name of the attribute of the data row (record) title displayed in a column header width of the displayed column <?xml version="1.0" encoding="utf-8"?> <xmlStore version="1.0"> <schema datanodename="song"> <field name="ReleaseDate" title="Release Date" width="9"/> <field name="Track" title="Track" width="5"/> <field name="Title" title="Title" width="40"/> <field name="Artist" title="Artist" width="15"/> <field name="Album" title="Album" width="25"/> </schema> <song ReleaseDate="11/22/1968" Track="29" Title="Revolution 9" Artist="Beatles" Album="The Beatles [White Album]" /> <song ReleaseDate="1960" Track="6" Title="Fools Rush In" Artist="Frank Sinatra" Album="Nice 'N' Easy" /> <song ReleaseDate="11/11/1971" Track="1" Title="One of These Days" Artist="Pink Floyd" Album="Meddle" /> <song ReleaseDate="1988" Track="7" Title="Where Is My Mind?" Artist="Pixies" Album="Surfer Rosa" /> <song ReleaseDate="5/1981" Track="9" Title="Can't Find My Mind" Artist="Cramps" Album="Psychedelic Jungle" /> <song ReleaseDate="6/10/2003" Track="13" Title="Scatterbrain. (As Dead As Leaves.)" Artist="Radiohead" Album="Hail to the Thief" /> <song ReleaseDate="6/30/1992" Track="3" Title="Dress" Artist="P J Harvey" Album="Dry" /> </xmlStore>
For more information on XmlStore see A utility to read, edit, encrypt, decrypt, write XmlStore files.
I undertook this project to learn a few things about Visual Studio 2005, C#, XML, DataGridView, PDFs, etc. Caveat Emptor: Nothing has been properly or adequately tested. More important, there is a good chance, you or someone else can do this better. So, if you do use this code and cannot produce what you expect, there may be little I can do to help you. On the bright side, though, you have the source code and all the technical references in the world you will ever need from Adobe and the good people at iTextSharp.
This solution contains three examples from the iTextSharp tutorial web site two of which were not covered in my Tutorials on Creating PDF files using C# (.NET 2.0) and iTextSharp .
This solution also contains code that reflects my somewhat limited understanding of C#, iTextSharp and PDFs. It is merely a learning device and does not purport to be anything more.
IMPORTANT: The XML files in the project's Data folder were the only ones used to actually test the code. These are:
The file Watermark.jpg file came from the iTextSharp Tutorial web site.
The solution contains a tab control with three tabs
Use this button to select an XmlStore or a plain vanilla XML file, like the ones shown above. The PDF file is generated immediately, based on the options you've selected (see picture below).
The options are described very briefly below. (For more detailed information, you will need to read the documentation provided by Adobe and iTextSharp, via the Help menu or some of the links above, or the recently released book by the creator of iText.)
widths. By default, all the columns span the width of the page (leaving room for the margins). The table is always horizontally centered on each page. So, if you set the scale factor to "0.8", the resulting table will be 20% narrower as the column width of each column is reduced proportionately. Setting the scale factor to, say, "1.05", will widen the table and each column by 5%. (NOTE: If the XML file does not contain column width values, each column defaults to the same width.) PdfWriter.PageLayoutTwoColumnLeft otherwise uses the default value PdfWriter.PageLayoutSinglePage (See iTextSharp for details) This solution was created using Visual Studio 2005 and before building the solution you need to use the VS2005 Project > Add Reference menu command to provide a Reference to iTextSharp.DLL version 4.x. If you don't have this, you can download just the DLL or the source [and build it yourself] using one of these links to SourceForge.net:
If you have Visual Studio 2005 then you should be able to use the project source code "out of the box" -- simply build and run. The code itself is not rocket science. It is reasonably documented. More important, you have access to a wealth of information on what the code does via valuable links in the Help menu. If you don't have Visual Studio 2005, you will have to ask a more experienced friend. (Don't ask me, as I am a newbie! I am also clueless about VB.NET!!)
VVX.PDF is a wrapper class used to translate the UI options described above and invoke the appropriate iTextSharp capabilities. It contains the following (each grouped in #region ... #endregion tags),
DoCreateFromXmlStore(...) This relatively tiny method simply follows the iTextSharp steps 1 through 5. For step 4, however, it turns to the DoLoadDocument(...) method to extract the data from the XML file.
DoLoadDocument(...) This is an enhanced and customized version of the LoadDocument method found in Chap0518 and Chap 1202 of the iTextSharp tutorial. Although the code is reasonably documented, it may help you to visit the iTextShart Tutorial web site (perhaps via the Help menu of this solution) and read more about the code.
DoConfigPageEventHandler(...) This is a helper method that initializes the custom "new page" Event Handler you must write to implement the Show ... options, namely, Title, Page Numbers, and Watermarks, as well as painting the "gray" background for the column headers.
VVX.PDF_TableEvents contains essentially a single class that contain the required [by iTextSharp] event handler. It is called after iTextSharp finishes composing each page of a PDF Table. In this class, you
If you want to learn more about iText, on which iTextSharp is based, click here.
If iTextSharp is too difficult you might consider using one of several commercial PDF solutions, some of them are quite inexpensive. However, I haven't had much success with them. (The only place I have found creating PDFs is a breeze is on my iMac; creating PDFs on it from any application is a no-brainer! And you get beautiful PDFs, too.)
I have learned a lot from people like you who have anonymously and freely shared your experiences with the world. Perhaps, you gave a little, but I learned a lot. If this utility helps even one person, you've made my day and given me a chance to give back to the community. So, thank you!
Please consider sending in a donation to one of my favorite causes: Year Up or to any similar NGO (non-governmental organization) in the world that is selflessly doing good work and helping people in need. Give a little, get a lot!
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 23 Mar 2007 Editor: |
Copyright 2007 by victorbos Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |