PageCount returns 0 when creating a new pdf file

Hi,


I’m generating a new pdf document using the Aspose.Pdf.Generator.Pdf and i want to the write the no.of pages in the same pdf. The text i’m going to write may contain HTML tags so im using the pdf generator.

When i use the PageCount property it returns 0. Please let me know how can i achieve this.

Hi Senthil,


Thanks for contacting support.

Aspose.Pdf.Generator namespace provides the capability to create PDF files from scratch and until the PDF file is saved, you cannot get the total number of pages present in PDF file. So if you are using Pdf.PageCount property before calling pdf1.Save(…) method, you will always get 0 as output. However you can use this property after Pdf.Save(…) method, in order to get appropriate page count.

We apologize for this inconvenience.

Hi,


Adding more to my previous comments, you may also consider using Aspose.Pdf.Document class to get the page count of PDF document generated with Aspose.Pdf.Generator namespace. Please take a look over following code snippet.

[C#]

//Save the Pdf<o:p></o:p>

pdf1.Save(“c:/pdftest/TableResult.pdf”);

// load existing PDF file<o:p></o:p>

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(pdf1);

// print page count

Console.WriteLine(doc.Pages.Count);

Hi Nayyer,


As i said, i have to render the html text while writing into pdf so i cannot use the
Document object.

Is there any enhancement available to add this capability to PageCount or please let me know any workaround to achieve the below

1. Generate PDF file rendering html text
2. Same PDF should contain a word saying the no. of actual pages generated

Hi Senthil,


Thanks for your feedback. You can use Replaceable symbol $P to print Total no. of Pages. Please check documentation link for the details. Hopefully it will serve the purpose.

row = tab.Rows.Add();<o:p></o:p>

//faxCoverPageCount = pdf.PageCount;

cell = row.Cells.Add("Total number of pages: $P");

.....

.....


Please feel free to contact us for any further assistance.


Best Regards,


Hi Tilal,


Thanks for the details. Please help me with the below scenario which should return the result as

1. Two pdf files
2. I want to merge these 2 pdf files
3. Total PageCount should be added to the 1st pdf page at specific cell (PagCount for Pdf1 + PageCount for pdf2).

Hi Senthil,


Thanks for your inquiry. Please check following documentation link to concatenate PDF files. To add PageCount in first page, later you can find a text place holder in specific cell and replace it with total no of pages using doc.Pages.Count property. Hopefully it will serve the purpose.


Please feel free to contact us for any further assistance.

Best Regards,

Hi Tilal,


Currently im doing like what you have suggested only but im not sure whether it will have any performance impact when finding and replacing a text for just updating the page count in first page.

Hi Senthil,


Thanks for your feedback. Hopefully it will not have any impact on performance, while searching text in as single page. Please give it a try, if you find any issue then please do let us know we will further investigate this scenario.

Best Regards,

Senthil B:
1. Two pdf files
2. I want to merge these 2 pdf files
3. Total PageCount should be added to the 1st pdf page at specific cell (PagCount for Pdf1 + PageCount for pdf2).
Hi Senthil,

Thanks for sharing the details.

Adding more to Tilal’s comments, once the PDF files are concatenated and you simply need to display the total page count, you can get the page count information and add this information as TextStamp at particular page region. You may try using the following code snippet to accomplish your requirement.
[C#]

// create Document object<o:p></o:p>

Document doc = new Document("c:/pdftest/facture.pdf");

// load second PDF file

Document doc2 = new Document("c:/pdftest/Updated_Page_Rotation.pdf");

// add pages of first PDF file to second document

doc2.Pages.Add(doc.Pages);

// get the page count of Document2 object (which now contains pages of both document)

//create text stamp

TextStamp textStamp = new TextStamp(doc2.Pages.Count+"");

//set origin

textStamp.XIndent = 100;

textStamp.YIndent = 100;

//add stamp to particular page (first page)

doc2.Pages[1].AddStamp(textStamp);

// save the PDF document

doc2.Save(“c:/pdftest/ConcatenatedFile_with_PageCount.pdf”);

Thanks Tilal and Nayyer.


I will give a try and reply if any issues.

Hi Senthil,


We will be waiting for your feedback and in case we can be of any further assistance, please do let us know.