How do I add page numbers to PDF pages?

Q: I'm trying to combine pdf file. The original pdf file doesn't have
page number on the each sheet. When I combine those pdf files and I
want to put page number on each sheet physically.
Is there any way to do it?
A: To add a page number you would need to add a new text element to
required pages. For a quick intro on the topic of 'stamping' PDF pages
you may want to take a look at the following FAQ:

Also the following PDFNet SDK Knowledge Base articles are relevant:



Q: I'm trying to use function below. Using this one, I can get the
"page number" located in center on the page. Do you know how I can
change the location of the text on the page? (I try to locate "page
number" upper left corner for this.)

private void Stamp(PDFDoc pdfdoc, string text, double font_sz, ColorPt
fill_color, double rot_angle) {
            ElementBuilder eb = new ElementBuilder();
            ElementWriter writer = new ElementWriter();
            pdftron.PDF.Font myFont = pdftron.PDF.Font.Create(pdfdoc,
            pdftron.PDF.ColorSpace fill_cs = ColorSpace.CreateDeviceRGB

            double deg2rad = 3.1415926535 / 180.0;
            Matrix2D rot_mtx = Matrix2D.RotationMatrix(rot_angle *
            rot_mtx.Scale(1, 1);
            rot_mtx.Set(1, 1, 1, 1, 1, 1);

            PageIterator itr = pdfdoc.PageBegin();
            PageIterator end = pdfdoc.PageEnd();
            int PageCount = 0;
            for (; itr != end; itr.Next()) {
                PageCount = PageCount + 1;
                Page page = itr.Current();
                writer.Begin(page, false);
                Element element = eb.CreateTextBegin(myFont, font_sz);
                element = eb.CreateTextRun(text + Convert.ToString
                GState gs = element.GetGState();

                // Position the text run
                Matrix2D mtx = new Matrix2D(rot_mtx);
                // scale the stamp relative to standard 'letter'
                double scale_factor = page.GetPageWidth() / 612.0;
                mtx.Scale(scale_factor, scale_factor);
                mtx.Translate((page.GetPageWidth() -
element.GetTextLength() * Math.Cos(rot_angle *deg2rad)) / 2,
(page.GetPageHeight() + element.GetTextLength() * Math.Sin(rot_angle
*deg2rad)) / 2);
A: I can update the line with mtx.Translate(). Something along the
following lines:

For upper-left corner:

mtx.Translate( 10, page.GetPageHeigh() - font_sz - 10);

For lower-right corner:

mtx.Translate(page.GetPageWidth() - element.GetTextLength() - 10,
font_sz + 10);

Using our Stamper class this is even easier. For example, to add a page number to the bottom right of each page, you can use the following.

string input_path = “…/…/…/…/TestFiles/”;
string output_path = “…/…/…/…/TestFiles/Output/”;
string input_filename = “newsletter.pdf”;

using (PDFDoc doc = new PDFDoc(input_path + input_filename))
using (Stamper s = new Stamper(Stamper.SizeType.e_font_size, 24, 0))

s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_right, Stamper.VerticalAlignment.e_vertical_bottom);
s.SetPosition(18, 18, false); // position quarter inch from bottom right corner
s.SetFontColor(new ColorPt(1, 0, 0));
PageIterator itr = doc.GetPageIterator();
for (; itr.HasNext(); itr.Next())
int pg_num = itr.GetPageNumber();
s.StampText(doc, String.Format("{0}", pg_num), new PageSet(pg_num));

doc.Save(output_path + input_filename + “.ex0.pdf”, SDFDoc.SaveOptions.e_linearized);
catch (PDFNetException e)