How to Enlarge PDF Margins without Changing Page Size in C#, VB.NET

PDF margins are white spaces between body contents and page edge. Unlike Word, margins in PDF document are not easy to be modified as Adobe does not provide any functionality for users to manipulate margins freely. However, you can change the page scaling (enlarge/compress content) or crop page to get fitted margins. In this article, you will learn how to enlarge PDF margins by compressing content.

Step 1: Create a PdfDocument object to load the original PDF document.

PdfDocument origDoc = new PdfDocument();
origDoc.LoadFromFile("sample.pdf");

Step 2: Create another PdfDocument object.

PdfDocument destDoc = new PdfDocument();

Step 3: Set the increments that you want to add to the margins in the existing PDF document.

float top = 50;
float bottom = 50;
float left = 50;
float right = 50;

Step 4: Transfer the compressed content from the original document to the new PDF document.

foreach (PdfPageBase page in origDoc.Pages)
{
    PdfPageBase newPage = destDoc.Pages.Add(page.Size, new PdfMargins(0));
    newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width,
                        (page.ActualSize.Height - top - bottom) / page.ActualSize.Height);
    newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(left, top));
}

Step 5: Save to file.

destDoc.SaveToFile("result.pdf", FileFormat.PDF);

Original PDF:

How to Enlarge PDF Margins without Changing Page Size in C#, VB.NET

Result:

How to Enlarge PDF Margins without Changing Page Size in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace ChangeMargins
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument origDoc = new PdfDocument();
            origDoc.LoadFromFile("sample.pdf");
            PdfDocument destDoc = new PdfDocument();

            float top = 50;
            float bottom = 50;
            float left = 50;
            float right = 50;

            foreach (PdfPageBase page in origDoc.Pages)
            {
                PdfPageBase newPage = destDoc.Pages.Add(page.Size, new PdfMargins(0));
                newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width,
                                    (page.ActualSize.Height - top - bottom) / page.ActualSize.Height);
                newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(left, top));
            }

            destDoc.SaveToFile("result.pdf", FileFormat.PDF);
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace ChangeMargins
	Class Program
		Private Shared Sub Main(args As String())
			Dim origDoc As New PdfDocument()
			origDoc.LoadFromFile("sample.pdf")
			Dim destDoc As New PdfDocument()

			Dim top As Single = 50
			Dim bottom As Single = 50
			Dim left As Single = 50
			Dim right As Single = 50

			For Each page As PdfPageBase In origDoc.Pages
				Dim newPage As PdfPageBase = destDoc.Pages.Add(page.Size, New PdfMargins(0))
				newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width, (page.ActualSize.Height - top - bottom) / page.ActualSize.Height)
				newPage.Canvas.DrawTemplate(page.CreateTemplate(), New PointF(left, top))
			Next

			destDoc.SaveToFile("result.pdf", FileFormat.PDF)
		End Sub
	End Class
End Namespace