In the previous topic, we have illustrated the replaceable symbols available in Aspose.Pdf and also their descriptions. Now, we are going to demonstrate the usage of these symbols.
Display Current or Total No. of Pages
$p and $P are used to deal with the page numbering at run time. $p is replaced with the number of the page where the current Paragraph is in. $P is replaced with the total page number of the document.
The typical usage is to set page number in the footer or header as the following example shows:
Code Snippet
[C#]
...
//Create a section in the pdf object
Section sec1 = pdf1.Sections.Add();
//Create a HeaderFooter object for the section
HeaderFooter hf = new HeaderFooter(sec1);
//Set the HeaderFooter object to odd and even footers
sec1.OddFooter = sec1.EvenFooter = hf;
//Add a text paragraph containing current page number of total number of pages
hf.Paragraphs.Add(new Text(hf,"page $p of $P"));
...
[VB.NET]
...
'Create a section in the pdf object
Dim sec1 As Section = pdf1.Sections.Add()
'Create a HeaderFooter object for the section
Dim hf As HeaderFooter = New HeaderFooter(sec1)
'Set the HeaderFooter object to odd footer
sec1.OddFooter = hf
'Set the HeaderFooter object to even footer
sec1.EvenFooter = hf
'Add a text paragraph containing current page number of total number of pages
hf.Paragraphs.Add(New Aspose.Pdf.Text(hf, "page $p of $P"))
...
[XML]
<Section>
<Footer>
<Text>
<Segment>page $p of $P</Segment>
</Text>
</Footer>
...
</Section>
Display Current Date
The $D is used to display the date and time when the document is generated. Similar to above example, it is possible to use $D symbol in any Text Segment where it is needed to display the date and time.
Aspose.Pdf also provides the flexibility to display date in different formats. DateFormat property of the Segment class is used to set the date format. There are different date format settings that can be assigned to DateFormat property of Segment class as a string.
The default value is "d" that displays date as 05/08/2004.
The following figure shows different date format setting and their effects:
|
Line & Page Breaks
The #$NL is used to force begining a new line and the #$NP is used to force begining a new page.
Column Breaking
The #$NC is used to force the Text after the symbol to be rendered into a next column.
Add Tab at the Start of a Segment
The #$TAB is used to set Tab at the start of a Segment . If Tab is used directly at the beginning of a Segment , the XML parser will ignore it.
Get the Page Number of a Specified Paragraph
This symbol #$REFPAGE(paragraphID) indicates the page number of a specified paragraph. For example, #$REFPAGE(title) indicates the page number of the paragraph with ID "title".
The typical usage is shown by the following examples :
Code Snippet
[XML]
<?xml version="1.0" encoding="utf-8" ?>
<Pdf xmlns="Aspose.Pdf">
<Section>
<Text ID="page1"><Segment>page 1</Segment></Text>
<Text>
<Segment>The last page is #$REFPAGE(page4).</Segment>
</Text>
<Text ID="page2" IsFirstParagraph="True"><Segment>page 2</Segment></Text>
<Text ID="page3" IsFirstParagraph="true"><Segment>page 3</Segment></Text>
</Section>
<Section>
<Text ID="page4"><Segment>page 4</Segment></Text>
</Section>
</Pdf>
[C#]
Pdf pdf1 = new Pdf();
Section sec1 = pdf1.Sections.Add();
Text text1 = new Text("page 1");
text1.ID = "page1";
sec1.Paragraphs.Add(text1);
sec1.Paragraphs.Add(new Text("The last page is #$REFPAGE(page4)."));
Text text2 = new Text("page 2");
text2.ID = "page2";
text2.IsFirstParagraph = true;
sec1.Paragraphs.Add(text2);
Text text3 = new Text("page 3");
text3.ID = "page3";
text3.IsFirstParagraph = true;
sec1.Paragraphs.Add(text3);
Section sec2 = pdf1.Sections.Add();
Text text4 = new Text("page 4");
text4.ID = "page4";
sec2.Paragraphs.Add(text4);
pdf1.Save("d:/test/test.pdf");
[VisualBasic]
Dim pdf1 As Pdf = New Pdf()
Dim sec1 As Section = pdf1.Sections.Add()
Dim text1 As Text = New Text("page 1")
text1.ID = "page1"
sec1.Paragraphs.Add(text1)
sec1.Paragraphs.Add(New Text("The last page is #$REFPAGE(page4)."))
Dim text2 As Text = New Text("page 2")
text2.ID = "page2"
text2.IsFirstParagraph = True
sec1.Paragraphs.Add(text2)
Dim text3 As Text = New Text("page 3")
text3.ID = "page3"
text3.IsFirstParagraph = True
sec1.Paragraphs.Add(text3)
Dim sec2 As Section = pdf1.Sections.Add()
Dim text4 As Text = New Text("page 4")
text4.ID = "page4"
sec2.Paragraphs.Add(text4)
pdf1.Save("d:/test/test.pdf")