Hi,
Thank you for your feedback.
Our product supports adding date at the end of the page, I put the complete code below for your reference:
- Code: Select all
// Create a new PDF document
PdfDocument doc = new PdfDocument();
// Set the size of the pages to A4
doc.PageSettings.Size = PdfPageSize.A4;
// Set the margins of the pages to 0
doc.PageSettings.Margins = new PdfMargins(0);
// Create a new PdfMargins object with custom values for top, bottom, left, and right margins
PdfMargins margins = new PdfMargins(40, 60, 40, 60);
// Set the bottom template of the document to the created footer template
doc.Template.Bottom = CreateFooterTemplate(doc, margins);
// Set the left template of the document to a new PdfPageTemplateElement with the specified margins and page size height
doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
// Set the right template of the document to a new PdfPageTemplateElement with the specified margins and page size height
doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);
// Add a new page to the document
PdfPageBase page1 = doc.Pages.Add();
// Add another new page to the document
PdfPageBase page2 = doc.Pages.Add();
// Create a new PdfTrueTypeFont object with the specified font name, size, and style
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("华文楷体", 15f, FontStyle.Bold), true);
// Save the document to a file named "HeaderAndFooter7.pdf"
doc.SaveToFile(@"..\HeaderAndFooter7.pdf");
}
// Define a static method to create a footer template for the document
static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins)
{
// Get the size of the page settings
SizeF pageSize = doc.PageSettings.Size;
// Create a new PdfPageTemplateElement with the specified width and bottom margins
PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
// Set the foreground color of the footer space to false
footerSpace.Foreground = false;
// Initialize the x and y coordinates for drawing the line
float x = margins.Left;
float y = 0;
// Create a new PdfPen with a gray brush and a thickness of 0.5
PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
// Draw a line from the top-left corner to the bottom-right corner of the footer space
footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);
// Increment the y coordinate by 5
y = y + 5;
// Create a new PdfTrueTypeFont object with the specified font name, size, and style
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("黑体", 10f, FontStyle.Bold), true);
// Create a new PdfStringFormat object with the specified text alignment
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
// Get the current date and time
DateTime s1 = DateTime.Now;
// Create a string containing the date and time information
String footerText = "日期:" + s1;
// Draw the footer text on the footer space using the specified font, brush, x and y coordinates, and format
footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Black, x, y, format);
// Return the created footer space
return footerSpace;
}
If the issue still exists, please offer your file to help us do further investigation, you can attach here or send it to us via email (
[email protected] ). Thank you in advance.
Sincerely,
Ula
E-iceblue support team