Free Support Forum - aspose.com

Page Numbering How Do You Control The Page Numbers?

I have a contract that has the following sections:

  1. First Page
  2. Addendum A
  3. Addendum B
  4. Addendum C
  5. Addendum D

On the footers for the first four sections I put "Page $p of $P".

When I get to the Addendum D section I set the "IsPageNumberRestarted" to true. In the footer of the Addendum D section I put "Amendment Page: $Spof $SP" and it prints the page perfectly.

What I want to do is when I reach the Addendum C section last page I want to obtain that as the last page so it won't print "Page 4 of 5". I want it to print "Page 4 of 4".

I've tried getting the number of Pages by doing the following: Int32 NumPages = pdf.Sections[mySectionId].PageCount

but my value always comes back to zero.

Is there a way to manipulate a section and put an SetLastPageNumber property to value??

AFTER I add the Text object for each section, I tried:

Int32 TotalNumPages = FirstPageSection.PageCount + AddendumASection.PageCount + AddendumBSection.PageCount + AddendumCSection.PageCount;

and it STILL returns ZERO!!! Wouldn't it calculate the number of pages then?? It has the sections already populated with text!!

I could get the Section.StartingPageNumber but what if a section runs more than one page! If you would have a “LastPageNumber” property then I could have something to work with!!

