How to apply border for each page in HTML to PDF

Hello Support,

We are using aspose.pdf to convert HTML to PDF. when we create PDF from HTML with dynamic content, it generating pdf with 4-5 pages. Now we wanted to add border to each indevidual pages. which is critical requirenment from client. Can you please sugges how to achive using aspose.pdf. we are using 23.5 version.

Please suggest.

Thanks in advance.

@vbhattsynoptek

First of all, we recommend using the latest version always as we provide support on the basis of the latest available version of the API which is 23.9 at the moment.

Please check the below sample code snippet that can be used to add page border in a PDF.

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = doc.Pages.Add();
page.PageInfo.Margin = new MarginInfo(72, 72, 72, 72);
Bitmap bmp = new Bitmap((int)page.PageInfo.Width, (int)page.PageInfo.Height);
Graphics gImage = Graphics.FromImage(bmp);
// starting points x,y = left,top margins i.e 72,72 and height/width = 72*2/72*2 => so that four sides of image would be at equal distance from page edges
gImage.DrawRectangle(Pens.Black, 72, 72, bmp.Width - 144, bmp.Height - 144);
MemoryStream imageStream = new MemoryStream();
bmp.Save(imageStream, ImageFormat.Png);
imageStream.Seek(0, SeekOrigin.Begin);
ImageStamp borderStamp = new ImageStamp(imageStream);
borderStamp.HorizontalAlignment = HorizontalAlignment.Center;
borderStamp.VerticalAlignment = VerticalAlignment.Center;
page.AddStamp(borderStamp);
doc.Save(dataDir + "PageBorder.pdf");

In case your PDF is being generated dynamically from HTML and you do not know how many pages it may have, you can use Document.ProcessParagraphs() method to update the document object with automatically added pages and you will be able to get updated page count using Document.Pages.Count Property.