How to add border of page?

How to add border of page?

@Junyoung

Thanks for contacting support.

You may please draw border in PDF page using Graph as in following code snippet:

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = doc.Pages.Add();
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);          
graph.ZIndex = 0;
doc.Save(dataDir + "PageBorder.pdf");

PageBorder.pdf (1.5 KB)

In case of any further assistance, please feel free to let us know.

The Page Border is strange.
Page margin can be changed by user, and Page border should always fit.
And I want to know what coordinates do it mean for each index of PositionArray.
And I wonder what the difference is between Document.PageInfo.Width, Document.PageInfo.Height and Page.PageInfo.Width, Page.PageInfo.Height

protected void Page_Load(object sender, EventArgs e)
{
SetCanvasSetting();
}

	public void SetCanvasSetting()
	{
		Document doc = new Document();
		doc.PageInfo.Width = PageSize.PageLetter.Width;
		doc.PageInfo.Height = PageSize.PageLetter.Height;
		doc.PageInfo.Margin = new MarginInfo(72, 72, 72, 72);

		Page page = doc.Pages.Add();
		page.PageInfo.Width = PageSize.PageLetter.Width;
		page.PageInfo.Height = PageSize.PageLetter.Height;
		page.PageInfo.Margin = new MarginInfo(72, 72, 72, 72);

		float[] positionArray = new float[]
		{
			72,
			(float)(page.PageInfo.Height - 72),
			(float)(page.PageInfo.Width - 72),
			(float)(page.PageInfo.Height - 72)
		};

		Line line = new Line(positionArray);

		line.GraphInfo.LineWidth = 0.25f;
		Graph graph = new Graph((float)doc.PageInfo.Width, (float)doc.PageInfo.PureHeight);
		graph.Shapes.Add(line);

		page.Paragraphs.Add(graph);

		doc.Save("~/Test.pdf");
		doc.Dispose();
	}

@Junyoung

Thanks for sharing more details.

The positionArray expects values of basic Rectangle system i.e. LLX, LLY, URX, URY, upon which a line would be drawn at the page.

Both properties are used to set Page height and width during PDF generation scenarios. However new generator API follows Page.PageInfo.Height approach, which enables you to set Page Height at page level individually. The other property (Document.PageInfo.Width) is going to be obsoleted and will be removed in upcoming version(s) of the API.

In case Page Margin values can be changed by user, you may please use following more simple approach in order to add page borders. In following approach, you can draw a bitmap at run-time, according to page height/width and margins - and add it to the page as image stamp. Please check following code snippet and attached output 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");

PageBorder.pdf (14.2 KB)

In case of any further assistance, please feel free to let us know.