This is my second article towards creating PDF files. The earlier one employed on simple text and this version in C# can add tables to the PDF files. This library can be used for simple HTML conversion, or conversion from other documents if you know that document format. At present, it can add only tables, and create PDF document with any number of pages.
Let us begin by using the library. Download the library and add a reference to the library in your .NET project.
Creating a PDF Document
Creating a PDF document can be divided into three steps.
- Writing the header of the PDF file.
- Creating pages of the document.
- Add text.
- Add table.
- Finish the document.
In each step, you open a file specifying the output file name. In the first step, it's is opened as create, and in every other step it is open as append. In PDF format, every object we create and write are referenced by the byte offset of that object within the file.
I will go by explaining the code sample for using the PDF library.
CatalogDict catalogDict=new CatalogDict();
PageTreeDict pageTreeDict=new PageTreeDict();
FontDict TimesRoman=new FontDict();
FontDict TimesItalic=new FontDict();
InfoDict infoDict=new InfoDict();
Utility pdfUtility=new Utility();
FileStream file=new FileStream(@"c:\text.pdf",FileMode.Create);
Create the pages in the document. Here we are creating only one page. This process can be repeated to include any number of pages. A page will have its contents. Contents are the text and table that we create. This content is an object of the type
ContentDict. A page can have any number of
ContentDicts, but two or more pages cannot have the same
Every page must be added to the
PageTreeDict. This is the RootNode of the pages in the PDF Document structure. In a PDF document, there will be only one
PageTree (created with this library). This is created by using
PageTreeDict.AddPage(). The order of the page in the document will be in the order how there were added to the
To every page, resources must be added. These resource include the font that must be used inside the page and the objects of the
ContentDict. This is done by calling
PageDict.AddResource. The font types that are to be used are specified in the library. In this library, only fonts of type Times New Roman will be correctly centered and right aligned. This is because, for aligning, we must have the width of all the character glimpses. I have stored only the width of Times New Roman.
AddPage, we specify the
objNum of the page to be added, and in
AddResource, we add the
objNum of the
For adding text and tables in the page, create an object of type
TextAndTables. For adding text, call
addText specifying the X and Y coordinates.
For adding table, first fill the parameters of the table using
TableParams. In the constructor, specify the number of columns and their widths. If the widths are same then you can use the other constructor just specifying the number of columns and setting the
numColumns parameter of the structure. In this version, only the three parameters of the table need to be set, they are:
- row height
- X coordinate
- Y coordinate
Now, we can specify the alignment of each text within a cell. If we have two columns, create an array of two Align elements.
After this much initialization, call the
TextAndTable.AddRow() member. This can be called as many times as to add new rows. When finished, call
EndTable(). This will return a string. This string should be passed to
Now add these things to the file. Then we have finished our second step.
PageDict page=new PageDict();
ContentDict content=new ContentDict();
PageSize pSize=new PageSize(612,792);
TextAndTables textAndtable=new TextAndTables(pSize);
Align align=new Align;
ColorSpec cellColor=new ColorSpec(100,100,100);
ColorSpec lineColor=new ColorSpec(98,200,200);
TableParams table=new TableParams(2,200,200);
textAndtable.AddRow(false,10,"T1",align,"First Column","Second Column");
textAndtable.AddRow(false,10,"T1",align,"Second Row","Second Row");
Now that we have finished the creation of the page, we can end the creation of the PDF document. This is done by writing every piece of information to the file.
All the code above is included inside a single function. Usually, for creating HTML files, the steps one and three will be done only once, but step two will be repeated many times. You can use this library and clarify any doubts as you go using this. Try experimenting with this.
This is a very basic library for creating PDF files. Once you know the PDF format, you can modify the library to include many more features like drawing lines and circles. Actually, the project I have been working only required simple tables to be added to the PDF files. That's is why I had to limit to only that feature.