public Pdf CreateServiceContract(Hashtable htSections, ServiceContractConst.ContractStatus myeContractStatus)
{
Pdf pdf = new Pdf();

if (htSections == null)
throw new Exception("Error: Tropical.Pricing20.SvcContract.Bus.PDF.AsposePdf.CreateServiceContract() found the Hashtable with the list of template sections null");

if(htSections.Count == 0)
throw new Exception("Error: Tropical.Pricing20.SvcContract.Bus.PDF.AsposePdf.CreateServiceContract() found the Hashtable with the list of template sections empty (count = 0)");

HeaderFooter myHeader = new HeaderFooter();
Text HeaderText;

HeaderFooter myFooter = new HeaderFooter();
Text FooterText;

HeaderFooter myDSectionFooter = new HeaderFooter();
Text DSectionFooterText;

HeaderFooter myDSectionFooterSignature = new HeaderFooter();
myDSectionFooterSignature.IsLastPageOnly = true;

Section FirstPageSection = new Section();
Text FirstPageText;

Section AddendumASection = new Section();
Text AddendumAText;

Section AddendumBSection = new Section();
Text AddendumBText;

Section AddendumCSection = new Section();
Text AddendumCText;

Section AddendumDSection = new Section();
Text AddendumDText;

Section AddendumDSectionSignature = new Section();
Text AddendumDTextSignature;

TextInfo ContractTextInfo = new TextInfo();
ContractTextInfo.FontName = "Arial";
ContractTextInfo.FontSize = 8;
//ContractTextInfo.IsUnicode = true;

MarginInfo ContractMarginInfo = new MarginInfo();
ContractMarginInfo.Left = 10F;
ContractMarginInfo.Right =10F;
ContractMarginInfo.Top = 10;
ContractMarginInfo.Bottom = 10;

TextInfo PageTitleTextInfo = new TextInfo();
PageTitleTextInfo.FontName = "Arial";
PageTitleTextInfo.FontSize = 9;
PageTitleTextInfo.IsTrueTypeFontBold = true;
PageTitleTextInfo.Alignment = AlignmentType.Center;

Text PageTitleText;

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
image1.ImageInfo.File = @"~/Images/DRAFTWATERMARK.gif";
image1.ImageInfo.ImageFileType = ImageFileType.Gif;
image1.ImageWidth = 359;
image1.ImageHeight = 84;
// image1.ImageScale = 0.1f;
image1.Opacity = 0.1F;
image1.ZIndex = 0;
FloatingBox watermark1 = new FloatingBox(200, 100);
watermark1.BoxHorizontalPositioning = BoxHorizontalPositioningType.Page;
watermark1.BoxHorizontalAlignment = BoxHorizontalAlignmentType.Center;
watermark1.BoxVerticalPositioning = BoxVerticalPositioningType.Page;
watermark1.BoxVerticalAlignment = BoxVerticalAlignmentType.Center;
watermark1.Paragraphs.Add(image1);

Text DraftText = new Text("CONTRACT IS IN " + myeContractStatus.ToString().ToUpper()+" STATUS");
TextInfo DraftTextInfo = new TextInfo();
DraftTextInfo.FontName = "Arial";
DraftTextInfo.FontSize = 10;
DraftTextInfo.IsTrueTypeFontBold = true;
DraftTextInfo.Alignment = AlignmentType.Center;
Aspose.Pdf.Color myDraftTextColor = new Aspose.Pdf.Color("Red");
DraftTextInfo.Color = myDraftTextColor;
DraftText.TextInfo = DraftTextInfo;

foreach (DictionaryEntry de in htSections)
{
String mySectionName = de.Key.ToString().ToUpper();
String mySectionText = de.Value.ToString();
if (mySectionName.Contains("HEADER"))
{
myHeader.DistanceFromEdge = 20;
Aspose.Pdf.Image imgTSLogo = new Aspose.Pdf.Image(myHeader);//(sec);
imgTSLogo.ImageInfo.File = @"~/Images/WOS_TSlogo_Mod.jpg";
imgTSLogo.ImageInfo.ImageFileType = ImageFileType.Jpeg;
imgTSLogo.ImageWidth = 150;
imgTSLogo.ImageHeight = 57;

HeaderText = new Text(mySectionText);
TextInfo HdrTxtInfo1 = new TextInfo();
HdrTxtInfo1.FontName = "Arial";
HdrTxtInfo1.FontSize = 9;
HdrTxtInfo1.IsTrueTypeFontBold = true;
HdrTxtInfo1.Alignment = Aspose.Pdf.AlignmentType.Center;
HeaderText.TextInfo = HdrTxtInfo1;

Text txtSpacer = CreateSpacer1();

myHeader.Paragraphs.Add(imgTSLogo);
myHeader.Margin = ContractMarginInfo;

myHeader.Paragraphs.Add(txtSpacer);
myHeader.Paragraphs.Add(HeaderText);
myHeader.Paragraphs.Add(txtSpacer);

//let's define the footer as well...
FooterText = CreateFooterText("");
if (myeContractStatus != ServiceContractConst.ContractStatus.Completed)
{
myFooter.Paragraphs.Add(DraftText);
}

myFooter.Paragraphs.Add(FooterText);
myFooter.Margin = ContractMarginInfo;

}
if (mySectionName.Contains("OPENING AND MIDDLE"))
{

FirstPageText = new Text(mySectionText.Trim());
FirstPageText.Margin = ContractMarginInfo;
FirstPageText.TextInfo = ContractTextInfo;

FirstPageSection.Paragraphs.Add(FirstPageText);
FirstPageSection.PageInfo.Margin = ContractMarginInfo;

}
if (mySectionName.Contains("ADDENDUM A"))
{

//String myContent = AddendumAText.Segments[0].Content;
AddendumASection.IsNewPage = true;
PageTitleText = new Text(mySectionName.Trim());
PageTitleText.Margin = ContractMarginInfo;
PageTitleText.TextInfo = PageTitleTextInfo;
AddendumASection.Paragraphs.Add(PageTitleText);

AddendumAText = new Text(mySectionText.Trim());//.Replace("$", "").Trim());
AddendumAText.Margin = ContractMarginInfo;
AddendumAText.TextInfo = ContractTextInfo;
AddendumASection.Paragraphs.Add(AddendumAText);
AddendumASection.PageInfo.Margin = ContractMarginInfo;
}
if (mySectionName.Contains("ADDENDUM B"))
{

AddendumBSection.IsNewPage = true;
PageTitleText = new Text(mySectionName);
PageTitleText.Margin = ContractMarginInfo;
PageTitleText.TextInfo = PageTitleTextInfo;
AddendumBSection.Paragraphs.Add(PageTitleText);

AddendumBText = new Text(mySectionText);
AddendumBText.Margin = ContractMarginInfo;
AddendumBText.TextInfo = ContractTextInfo;
AddendumBSection.Paragraphs.Add(AddendumBText);
AddendumBSection.PageInfo.Margin = ContractMarginInfo;
}
if (mySectionName.Contains("ADDENDUM C"))
{

AddendumCSection.IsNewPage = true;
PageTitleText = new Text(mySectionName);
PageTitleText.Margin = ContractMarginInfo;
PageTitleText.TextInfo = PageTitleTextInfo;
AddendumCSection.Paragraphs.Add(PageTitleText);

AddendumCText = new Text(mySectionText);
AddendumCText.Margin = ContractMarginInfo;
AddendumCText.TextInfo = ContractTextInfo;
AddendumCSection.Paragraphs.Add(AddendumCText);
AddendumCSection.PageInfo.Margin = ContractMarginInfo;
}
if (mySectionName.Contains("ADDENDUM D"))
{
pdf.IsPageNumberRestarted = true;
AddendumDSection.IsNewPage = true;
AddendumDSection.IsPageNumberRestarted = true;

PageTitleText = new Text(mySectionName);
PageTitleText.IsHtmlTagSupported = true;
PageTitleText.Margin = ContractMarginInfo;
PageTitleText.TextInfo = PageTitleTextInfo;
AddendumDSection.Paragraphs.Add(PageTitleText);
AddendumDSection.Paragraphs.Add(CreateSpacer());
AddendumDText = new Text(mySectionText);
AddendumDText.Margin = ContractMarginInfo;
AddendumDText.TextInfo = ContractTextInfo;
AddendumDSection.Paragraphs.Add(AddendumDText);
AddendumDSection.PageInfo.Margin = ContractMarginInfo;
}
if (mySectionName.Contains("ADDENDUM D SIGNATURE"))
{
AddendumDTextSignature = new Text(mySectionText);
AddendumDTextSignature.Margin = ContractMarginInfo;
AddendumDTextSignature.TextInfo = ContractTextInfo;
//AddendumDSectionSignature.Paragraphs.Add(AddendumDTextSignature);
myDSectionFooterSignature.Paragraphs.Add(AddendumDTextSignature);
}
}

Int32 TotalNumPages = FirstPageSection.PageCount + AddendumASection.PageCount + AddendumBSection.PageCount + AddendumCSection.PageCount;

Int32 TotalNumPages2 = FirstPageSection.StartingPageNumber + AddendumASection.StartingPageNumber + AddendumBSection.StartingPageNumber + AddendumCSection.StartingPageNumber;

//now let's create this.....
FirstPageSection.OddHeader = myHeader;
FirstPageSection.EvenHeader = myHeader;
FirstPageSection.OddFooter = myFooter;
FirstPageSection.EvenFooter = myFooter;
pdf.Sections.Add(FirstPageSection);

AddendumASection.OddHeader = myHeader;
AddendumASection.EvenHeader = myHeader;
AddendumASection.OddFooter = myFooter;
AddendumASection.EvenFooter = myFooter;
pdf.Sections.Add(AddendumASection);

AddendumBSection.OddHeader = myHeader;
AddendumBSection.EvenHeader = myHeader;
AddendumBSection.OddFooter = myFooter;
AddendumBSection.EvenFooter = myFooter;
pdf.Sections.Add(AddendumBSection);


ArrayList alSectionNames = new ArrayList();
foreach (DictionaryEntry de in htSections)
{
String mySectionName = string.Empty;
mySectionName = de.Key.ToString();
alSectionNames.Add(mySectionName);
}

alSectionNames.Sort();

foreach (String SectName in alSectionNames)
{
//
if (SectName.Contains("Addendum C"))
{

AddendumCSection.OddHeader = myHeader;
AddendumCSection.EvenHeader = myHeader;
AddendumCSection.OddFooter = myFooter;
AddendumCSection.EvenFooter = myFooter;

pdf.Sections.Add(AddendumCSection);
}

if (SectName.Contains("Addendum D"))
{
pdf.IsPageNumberRestarted = true;
//let's define the addendum d's new footer...
String mySectionId = AddendumDSection.ID;
pdf.Sections.Add(AddendumDSection);

Int32 NumPages = pdf.Sections[mySectionId].PageCount;//AddendumDSection.PageCount;
DSectionFooterText = CreateFooterText(NumPages.ToString());
myDSectionFooter.Paragraphs.Add(DSectionFooterText);
if (myeContractStatus != ServiceContractConst.ContractStatus.Completed)
{
myDSectionFooter.Paragraphs.Add(DraftText);
}

AddendumDSection.OddHeader = myHeader;
AddendumDSection.EvenHeader = myHeader;
AddendumDSection.OddFooter = myDSectionFooter;
AddendumDSection.EvenFooter = myDSectionFooter;
AddendumDSection.AdditionalEvenFooter = myDSectionFooterSignature;
AddendumDSection.AdditionalOddFooter = myDSectionFooterSignature;
}
}
//

pdf.TextInfo = ContractTextInfo;
pdf.PageSetup.Margin = ContractMarginInfo;

if (myeContractStatus != ServiceContractConst.ContractStatus.Completed)
{
pdf.Watermarks.Add(watermark1);
}

PageSetup myPageSetup = new PageSetup();
myPageSetup.Margin = ContractMarginInfo;

return pdf;

}

