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

Free Support Forum - aspose.com

Add margin and border to large pdf without looping through individual page

How to add margin and border to large pdf without looping through individual page.

@laxmanbandaru

To add border in PDF file, you need to loop through all pages of PDF as shown in following code example.

We have logged a feature request as PDFNET-51533 to add border to PDF without looping through all pages. We will inform you via this forum thread once this feature is available. We apologize for your inconvenience.

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Pages.Add();
doc.Pages.Add();
doc.Pages.Add();
foreach (Aspose.Pdf.Page page in doc.Pages)
{
    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, System.Drawing.Imaging.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 + "22.2.pdf");

@tahir.manzoor

Thanks for logging a feature request.