How to add page number to footer

how to add “page of total page” to pdf document footer?


the total page is doc.Pages.Count.
how to get current page like 1 of 20, 2 of 20… on the footer of each page if i have a 20 page document.
i am using vb.net

thanks,
Ying

Hi Ying,

Thanks for your inquiry. You can easily add page number stamp in header/footer using PageNumberStamp() object. Please check following sample code snippet for the purpose.

' Open the document
Dim pdfDocument As New Document("PageNumber.pdf")

' Create a page number stamp
Dim pageNumberStamp As New PageNumberStamp() With {
    .Background = False,
    .Format = "Page # of " & pdfDocument.Pages.Count,
    .BottomMargin = 10,
    .HorizontalAlignment = HorizontalAlignment.Center,
    .StartingNumber = 1
}

' Set text properties
pageNumberStamp.TextState.Font = FontRepository.FindFont("Arial")
pageNumberStamp.TextState.FontSize = 14F
pageNumberStamp.TextState.FontStyle = FontStyles.Bold Or FontStyles.Italic
pageNumberStamp.TextState.ForegroundColor = Aspose.Pdf.Color.Aqua

' Add the stamp to each page
For Each page As Aspose.Pdf.Page In pdfDocument.Pages
    page.AddStamp(pageNumberStamp)
Next

' Save the output document
pdfDocument.Save("Footer_output.pdf")

Please feel free to contact us for any further assistance.

Best Regards,

thanks for the response. sorry i did not make my question clear. i have a footer table and three columns in it. i can’t add the pageNumberStamp in table cell.

below is the sample code
thanks,

Dim footerCell1 As Aspose.Pdf.Generator.Cell = footerRow.Cells.Add(“Confidential”)

Dim footerCell5 As Aspose.Pdf.Generator.Cell = footerRow.Cells.Add(pageNumberStamp)
Dim footerCell11 As Aspose.Pdf.Generator.Cell = footerRow.Cells.Add("Printed on: " + currentTime)

Hi Ying,

Thanks for your feedback. It is recommended to use new generator(Aspose.Pdf for .NET) for creating PDF from scratch instead old generator(Aspose.Pdf.Generator), it is more efficient and improved. Please check following code snippet to add page number stamp while creating a new PDF document, hopefully it will help you to accomplish the task.

// Create a new PDF document
Document pdf = new Document();

// Add a page to the document
Aspose.Pdf.Page page = pdf.Pages.Add();

// Create a table for the footer
Aspose.Pdf.Table footerTable = new Aspose.Pdf.Table();
footerTable.ColumnWidths = "33% 33% 33%";

// Add a row to the footer table
Aspose.Pdf.Row footerRow = footerTable.Rows.Add();
footerRow.Cells.Add("Aspose.PDF for .NET");
footerRow.Cells.Add("center");
footerRow.Cells.Add("page $p / $P");

// Create and configure a footer
Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter();
footer.Paragraphs.Add(footerTable);
page.Footer = footer;

// Create a table for the main content
Aspose.Pdf.Table contentTable = new Aspose.Pdf.Table();
contentTable.Border = new Aspose.Pdf.BorderInfo(
    Aspose.Pdf.BorderSide.All, 0.5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)
);
contentTable.DefaultCellBorder = new Aspose.Pdf.BorderInfo(
    Aspose.Pdf.BorderSide.All, 0.5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)
);

// Add rows and cells to the content table
for (int rowCount = 1; rowCount <= 100; rowCount++)
{
    // Add a new row
    Aspose.Pdf.Row contentRow = contentTable.Rows.Add();
    contentRow.IsRowBroken = true;

    // Add cells to the row
    contentRow.Cells.Add($"table2 Column ({rowCount}, 1)");
    contentRow.Cells.Add($"table2 Column ({rowCount}, 2)");
    contentRow.Cells.Add($"table2 Column ({rowCount}, 3)");
}

// Add the content table to the page
page.Paragraphs.Add(contentTable);