private Text CreateFooterText(String NumPages)
{
String FooterText = string.Empty;
if (String.IsNullOrEmpty(NumPages))
{
String PageNum = "$P";

FooterText = "CONFIDENTIAL Page: $p of $P INITIALS_____";
}
else
{
FooterText = "CONFIDENTIAL Amendment Page: $Sp of $SP INITIALS_____";//Contract Page: $P of $P,
}

Text txtfooter = new Text(FooterText);
txtfooter.TextInfo = DefineTextInfo("Arial", 8, false, AlignmentType.Center);
return txtfooter;

//$P
// Displays total number of pages in the document

//$p
// Displays the page number of current page in the document

//$Sp
// Displays page number in the current section

//$SP
// Displays page count of the current section

//$SN
// Displays section index of the section in the document

//$D
// Displays date and time

//#$NL
// Forces beginning a new line

//#$NP
// Forces beginning a new page

//#$NC
// Forces the text after the symbol to be rendered in next column

//#$TAB
// Sets tab at the start of a segment

//#$REFPAGE(paragraphID)
// Indicates the page number of a specified paragraph

//#$UNICODE(unicodeValue)
// The number in the macro will be translated into Unicode character. For example, #$UNICODE(9633) will be rendered as '?'.

}

Hi,

We're working over this query and will get back to you shortly. We apologize for your inconvenience.

