C#/VB.NET: Adjust the Margins of a PDF Document

2023-04-21 01:23:59 Written by  support iceblue
Rate this item
(0 votes)

PDF margins are blank areas between content and page edges. In most cases, we set moderate or narrow margins in order to create a compact appearance. However, if we wish to place a company logo or other relevant information in the margins, we need to make the margins a bit wider. In this article, you will learn how to increase or decrease the margins of an existing PDF document in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Increase the Margins of a PDF Document in C#, VB.NET

The way to enlarge the margins of a PDF document is to create a new PDF that has a larger page size, and then draw the source page on the large page at the appropriate location. The following are the steps to increase the margins of a PDF document using Spire.PDF for .NET.

  • Load the original PDF document while initialing the PdfDocument object.
  • Create another PdfDocument object, which is used to create a new PDF document that has a larger page size.
  • Set the increasing values of the margins.
  • Calculate the page size of the new PDF document.
  • Loop through the pages in the original document, and create a template based on a specific page using PdfPageBase.CreateTemplate() method.
  • Add a page to the new PDF document using PdfDocument.Pages.Add() method.
  • Draw the template on the page at the coordinate (0, 0) using PdfTemplate.Draw() method.
  • Save the new PDF document to file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace IncreaseMargins
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the original PDF document
            PdfDocument originalPdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\sample.pdf");

            //Get the first page
            PdfPageBase firstPage = originalPdf.Pages[0];

            //Create a new PdfDocument object
            PdfDocument newPdf = new PdfDocument();

            //Set increasing value of the margins
            PdfMargins margins = newPdf.PageSettings.Margins;
            margins.Top = 40;
            margins.Bottom=40;
            margins.Left=40;
            margins.Right= 40;

            //Calculate the new page size
            SizeF sizeF = new SizeF(firstPage.Size.Width + margins.Left + margins.Right, firstPage.Size.Height + margins.Top + margins.Bottom);

            //Loop through the pages in the original document
            for (int i = 0; i < originalPdf.Pages.Count; i++)
            {
                //Create a template based on a spcific page
                PdfTemplate pdfTemplate = originalPdf.Pages[i].CreateTemplate();

                //Add a page to the new PDF
                PdfPageBase page = newPdf.Pages.Add(sizeF);

                //Draw template on the page
                pdfTemplate.Draw(page, 0, 0);
            }

            //Save the new document
            newPdf.SaveToFile("IncreaseMargins.pdf", FileFormat.PDF);
        }
    }
}

C#/VB.NET: Adjust the Margins of a PDF Document

Decrease the Margins of a PDF Document in C#, VB.NET

The way to decrease the margins of a PDF is to create a new PDF that has a smaller page size, and then draw the source page on the small page at a specified coordinate. The following are the steps to decrease the margins of a PDF document using Spire.PDF for .NET.

  • Load the original PDF document while initialing the PdfDocument object.
  • Create another PdfDocument object, which is used to create a new PDF document that has a smaller page size.
  • Set the decreasing values of the margins.
  • Calculate the page size of the new PDF document.
  • Loop through the pages in the original document, and create a template based on a specific page using PdfPageBase.CreateTemplate() method.
  • Add a page to the new PDF document using PdfDocument.Pages.Add() method.
  • Draw the template on the page at a specified coordinate using PdfTemplate.Draw() method.
  • Save the new PDF document to file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace DecreaseMargins
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the original PDF document
            PdfDocument originalPdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\sample.pdf");

            //Get the first page
            PdfPageBase firstPage = originalPdf.Pages[0]; 

            //Create a new PdfDocument object
            PdfDocument newPdf = new PdfDocument();

            //Set decreasing value
            float left = -20;
            float right = -20;
            float top = -20;
            float bottom = -20;

            //Calculate the new page size
            SizeF sizeF = new SizeF(firstPage.Size.Width + left + right, firstPage.Size.Height + top + bottom);

            //Loop through the pages in the original document
            for (int i = 0; i < originalPdf.Pages.Count; i++)
            {
                //Create a template based on a specific page
                PdfTemplate pdfTemplate = originalPdf.Pages[i].CreateTemplate();

                //Add a page to the new PDF
                PdfPageBase page = newPdf.Pages.Add(sizeF, new PdfMargins(0));

                //Draw template on the page
                pdfTemplate.Draw(page, left, top);
            }

            //Save the new document
            newPdf.SaveToFile("DecreaseMargins.pdf", FileFormat.PDF);
        }
    }
}

C#/VB.NET: Adjust the Margins of a PDF Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 20 June 2023 01:57