static void Main(string[] args)
{
// Define the HTML content to convert to PDF
string htmls = "";
// Create a MemoryStream to hold the generated PDF data
MemoryStream stream = new MemoryStream();
// Set the margins for the PDF document
PdfMargins margins = new PdfMargins(40, 40, 40, 40);
// Set the path to the HTML converter plugins
string pluginPath = "F:\\Libraries\\plugins-windows-x64\\plugins";
HtmlConverter.PluginPath = pluginPath;
// Convert the specified HTML content to PDF and write it to the MemoryStream
HtmlConverter.Convert(htmls, stream, true, 100000, new Size(1080, 1000), margins, LoadHtmlType.SourceCode);
// Create a new PdfDocument object and load the converted PDF from the MemoryStream
PdfDocument doc = new PdfDocument();
doc.LoadFromStream(stream);
// Get the page margins from the PDF document
PdfMargins margin = doc.PageSettings.Margins;
// Draw page numbers on each page of the PDF document
DrawPageNumber(doc, margin, 1, doc.Pages.Count);
// Save the PDF document to a file named "result.pdf"
doc.SaveToFile("result.pdf");
}
// Method for drawing page numbers on each page of the PDF document
private static void DrawPageNumber(PdfDocument doc, PdfMargins margin, int startNumber, int pageCount)
{
// Iterate through each page in the document
foreach (PdfPageBase page in doc.Pages)
{
// Set transparency for the canvas
page.Canvas.SetTransparency(0.5f);
// Define brush and pen styles for drawing
PdfBrush brush = PdfBrushes.Black;
PdfPen pen = new PdfPen(brush, 0.75f);
// Define font style for the page number label
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Italic), true);
// Define string format for aligning the page number label
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
format.MeasureTrailingSpaces = true;
// Calculate spacing and positioning for the page number label
float space = font.Height * 0.75f;
float x = margin.Left;
float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
float y = page.Canvas.ClientSize.Height - margin.Bottom + space;
// Draw a horizontal line below the page number label
page.Canvas.DrawLine(pen, x, y, x + width, y);
// Increment the y position and format the page number label
y = y + 1;
String numberLabel = String.Format("{0} of {1}", startNumber++, pageCount);
// Draw the page number label on the canvas
page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format);
// Reset the transparency for the canvas
page.Canvas.SetTransparency(1);
}
}