Hello Darlene,

Sorry for replying you so late.

I'm afraid currently there is not such property available in Section class which can return the last page number in a particular section. For the sake of implementation, I've logged it as "New Feature" in our issue tracking system as PDFNET-15005. We'll look into the details of this requirement and will keep you updated on the status of correction.

Regarding Section.PageCount property, it only returns the value once the PDF document is saved.

We're sorry for your inconvenience.

Hello Darlene,

Thanks for your patience.

We have further investigated the issue that you have reported earlier that "pdf.Sections[mySectionId].PageCount always returns zero.". The PageCount cannot be counted before saving a document because whole document structure processing starts when Save is calling. After the document was saved, PageCount works correctly. You may also test the scenario with the following code snippet where the dialog box displays proper page count for Section2.

[C#]

Pdf pdf = new Pdf();
Aspose.Pdf.Section sec1 = pdf.Sections.Add();
sec1.Paragraphs.Add(new Text("Hello Word in section 1"));

Aspose.Pdf.Section sec2 = pdf.Sections.Add();
for (int i = 0; i <= 200; i++)
{
sec2.Paragraphs.Add(new Text("line - " + i.ToString()));
}

sec2.Paragraphs.Add(new Text(sec2.PageCount.ToString()));
//Save the pdf document
pdf.Save(@"d:/pdftest/LineSpacing_FormattingIssue_4.6.0.pdf");
// get the page count of Section2
MessageBox.Show(sec2.PageCount.ToString());

In case you have any further query, please feel free to contact. We apologize for the delay and inconvenience.

Hello,

I tried this code in VB.net (version 4.9)

sec1.PageCount = 0<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Here is the code. Please let me know what should be changed.

Dim pdf As New Pdf

Dim sec1 As Aspose.Pdf.Section = pdf.Sections.Add

sec1.Paragraphs.Add(New Text("Hello Word in section 1"))

For i As Integer = 0 To 200

sec1.Paragraphs.Add(New Text("line - " + i.ToString()))

Next

sec1.Paragraphs.Add(New Text(sec1.PageCount.ToString()))

pdf.Save(path)

Hello Igor,

Thanks for using our products.

The PageCount cannot be counted before saving a document because whole document structure processing starts when Save is calling. After the document was saved, PageCount works correctly. If you call the same property Section.PageCount once Pdf.Save(..) method has been executed, you will get the correct result. In case I have not properly understood your requirements or you have any further query, please feel free to contact. We apologize for any confusion and inconvenience.

Thanks for the reply. It is working after Pdf.Save(..) method. PDF document that I need to create could have 1000 of pages. It takes time to save document to the drive. I am trying to avoid extra IO operations. Is it possible to know the PDF.PageCount before Pdf.Save(..) method has been executed?

When I use Pdf.Save(..) first time, and have ("Page $p of $P ") in the footer of the document, $P works. So, why PDF.PageCount or sec1.PageCount doesn’t work?

Also, in the footer of the document I have:

Dim txt As Text = New Text("Page $p of $P ")<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

footer.Paragraphs.Add(txt)

After I use Pdf.Save(..) method second time, created document has double page number like

Page 4 of 2

Should I restart paging before second Pdf.Save(..)?

Here is my code:

Dim pdf As New Pdf

Dim sec1 As Aspose.Pdf.Section = pdf.Sections.Add

sec1.Paragraphs.Add(New Text("Hello Word in section 1"))

For i As Integer = 0 To 100

sec1.Paragraphs.Add(New Text("line - " + i.ToString()))

Next

sec1.Paragraphs.Add(New Text(sec1.PageCount.ToString()))

sec1.Paragraphs.Add(New Text(pdf.PageCount.ToString()))

Dim footer As Aspose.Pdf.HeaderFooter = New Aspose.Pdf.HeaderFooter(sec1)

sec1.OddFooter = footer

sec1.EvenFooter = footer

Dim txt As Text = New Text("Page $p of $P ")

footer.Paragraphs.Add(txt)

pdf.Save(path)

sec1.Paragraphs.Add(New Text(sec1.PageCount.ToString()))

sec1.Paragraphs.Add(New Text(pdf.PageCount.ToString()))

pdf.Save(path)

Hello Igor,

Thanks for using our products.

As I have shared earlier, I am afraid you cannot get the page count information for whole PDF or Section, before saving the document.

Regarding the other query on invalid page count information when calling Pdf.Save(...) method twice, I am able to notice this problem and we are looking into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

The issues you have found earlier (filed as 24252 ) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.