We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert HTML to PDF - Add border to each PDF page during conversion in C#

Hi,
I m converting an HTML string to PDF. In this process, I want to add a corner border to each page of PDF and my HTML content should be placed within that border. How can I do that? Can you please help me on this.

@agangula

Thanks for contacting support.

Would you please share the platform (e.g .NET, Java) which you are using to convert HTML into PDF along with a sample HTML file. We will test the scenario in our environment and share our feedback accordingly.

In .Net I m writing code.

Its a simple HTML file with table tags and div tags. when ever I m converting that HTML into PDF, I want a border for this HTML content in PDF file. That to every page of PDF file.

Sample code:
var ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html));
Document doc = new Document(ms, new HtmlLoadOptions());
var ms1 = new MemoryStream();
doc.Flatten();
doc.Save(ms1, Aspose.Pdf.SaveFormat.Pdf);

@agangula

Thanks for sharing more information.

As there is no direct way to add border to the PDF page while generating it from HTML, so you can add page border after PDF document is converted. Please check following code snippet, in order to add border around page content, after generating PDF from HTML.

var loadoptions = new HtmlLoadOptions();
loadoptions.PageInfo.IsLandscape = true;
loadoptions.PageInfo.Margin = new MarginInfo(10, 10, 10, 10);
var ms1 = new MemoryStream();
doc.Save(ms1, Aspose.Pdf.SaveFormat.Pdf);

foreach(Page page in doc.Pages)
{
 Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
 page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
 page.Paragraphs.Add(graph);
 Aspose.Pdf.Drawing.Line bottomline = new Aspose.Pdf.Drawing.Line(new float[] { 10, 10, (float)page.PageInfo.Width - 10, 10 });
 Aspose.Pdf.Drawing.Line topline = new Aspose.Pdf.Drawing.Line(new float[] { 10, (float)page.PageInfo.Height - 10, (float)page.PageInfo.Width - 10, (float)page.PageInfo.Height - 10 });
 Aspose.Pdf.Drawing.Line rightline = new Aspose.Pdf.Drawing.Line(new float[] { (float)page.PageInfo.Width - 10, 10, (float)page.PageInfo.Width - 10, (float)page.PageInfo.Height - 10 });
 Aspose.Pdf.Drawing.Line leftline = new Aspose.Pdf.Drawing.Line(new float[] { 10, 10, 10, (float)page.PageInfo.Height - 10 });
 graph.Shapes.Add(topline);
 graph.Shapes.Add(bottomline);
 graph.Shapes.Add(rightline);
 graph.Shapes.Add(leftline);
}
doc.Save(dataDir + "OutputWithBorder.pdf");

In case of any further assistance, or you face any issue, please feel free to let us know.