Complex PageNumberStamp Requirement

Hi,


I have a requirement to merge numerous documents and then add a page number stamp in a complex way.

For example:

  • The first section of combined document should not be numbered.
  • The second section starts with an index document which should not be numbered, but remaining pages should be numbered from 1
  • The third section also starts with an index document which is not numbered, but remaining pages should be numbered from 1.
So I end up with a document where for example pages 21-30 are numbered 1-9 and pages 32-40 are also numbered 1-9

How can I achieve this with a PageNumberStamp?

Jim.

Thanks for contacting support.

In order to achieve custom numbering of the pages inside a PDF, you can use TextStamp. Please check following code snippet where I have implement the custom numbering in a PDF as per your requirement.

Document doc1 = new Document();

TextStamp pns = new TextStamp("");

pns.BottomMargin = 10;

pns.HorizontalAlignment = HorizontalAlignment.Center;

pns.VerticalAlignment = VerticalAlignment.Bottom;

int i = 1;

while (i < 20)

{

    Page currpage = doc1.Pages.Add();

    currpage.Paragraphs.Add(new TextFragment("This is First Document with Page # " + i));

    pns.Value = i.ToString();

    currpage.AddStamp(pns);

    i++;

}

Document doc2 = new Document();

i = 1;

while (i <= 11)

{

    doc2.Pages.Add().Paragraphs.Add(new TextFragment("This is Second Document with Page # " + i));

    i++;

}

Document doc3 = new Document();

i = 1;

while (i <= 10)

{

    doc3.Pages.Add().Paragraphs.Add(new TextFragment("This is Third Document with Page # " + i));

    i++;

}


int secondDocStart = doc1.Pages.Count + 2;

int secondDocEnd = doc1.Pages.Count + doc2.Pages.Count;

int thirdDocStart = doc1.Pages.Count + doc2.Pages.Count + 2;

int thirdDocEnd = doc1.Pages.Count + doc2.Pages.Count + doc3.Pages.Count;


doc1.Pages.Add(doc2.Pages);

doc1.Pages.Add(doc3.Pages);


i = 1;

for (int a = secondDocStart; a <= secondDocEnd; a++)

{

    pns.Value = i.ToString();

    doc1.Pages[a].AddStamp(pns);

    i++;

}


i = 1;

for (int a = thirdDocStart; a <= thirdDocEnd; a++)

{

    pns.Value = i.ToString();

    doc1.Pages[a].AddStamp(pns);

    i++;

}

doc1.Save(dataDir + "PageNumberStamps.pdf");

For your reference, an output generated by above code, is also attached. In case of any further assistance, please feel free to contact us.

Best Regards,