Free Support Forum - aspose.com

Footer alignment issues

Hi,

As this is my first pdf document that I'm generating using Aspose.PDF, I would appreciate any tips I can get.

I'm trying to add a footer to my document with the date generated at the left side of the page, some text in the center, and the pagenumbers at the far right. But I can't see to get the alignment to behave accordingly, everything comes out smashed together in the center.

Here's my footer code:

Dim

hf2 As Aspose.Pdf.Generator.HeaderFooter = New Aspose.Pdf.Generator.HeaderFooter(sec1)

'Set the HeaderFooter object to odd footer

sec1.OddFooter = hf2

'Set the HeaderFooter object to even footer

sec1.EvenFooter = hf2

'Add Distance From Edge Property of header to 150 unit Points

hf2.DistanceFromEdge = 150

hf2.Margin.Bottom = 70

'Add a text paragraph containing current page number of total number of pages

Dim t3 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text

Dim seg0 As Aspose.Pdf.Generator.Segment = t3.Segments.Add()

Dim seg1 As Aspose.Pdf.Generator.Segment = t3.Segments.Add()

Dim seg2 As Aspose.Pdf.Generator.Segment = t3.Segments.Add()

seg0.Content =

"Generated at $D"

seg0.DateFormat =

"g"

seg0.TextInfo.Color =

New Aspose.Pdf.Generator.Color("Grey")

seg0.TextInfo.IsTrueTypeFontBold =

False

seg0.TextInfo.Alignment = Aspose.Pdf.Generator.

AlignmentType.Left

seg1.Content =

"My Text"

seg1.TextInfo.Color =

New Aspose.Pdf.Generator.Color("Black")

seg1.TextInfo.IsTrueTypeFontBold =

True

seg1.TextInfo.Alignment = Aspose.Pdf.Generator.

AlignmentType.Center

seg2.Content =

"Page $p of $P"

seg2.TextInfo.Color =

New Aspose.Pdf.Generator.Color("Grey")

seg2.TextInfo.IsTrueTypeFontBold =

False

seg2.TextInfo.Alignment = Aspose.Pdf.Generator.

AlignmentType.Right

'Set Font Size

seg0.TextInfo.FontSize = 10

seg1.TextInfo.FontSize = 10

seg2.TextInfo.FontSize = 10

'Set Font Name

seg0.TextInfo.FontName =

"Arial"

seg1.TextInfo.FontName =

"Arial"

seg2.TextInfo.FontName =

"Arial"

hf2.Paragraphs.Add(t3)

v

Alternatively, I tried doing this with html code, the layout comes out good, but the replaceable fields are not getting replaced.

'Create a HeaderFooter object for the section

Dim hf2 As Aspose.Pdf.Generator.HeaderFooter = New Aspose.Pdf.Generator.HeaderFooter(sec1)

'Set the HeaderFooter object to odd footer


sec1.OddFooter = hf2

'Set the HeaderFooter object to even footer

sec1.EvenFooter = hf2

'Add Distance From Edge Property of header to 50 unit Points

hf2.DistanceFromEdge = 50

hf2.Margin.Bottom = 75

'Add a text paragraph containing current page number of total number of pages

'Create string variables with text containing html tags

Dim string5 As String = "

" & _

"

" & _

"

" & _

"

Generated at $D My Footer Text Page $p of $P
"

'Create text paragraphs containing HTML text

Dim t3 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(string5)

'Add the text paragraphs containing HTML text to the header section

t3.Segments(0).DateFormat =

"g"

hf2.Paragraphs.Add(t3)

t3.IsHtmlTagSupported =

True

please advise which of these is best way to go, and how to get it to display with correct values and in correct layout.

Thanks,

-S. Abowitz

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Sorry for the delay in response.

Well, TextInfo.Alignment cannot be applied to Segment object. The documentation states that “Gets or sets AlignmentType that indicates the text alignment mode. This property should be set for the Text object but not Segment object.”

Now, to achieve your desired results, you can use Table inside the footer. Please see the following sample code as per your requirements.

'Instantiate a Pdf object by calling its empty constructor

Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()

'Create a section in the Pdf object

Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()

Dim hf2 As Aspose.Pdf.Generator.HeaderFooter = New Aspose.Pdf.Generator.HeaderFooter(sec1)

'Set the HeaderFooter object to odd footer

sec1.OddFooter = hf2

'Set the HeaderFooter object to even footer

sec1.EvenFooter = hf2

'Add Distance From Edge Property of header to 150 unit Points

hf2.DistanceFromEdge = 150

hf2.Margin.Bottom = 70

'Add a text paragraph containing current page number of total number of pages

Dim t1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text

Dim t2 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text

Dim t3 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text

Dim seg0 As Aspose.Pdf.Generator.Segment = t1.Segments.Add()

Dim seg1 As Aspose.Pdf.Generator.Segment = t2.Segments.Add()

Dim seg2 As Aspose.Pdf.Generator.Segment = t3.Segments.Add()

seg0.Content = "Generated at $D"

seg0.DateFormat = "g"

seg1.Content = "My Text"

seg2.Content = "Page $p of $P"

'Instantiate a table object

Dim tab1 As Aspose.Pdf.Generator.Table = New Aspose.Pdf.Generator.Table()

'Add the table in paragraphs collection of the desired section

hf2.Paragraphs.Add(tab1)

'Set default cell border using BorderInfo object

tab1.DefaultCellBorder = New Aspose.Pdf.Generator.BorderInfo(Aspose.Pdf.Generator.BorderSide.All, 0.1F)

'Set with column widths of the table

tab1.ColumnWidths = "150 150 150"

'Create rows in the table and then cells in the rows

Dim row1 As Aspose.Pdf.Generator.Row = tab1.Rows.Add()

row1.Cells.Add()

row1.Cells.Add()

row1.Cells.Add()

' set the vertical allignment of the text as center alligned

row1.Cells(0).Alignment = Generator.AlignmentType.Left

row1.Cells(1).Alignment = Generator.AlignmentType.Center

row1.Cells(2).Alignment = Generator.AlignmentType.Right

row1.Cells(0).Paragraphs.Add(t1)

row1.Cells(1).Paragraphs.Add(t2)

row1.Cells(2).Paragraphs.Add(t3)

'Save the Pdf

pdf1.Save("D:\\AP Data\\November2012\\image2pdf.pdf")

You may also check the following documentation link for further details.

Table in Header Footer section

Thank You & Best Regards,

Hi,

Thanks for the tip on alignments and the sample code on using a table. I implemented that in my code and it worked great!

Thanks again,

-S. Abowitz