Add page numbers inside a PDF document in C# using Aspose.PDF - System.NullReferenceException

Hi,

We are experiencing an issue where it throws System.NullReferenceException when we tried to add page on pdf document. We are using license Aspose.PDF Version=20.2.0.0.
PDF Documnet
image.png (36.3 KB)

@jmagalso

Could you please share the complete code snippet for adding stamp in text format with us. Also, please try to use Aspose.PDF for .NET 20.12 as well before sharing the code snippet. We will test the scenario in our environment and address it accordingly.

Below is the complete code snippet that we used to add page number on pdf.

public string AddPageNumbersToPDF(string agendaPacketNumberText, int agendaPacketNumberPosition, string pdfFilePath) {
string outputFile = pdfFilePath.Replace("_out2.pdf", “_out3.pdf”);

		using (Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(pdfFilePath)) {

			//create page number stamp
			Aspose.Pdf.PageNumberStamp pageNumberStamp = new Aspose.Pdf.PageNumberStamp {
				//whether the stamp is background
				Background = false,
				Format = agendaPacketNumberText.Replace("[TOTAL]", pdfDocument.Pages.Count.ToString()),
				BottomMargin = 10,

			};

			if (agendaPacketNumberPosition == 0) {
				pageNumberStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
				pageNumberStamp.LeftMargin = 20;
			}
			else if (agendaPacketNumberPosition == 1) {
				pageNumberStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
			}
			else {
				pageNumberStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
				pageNumberStamp.RightMargin = 20;
			}
			pageNumberStamp.StartingNumber = 1;
			pageNumberStamp.TextState.FontSize = 10.0F;
			pageNumberStamp.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
			pageNumberStamp.StartingNumber = 1;

			for (int i = 1; i <= pdfDocument.Pages.Count; i++) {
				pdfDocument.Pages[i].AddStamp(pageNumberStamp);
			}
			pdfDocument.Save(outputFile);
		}

		File.Delete(pdfFilePath);
		return outputFile;
	}

@jmagalso

We were able to reproduce the issue in our environment while testing the scenario with Aspose.PDF for .NET 20.12. We have logged it as PDFNET-49197 in our issue tracking system for the sake of correction. We will look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFNET-49197) have been fixed in Aspose.PDF for .NET 22.8.