|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThe Tutorials on Creating PDF files using C# (.NET 2.0) and iTextSharp provide insights to a fairly wide variety of PDF files you can create. The focus of this article is on a relatively small, yet very interesting, subset of iTextSharp; the ability to create PDF files in which the data are presented in a table, like the one shown in the picture.ComplicationThere are three challenges one faces, even with the narrow mission of creating a PDF Table.1: The PDF SpecificationOne 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. 2: iTextSharp DocumentationiTextSharp 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: 3: Vast variety of data and tablesTabular 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. Therefore ...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. WARNING:Creating simple PDFs is easy once you grasp iTextSharp. But, creating useful, practical, decent-looking PDFs can be harder than it looks at first. SolutionIn its elemental form, tabular data looks like this:
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.Plain Vanilla XMLIn the XML shown below, each row (record) is represented in a single node called <?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>
XmlStoreThe XmlStore file shown below contains exactly the same data as the plain vanilla XML above, but with a few differences:
<?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. DISCLAIMERI 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. Using the applicationBe sure to read the DISCLAIMER above before you do anything.Main WindowThe solution contains a tab control with three tabs
The Selections tabThe Selections tab contains the facilities needed to create the desired PDF from an XML file. These are briefly described below.XmlStore File ... buttonUse 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.) PDF Document SummaryThe information you provide in these four textboxes is embedded in the PDF:
Default buttonIt populates the various options with illustrative defaults.Show ... optionsThese options only apply to how this solution generates a PDF Table.
Font optionsThese options only apply to how this solution generates a PDF Table. They determine the fonts used for the data fields in the Body and each column Header of the table.
View ... optionsThese options only apply to how this solution generates a PDF Table. They specify your "viewing preferences", namely, how you want Adobe Reader to display the PDF when the file is first opened.
Encrypt ... optionsThese options only apply to how this solution generates a PDF Table. They determine if the PDF file generated needs to be encrypted and/or contain embedded restrictions on the things a reader can do with your PDF when viewing it in an Adobe PDF Reader
Help MenuThe Help menu contains links to two important sources of information:Try them. You will see why they are very important, especially if you are determined to create your own PDFs. Using the codeBe sure to read the DISCLAIMER above before you do anything.PrerequisiteThis 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
Building the solutionIf 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!!) Code modulesSide benefits of the code for newbies (like myself): The project includes modules with reasonably documented and self-explantory code. With luck, they may help you learn how to use a few features of the sub-systems employed. (Many of them are described elsewhere in the articles listed at the end.) Here the focus is only those related to creating PDFs using C#.VVX_PDF.cs
VVX_PDF_TableEvents.cs
iTextIf you want to learn more about iText, on which iTextSharp is based, click here. There are alternativesIf 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.) Finally, a word of thanks to youI 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! Other recent contributionsIf any of these have helped 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! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||