Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / C#
Tip/Trick

Generate a PDF based on HTML content and adding pagenumbers

Rate me:
Please Sign up or sign in to vote.
2.88/5 (5 votes)
10 Feb 2011CPOL2 min read 28.9K   5   4
This article describes how to generate a PDF document based on HTML and add page numbers to each page
Background

I was recently developing a site which needed to produce PDF documents automatically based on HTML content. The HTML originated from a variety of other sites and I needed to consolidate the content into a single PDF file. The challenge was that the full HTML was fairly large and the PDF file needed to be created quickly.

I was doing all the development work in .NET and C# so I starting looking at various .NET modules which could help me accomplish the task. I looked at several different products such as iTextSharp and I wanted a product that was easy to use and also a product that could create a PDF which interpreted the HTML styling in a proper way. After spending several hours testing a variety of products, I found a product from webSupergoo called ABCPdf .NET. I got it working within minutes and it is, in my opinion, the best product for creating a PDF file based on HTML content. The PDF files correctly interpret all the styling information in the HTML, including CSS directives.

One of the advantages with ABCPdf .NET is that it is fairly cheap with regards to the functionality it offers. Licenses for ABCpdf .NET only costs $329 per installed copy. The API is easy to learn and one of the best things with the product is that they offer very good support. I’ve asked for assistance with some of the more complex development tasks that I’ve had and I’ve always received an answer within 24 hrs. The answers have also typically included a code sample.
More info about ABCPdf can be found at Websupergoo


Using the Code
I want to show you how I used ABCPdf .NET to process HTML, add page numbers to each page and then save the generated PDF file. The page number needed to be added at the bottom of the page and also below the content. I wanted to add a separator line below the content and then add the page number just below the separator line. I intended on printing the PDF double sided which meant that the page number needed to be right-aligned on the first page, left-aligned on the second and so on.

C#
//Get the HTML. In this example I'm reading the html from a text file (for demonstration purposes)

string HTML_STRING;
var streamReader = new StreamReader(@"c:\html.txt");
HTML_STRING = streamReader.ReadToEnd();
streamReader.Close();

//****************************************
// Create PDF
//****************************************

//Initiate the document

var theDoc = new Doc();
theDoc.TextStyle.Size = 20;
theDoc.Rect.Inset(40, 55);
theDoc.Color.String = "255 255 255"; //Clear rectangle

//Add the html
int theId = theDoc.AddImageHtml(HTML_STRING);

//We now chain subsequent pages together. We stop when we reach a page which wasn't truncated

while (true)
{
theDoc.FrameRect();
if (!theDoc.Chainable(theId))
break;
theDoc.Page = theDoc.AddPage();
theId = theDoc.AddImageToChain(theId);
}

////////////////////////////////////////////////////
// Set pagenumber
////////////////////////////////////////////////////

theDoc.HtmlOptions.LinkPages();

//Set the position for the page number

theDoc.Rect.String = "35 30 580 50";
theDoc.Font = theDoc.AddFont("Trebuchet");
theDoc.FontSize = 11;
int pagenumber = 1;

//flatten the pages. We can't do this until after the pages have been added because flattening will invalidate our previous ID and break the chain.

for (int i = 1; i <= theDoc.PageCount; i++)
{
theDoc.PageNumber = i;

//Add page number
//========================================

string txt = pagenumber.ToString();
theDoc.Color.String = "169 169 169"; //Dark grey text

//Positioning depends on if the page is even or odd
theDoc.VPos = 1.0;
if ((i%2) == 0)
{
//Even
theDoc.HPos = 0.01;
}
else
{
//Odd
theDoc.HPos = 0.99;
}

//Add the page number
theDoc.AddText(txt);

//Add a line above page number
theDoc.AddLine(21, 55, 590, 55);

//Flatten page
theDoc.Flatten();

//Increase the page number count
pagenumber++;

}

//==============================

////////////////////////////////////////////////////
// Save the pdf file
////////////////////////////////////////////////////

// Save the document
theDoc.Save(@"c:\pdf.pdf");

//Clear
theDoc.Clear();

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHow is this easier than iTextSharp Pin
Takalond13-Aug-15 21:11
Takalond13-Aug-15 21:11 
GeneralReason for my vote of 5 Currently looking at various PDF pro... Pin
johanskane10-Feb-11 7:27
johanskane10-Feb-11 7:27 
GeneralReason for my vote of 1 pure commercial spam. Not a real art... Pin
SteveKing9-Feb-11 21:03
SteveKing9-Feb-11 21:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.