Hi,
I want the code to add page numbers to a pdf file using c#. Can anyone help me out for the code .
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Govinda Rajulu YemineniRank #1,517 · 1K reputationPosted Jul 20, 2015, 10:52 PM EDT
Yashwanth MuthineniRank #1,735 · 870 reputationPosted Jul 20, 2015, 11:19 PM EDT
Uehara AyakaRank #2,730 · 512 reputationPosted Jul 30, 2015, 10:39 PM EDT
using Spire.Pdf;
using Spire.Pdf.Widget;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddPageNumber2Pdf
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Test.pdf");
//set the margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
//draw page number
DrawPageNumber(doc.Pages, margin, 1, doc.Pages.Count);
//save the file
doc.SaveToFile("AddNumber.pdf",FileFormat.PDF);
System.Diagnostics.Process.Start("AddNumber.pdf");
}
private static void DrawPageNumber(PdfPageCollection section, PdfMargins margin, int startNumber, int pageCount)
{
foreach (PdfPageBase page in section)
{
page.Canvas.SetTransparency(0.5f);
PdfBrush brush = PdfBrushes.Black;
PdfPen pen = new PdfPen(brush, 0.75f);
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
format.MeasureTrailingSpaces = true;
float space = font.Height * 0.75f;
float x = margin.Left;
float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
float y = page.Canvas.ClientSize.Height - margin.Bottom + space;
y = y + 1;
String numberLabel = String.Format("{0} of {1}", startNumber++, pageCount);
page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format);
page.Canvas.SetTransparency(1);
}
}
}
}
Yashwanth MuthineniRank #1,735 · 870 reputationPosted Aug 1, 2015, 2:07 AM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Aug 1, 2015, 3:33 AM EDT
Yashwanth MuthineniRank #1,735 · 870 reputationPosted Aug 1, 2015, 4:05 AM EDT
Yashwanth MuthineniRank #1,735 · 870 reputationPosted Aug 1, 2015, 4:05 AM EDT
Marvin ReidRank #1,963 · 746 reputationPosted Nov 14, 2023, 9:15 AM EST
You can use the DocumentPage PageNumber property to first get the page number you are on, and then populate an AnnTextObject text annotation with that value. You can add this annotation onto each page of your PDF file.
Here is some code that demonstrates this:
AnnTextObject textObj = new AnnTextObject(); AnnContainer container = new AnnContainer(); double inch = 720.0; using (var document = DocumentFactory.LoadFromFile("Leadtools.pdf", new LoadDocumentOptions())) { foreach (DocumentPage page in document.Pages) { textObj.Points.Add(LeadPointD.Create(1 * inch, 1 * inch)); textObj.Points.Add(LeadPointD.Create(2 * inch, 1 * inch)); textObj.Points.Add(LeadPointD.Create(2 * inch, 2 * inch)); textObj.Points.Add(LeadPointD.Create(1 * inch, 2 * inch)); // Set the text textObj.Text = page.PageNumber.ToString(); container.Children.Add(textObj); }Once that annotations are added, you can embed the text into your final output PDF file using a similar approach:
DocumentConverter docConverter = new DocumentConverter(); docConverter.SetDocumentWriterInstance(new Leadtools.Document.Writer.DocumentWriter()); docConverter.SetAnnRenderingEngineInstance(new AnnDrawRenderingEngine()); var jobData = new DocumentConverterJobData { AnnotationsMode = DocumentConverterAnnotationsMode.Embed, Document = virtualDocument, OutputDocumentFileName = @"C:\LEADTOOLS22\Resources\Images\BurnAnnotationsDoc.pdf", DocumentFormat = Leadtools.Document.Writer.DocumentFormat.Pdf, }; var job = docConverter.Jobs.CreateJob(jobData); docConverter.Jobs.RunJob(job); foreach (var error in job.Errors) Console.WriteLine($"There was an error:{error.Error}"); }https://www.leadtools.com/help/sdk/dh/ac/anntextobject.html
https://www.leadtools.com/help/sdk/tutorials/dotnet-console-burn-annotations-to-a-leaddocument.html