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

Free Support Forum - aspose.com

Add Border to PDF Pages Programmatically using Aspose.PDF for .NET

Hi !
I have a PDF out.pdf (49.7 KB) that I want to add 10 pt/px border to all pages. See image test.png (74.2 KB)

Can you provide some sample code?


Thank you for contacting support.

We have investigated your requirements but the border is not correctly added for this PDF document being a tesseracted file which contains image and overlaying text. Below are the approaches we have devised to add border:

Aspose.Pdf.Document document = new Aspose.Pdf.Document(dataDir + "out.pdf");
foreach (Aspose.Pdf.Page page in document.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);
    Aspose.Pdf.Drawing.Line bottomline = new Aspose.Pdf.Drawing.Line(new float[] { (float)page.MediaBox.LLX, (float)page.MediaBox.LLY, (float)page.MediaBox.Width, (float)page.MediaBox.LLY });
    Aspose.Pdf.Drawing.Line topline = new Aspose.Pdf.Drawing.Line(new float[] { 0, (float)page.PageInfo.Height, (float)page.MediaBox.Width, (float)page.PageInfo.Height });
    Aspose.Pdf.Drawing.Line rightline = new Aspose.Pdf.Drawing.Line(new float[] { (float)page.MediaBox.Width, 0, (float)page.MediaBox.Width, (float)page.PageInfo.Height });
    Aspose.Pdf.Drawing.Line leftline = new Aspose.Pdf.Drawing.Line(new float[] { 0, 0, 0, (float)page.PageInfo.Height });

    bottomline.GraphInfo.LineWidth = 10;
    topline.GraphInfo.LineWidth = 10;
    rightline.GraphInfo.LineWidth = 10;
    leftline.GraphInfo.LineWidth = 10;


    graph.ZIndex = 0;
document.Save(dataDir + "outBorder_19.1.pdf");

The second approach includes use of ImageStamp, as under:

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "out.pdf");
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;
doc.Save(dataDir + "outBorder_19.1.pdf");

However, the border is still not added fine for shared document. Therefore, a ticket with ID PDFNET-45878 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

Thank you! I will wait for the ticket to be resolved (has same issue). However in the next step I will convert PDF to Tiff. Is it possible to add the border during TIFF conversion?

        var pdfDocument = new Document(path);
        var resolution = new Resolution(300,300);
        var tiffSettings = new TiffSettings();
        tiffSettings.Shape = ShapeType.None;
        var tiffDevice = new TiffDevice(resolution, tiffSettings);
        tiffDevice.Process(pdfDocument, path.Replace("pdf", "tiff"));


We are checking your requirements and will let you know soon.

Hi !
I tested your code. But even though I don’t use PDF documents that aren’t a tesseracted test.pdf (21.0 KB), I don’t get it right.
In this case, the border on the underside is missing and on many other PDFs it is missing from the right side. outBorder_19.1.pdf (21.2 KB)


We will let you know as soon as the ticket will be resolved. Moreover, in order to work with generated TIFF file, you can add border with Aspose.Imaging for .NET API by using below code snippet in your environment:

    using (TiffImage tiffImage = (TiffImage)Image.Load(sourcePath))
        foreach (TiffFrame frame in tiffImage.Frames)
            Graphics graphics = new Graphics(frame);
            Rectangle rect = new Rectangle(1,1,frame.Width-2,frame.Height-2);
            graphics.DrawRectangle(new Pen(Color.Blue,2), rect);
        tiffImage.Save("out_"+ sourcePath);

Please feel free to contact us if you need any further assistance.


Suggested code snippet works fine for the ideal case when a page is added to blank PDF document and then border is applied, as under:

Aspose.Pdf.Document document = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = document.Pages.Add();
//Add border to this page object and save the document


However, a ticket is already logged for your scenario and your recent concerns have also been recorded. We will let you know as soon as some significant progress will be made in this regard.