how to add “page of total page” to pdf document footer?
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.
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.
Hi,
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
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)
, but how to make the pdf understand html?
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.
Hi,
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 Ying,
Hi Ying,
thanks for your response.