How to get the page number and display it

Hi,

I am new to aspose. I want to get the page number of the document in the following format

Page Number / Total Page Number (e.g 3/99. in this 3 is page number and 99 is total pages).

I need to display page number in this format as watermark. Is there any method to get the each page number and total page number. so that i can add this as a string the in the stamp and i can display it as a water mark. Kindly help me in this.

Thank you in advance

Regards

Srirangam K

Hello Srirangam,

Thanks for considering Aspose.

Please try the following code snippet for your desired results.

string inFile = @"C:\Temp\pdf_Temp\Employee.Pdf";
string outFile = @"C:\Temp\pdf_Temp\StampLogo.pdf";

PdfFileStamp fileStamp = new PdfFileStamp(inFile, outFile);
PdfFileInfo fileInfo = new PdfFileInfo(inFile);
FormattedText ft = new FormattedText("Page #" + "/" + fileInfo.NumberofPages);

fileStamp.AddPageNumber(ft);
fileStamp.Close();

For more information kindly visit public void AddPageNumber(FormattedText); and if you need to add Watermark at particular position in page please check public void AddPageNumber(FormattedText,int);
For information on displaying Pdf file information kindly visit http://www.aspose.com/documentation/file-format-components/aspose.pdf.kit-for-.net-and-java/show-pdf-information.html

Hi,

FileStream aFS = new FileStream(@"C:\CSharp.pdf", FileMode.Open, FileAccess.Read);

Int32 aLength = (Int32)aFS.Length;

FileStream aOS = new FileStream(@"C:\CSharpcopy.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);

MemoryStream aMS1 = new MemoryStream(aLength + 1000000);

PdfFileStamp aPFS = new PdfFileStamp(aFS, aMS1);

FormattedText aFT = new FormattedText(DateTime.Now.ToString());

aPFS.AddFooter(aFT,20);

//Instantiate a stamp object

Stamp logoStamp = new Stamp();

//Instantiate an object of FormattedText class

FormattedText formatText = new FormattedText("User Name", System.Drawing.Color.FromArgb(192, 192, 192), FontStyle.TimesItalic, EncodingType.Winansi, false, 50);

//BindLogo to PDF

logoStamp.BindLogo(formatText);

logoStamp.Rotation = 50;

logoStamp.SetOrigin(200, 275);

aPFS.AddStamp(logoStamp);

PdfFileInfo fileInfo = new PdfFileInfo(@"C:\CSharp.pdf");

FormattedText ft = new FormattedText("#" + "/" + fileInfo.NumberofPages);

aPFS.AddPageNumber(ft);

aPFS.Close();

aMS1.WriteTo(aOS);

aFS.Close();

aMS1.Flush();

aMS1.Close();

I am using the above code. here i am trying to add water mark, footer and page number. Now i have placed the code for adding page number, below the footer and water mark. now what is hppening is only page number is displayed in the page. Footer and water mark is not getting displayed. i have given different position for displaying page number like bottom right, upper right. but still footer and water mark is not getting displayed. If i move the adding page number code above the footer and water mark display code then page number is not getting displayed. but footer and water mark are getting displayed. I think footer and water mark is overwriting page number. Depends on the order of the code one thing is overwriting others. Kindly help me how to display water mark, footer and page number together.

Thank you............

Regards

Srirangam K

Hello Srirangam,

We apologies for the inconvenience. I have tested the code and have been able to reproduce the issue. I have discussed it with the concerned department, soon you will be udpated with the status.

hi,

Sorry for the inconvenience, I make some complements for the PdfFileStamp, in PdfFileStamp, the function of AddStamp and the function of AddPageNumber can not be used at the same time, or only one function work. We will add some comments for the use of these functions. now I modified your code in order to finished your task, please refer to the code below:

FileStream aFS = new FileStream(TestPath +"example1.pdf", FileMode.Open, FileAccess.Read);
FileStream aOS = new FileStream(TestPath +"addFooter_out.pdf", FileMode.Create, FileAccess.ReadWrite);
MemoryStream ms = new MemoryStream();
PdfFileStamp aPFS1 = new PdfFileStamp(aFS, ms);
FormattedText aFT = new FormattedText(DateTime.Now.ToString());
aPFS1.AddFooter(aFT, 20, 30, 0);
Stamp logoStamp = new Stamp();
FormattedText formatText = new FormattedText("User Name", System.Drawing.Color.FromArgb(192, 192, 192), FontStyle.TimesItalic, EncodingType.Winansi, false, 50);
logoStamp.BindLogo(formatText);
logoStamp.Rotation = 50;
logoStamp.SetOrigin(200, 275);
aPFS1.AddStamp(logoStamp);
aPFS1.Close();
PdfFileInfo fileInfo = new PdfFileInfo(TestPath + "example1.pdf");
ms.Position = 0;
PdfFileStamp aPFS2 = new PdfFileStamp(ms, aOS);
FormattedText ft = new FormattedText("#" + "/" + fileInfo.NumberofPages);
aPFS2.AddPageNumber(ft);
aPFS2.Close();
aFS.Close();
aOS.Close();

Hi,

Thank you for your quick reply. The work around which was given by is working fine. Once againg thank you for your quick respone. Actually i need one more clarification. Can we display the page number anywhere in the page or we should display it only in the predefined positions like bottom middle, top center, top right etc? Because in my application i should display it in the center of the page like water mark display. is it possible to display it in the center of the page? Kindly help me on this.

Thank you in advance

Regards

Srirangam K

Hi,

Now, no method in our kit supports to add page number anywhere like water mark display, but we can add such a method to support your application.

Hi Allen Wen,

Can you give me the method to add page number anywhere in the page as you told? so that it will be very greatful for us. As I told I need to display the page number in the center of the page.

Regards

Srirangam K

Hi,

Sorry for not explainning this problem carefully.

First, our kit don't support add page number anywhere, but we can add a interface to support the function.

Second, if you only need add page number in the center of the page, you can refer to the code below:

FileStream aFS = new FileStream(TestPath +"example1.pdf", FileMode.Open, FileAccess.Read);
FileStream aOS = new FileStream(TestPath +"addFooter_out.pdf", FileMode.Create, FileAccess.ReadWrite);
PdfFileStamp aPFS1 = new PdfFileStamp(aFS, aOS );
PdfFileInfo fileInfo = new PdfFileInfo(TestPath + "example1.pdf");
FormattedText ft = new FormattedText("#" + "/" + fileInfo.NumberofPages);
//you can adjust the leftMargin or rightMargin to adapt your document
aPFS1.AddPageNumber(ft,6,300,0,0,0);
aPFS1.Close();
aFS.Close();
aOS.Close();

Hi Allen,

Once again thank you for your quick response. Actually can i display the page number with particular angle. because water mark we can display with particular angle using "Rotation" Property. Is any way to display the page number in particular angle? Kindly help me on this.

Thank You

Regards

Srirangam K

Hi,

Our kit don't support to display the page number in particular angle now, but we can not improve the AddPageNumber to support rotation.

Hi Allen,

Thank you. So we can't display the page number with angle of rotation. I will give this as reference to my client and try to convince them.

Thank You

Regards

Srirangam K

Hi,

In my last post, I am sorry that a typewriting error was happened, my real intent is that we can improve the function of AddPageNumber to support rotation, moreover, we have finished the work and the function will appear in our future verion.