// Save the PDF document
pdf.Save(@"E:\Data\headtable.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

thanks for the response. i got my footer working.

as you suggested. i am using “aspose.pdf”. however without the generator my code to add image is not working. could you send the sample code for that?
i have need to put image to a header table so it will appear on every page. thanks.

Dim doc As Document = New Document()
Dim page As Aspose.Pdf.Page = doc.Pages.Add()
Dim imglogo As New Aspose.Pdf.Image

imglogo.ImageInfo.File = “C:\OMA\AgendaTrusteeLetter\AgendaTrusteeLetter\uphslogo.gif” (does not work)
imglogo.ImageFileType = Aspose.Pdf.ImageFileType.Gif (does not work)

Dim header As Aspose.Pdf.HeaderFooter = New Aspose.Pdf.HeaderFooter()
Dim headerTable As New Aspose.Pdf.Table()
headerTable.ColumnWidths = “80 300 120”
headerTable.DefaultCellPadding.Top = 2

‘’ add logo
Dim headerRow As Aspose.Pdf.Row = headerTable.Rows.Add()
‘’ headerRow.Border = New Aspose.Pdf.BorderInfo(BorderSide.Bottom, 0.5F, Aspose.Pdf.Color(1))
Dim cellimglogo As Aspose.Pdf.Cell = headerRow.Cells.Add()
cellimglogo.Paragraphs.Add(imglogo)
cellimglogo.VerticalAlignment = VerticalAlignment.Bottom
Dim cellimgempty As Aspose.Pdf.Cell = headerRow.Cells.Add("")
Dim cellimgtxt As Aspose.Pdf.Cell = headerRow.Cells.Add(“Office of Medical Affairs " & " PEER REVIEW ONLY”)
cellimgtxt.Alignment = HorizontalAlignment.Right
cellimgtxt.VerticalAlignment = VerticalAlignment.Center
cellimgtxt.DefaultCellTextState.FontSize = “10”



additional to add image to header table issue, without the generator, my add string is not working also,
could you send me the code for add string to table cell too?

thanks,
Ying

here is what i have which is working with generator.
Dim s1 As String = "hello"
Dim text1 As Aspose.Pdf.Generator.Text = New Generator.Text(s1)
text1.IsHtmlTagSupported = True
text1.TextInfo.IsUnicode = True
Dim bottomrow9 As Aspose.Pdf.Generator.Row = EndTable.Rows.Add()
bottomrow9.Cells.Add().Paragraphs.Add(text1)

Hi,

please see my posts above i have two issues both relate to using aspose.pdf. i got them work with generator before.
one is add image to table cell, another one is add text to table cell.
thanks
Ying

Hi Ying,

Thanks for sharing the details.

ImageInfo class is part of Aspose.Pdf.Generator namespace and is not available in new Document Object Model of Aspose.Pdf namespace. So when using new DOM, please try using following code snippet to accomplish your requirement. For your reference, I have also attached the output generated over my end.

[VB.NET]

' Create a new PDF document
Dim doc As New Document()

' Add a page to the document
Dim page As Aspose.Pdf.Page = doc.Pages.Add()

' Create an image object for the logo
Dim imglogo As New Aspose.Pdf.Image()
imglogo.File = "C:\pdftest\aspose-Pdf-for-net.png"
imglogo.FixWidth = 100
imglogo.FixHeight = 80

' Create a header for the page
Dim header As New Aspose.Pdf.HeaderFooter()

' Create a table to structure the header content
Dim headerTable As New Aspose.Pdf.Table()
headerTable.ColumnWidths = "80 120 300"

' Add a row to the header table
Dim headerRow As Aspose.Pdf.Row = headerTable.Rows.Add()

' Add the logo to the first cell
Dim cellimglogo As Aspose.Pdf.Cell = headerRow.Cells.Add()
cellimglogo.Paragraphs.Add(imglogo)
cellimglogo.VerticalAlignment = VerticalAlignment.Bottom

' Add an empty cell for spacing
Dim cellimgempty As Aspose.Pdf.Cell = headerRow.Cells.Add("")

' Add the text to the third cell
Dim cellimgtxt As Aspose.Pdf.Cell = headerRow.Cells.Add("Office of Medical Affairs PEER REVIEW ONLY")
cellimgtxt.Alignment = HorizontalAlignment.Right
cellimgtxt.VerticalAlignment = VerticalAlignment.Center

' Set the font size for the text
cellimgtxt.DefaultCellTextState.FontSize = 10

' Add the table to the header
header.Paragraphs.Add(headerTable)

' Assign the header to the page
page.Header = header

' Save the resultant PDF file
doc.Save("C:\pdftest\Image_In_Header.pdf")

thanks for you response

how about add text to cell?

thanks

here is what i have which is working with generator.
Dim s1 As String = “hello”
Dim text1 As Aspose.Pdf.Generator.Text = New Generator.Text(s1)
text1.IsHtmlTagSupported = True
text1.TextInfo.IsUnicode = True
Dim bottomrow9 As Aspose.Pdf.Generator.Row = EndTable.Rows.Add()
bottomrow9.Cells.Add().Paragraphs.Add(text1)

Hi Ying,

Thanks for sharing the details.

In order to add text to table cell, you can create TextFragment instance and then add it to paragraphs collection of table cell.

[VB.NET]

Dim cellimgempty As Aspose.Pdf.Cell = headerRow.Cells.Add(“”)
Dim fragment As Aspose.Pdf.Text.TextFragment = New TextFragment("Middle Text")
cellimgempty.Paragraphs.Add(fragment)
Hi ,
i have three questions:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
i have below code which was working with 2009 version of pdf.
they don't work with Dim doc As Document = New Document() Dim page As Aspose.Pdf.Page = doc.Pages.Add()

ActionTable.DefaultCellPadding.Right = 2 (not working)

Dim Actionrow1 As Row = ActionTable.Rows.Add()
Dim Actionrow1col1 As Cell = Actionrow1.Cells.Add("ACTION:")
Dim Actionrow1col2 As Cell = Actionrow1.Cells.Add(TrusteeActionwording & " on " & BoardDate & ".")
Actionrow1col1.DefaultCellTextInfo.FontSize = FontSize.Medium
Actionrow1col1.DefaultCellTextInfo.FontName = "Arial Black" (not working)
Actionrow1col1.Alignment = HorizontalAlignment.Center
Actionrow1col1.DefaultCellTextInfo.IsTrueTypeFontBold = True (not working)
could you give me the code to set the text font? you can tell that they are in a table cell. thanks.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
"#$NL" used to put the text to the next line, what is the code now? below does not work anymore.
Dim celltext As Cell = toptablerow.Cells.Add(drHeader("Facilityname") + "#$NL" + committeetitle + " Minutes " + "#$NL" + "*CONFIDENTIAL/PEER REVIEW ONLY*" + "#$NL" + CommitteeJustDate + spmeeting)
i can use
, but how to make the pdf understand html?
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
how to set the margin of a page. my pdf is not at the center of the page. left margin is too wide.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
thanks,
YIng
found how to set bold.

DateRowcol.DefaultCellTextState.FontStyle = Text.FontStyles.Bold

Hi there,

Thanks for your inquiry. Please check following sample code, it addresses your above all question. Hopefully it will resolve the issue.

Dim doc As New Document()

Dim page As Aspose.Pdf.Page = doc.Pages.Add()

' Set page margin

page.PageInfo.Margin.Bottom = 10

page.PageInfo.Margin.Top = 10

page.PageInfo.Margin.Left = 10

page.PageInfo.Margin.Right = 10

Dim ActionTable As New Aspose.Pdf.Table()

Dim margin As New Aspose.Pdf.MarginInfo()

margin.Top = 2F

ActionTable.DefaultCellPadding = margin

Dim Actionrow1 As Aspose.Pdf.Row = ActionTable.Rows.Add()


Dim Actionrow1col1 As Aspose.Pdf.Cell = Actionrow1.Cells.Add("Electronic Supporting " & vbCr & vbLf & "ACS Web Society " & vbCr & vbLf & "holds")

Actionrow1col1.DefaultCellTextState.FontSize = 12

Actionrow1col1.DefaultCellTextState.Font = FontRepository.FindFont("Arial Black")

Actionrow1col1.Alignment = HorizontalAlignment.Center

Actionrow1col1.DefaultCellTextState.FontStyle = FontStyles.Bold

page.Paragraphs.Add(ActionTable)

doc.Save("testlinebreak.pdf")

Please feel free to contact us for any further assistance.

Best Regards,

thank you! it resolved my text format issues. my page is in shape.

how do i move my header table to the left?
my page margin is
page.PageInfo.Margin.Bottom = 20
page.PageInfo.Margin.Top = 70
page.PageInfo.Margin.Left = 35
page.PageInfo.Margin.Right = 35

from the top my header is at the right place, but it seems the header moved at least 70 from right. rest of the tables in the page is starts at 35. do i need to set the margin for the header and footer?

thank you very much for your help!
Ying


Hi,

i found the way to move my header to the left by setting the margin. my new issue is that i have tables in a table. the table1 does not fill the page, the table2 starts from a new page. it leaves a big blank space on the page above. what do i need to do so the table2 will add to the page directly after table1 without any space in between. all my table margin is 2,

thanks,
Yinf

Hi Yinf,

Thanks for sharing the details. Can you please share some sample project, so that we can test the scenario in our environment. We are sorry for your inconvenience.

Hi,
thank you for get back to me so quick.
i set tablename.IsInNewPage = False to every table. it dose not work of the last table.

the margin for all the tables are 2.0F

’‘boxedtable is outer table with border width 528
BoxedTable.DefaultCellBorder = New BorderInfo(BorderSide.All, 0.2F)

’‘innertable inside the boxedtable, all tables are inside innertable width 524
innertable.DefaultCellBorder = New BorderInfo(BorderSide.All, 0.0F)

all other tables width=520
’’ first table inside innertable
commTable.DefaultCellBorder = New BorderInfo(BorderSide.All, 0.0F)
Dim innertableRow As Row = innertable.Rows.Ad
innertableRow.Cells.Add.Paragraphs.Add(commTable)


’’ all tables below have lot of rows in them, they could end in the middle of the page, and the one after it starts at the new page, not directly after it. all tables are built in separate subroutine

’’ second table inside innertable this is the table has lots of row in it and could end at the middle of the page
ProviderTable.DefaultCellBorder = New BorderInfo(BorderSide.All, 0.0F)
Dim innertableRowRowTemp As Row = innertable.Rows.Add()
innertableRowRowTemp.Cells.Add.Paragraphs.Add(ProviderTable)

’’ term table is the third table inside the innertable.
Dim innertableRowTerm As Row = innertable.Rows.Add()
innertableRowTerm.Cells.Add.Paragraphs.Add(TermLable)

’’ other table is the fourth table inside the innertable
Dim innertableRowotherLable As Row = innertable.Rows.Add()
innertableRowotherLable.Cells.Add.Paragraphs.Add(otherLable)

thanks,
Ying

Hi Ying,


Thanks for sharing the code snippet.

I have tried replicating the issue but it appears that you have too many tables defined in above code snippet (innertable, ProviderTable, commTable). Can you please share the sample project which can help us in replicating the issue in our environment. We are sorry for this inconvenience.

Hi Ying,


Thanks for sharing the code snippet.

I have tried executing the code but it appears that you have some custom SQL and other data binding objects defined i.e. ConfigurationManager etc. Can you please share some sample project, so that we can again try replicating the issue in our environment. We are sorry for this delay and inconvenience.

thanks for your response.

not sure what i can provide this time. you will need my database to query the records and fill the table. the ConfigurationManager is in the web.config

spname = ConfigurationManager.ConnectionStrings(“SPGetTurstee”).ConnectionString

<add name=“ApplicationServices”
connectionString=“data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true”
providerName=“System.Data.SqlClient” />
<add name=“MeetingAgenda” connectionString=“Data Source= ;Initial Catalog=PennAgenda;Persist Security Info=True;User ID=EmailWithAttachment;Password=”
providerName=“System.Data.SqlClient” />