Add Margins in PDF without Changing Page Size in C#, VB.NET

Margins in PDF are white spaces between the content and the left, right, top and bottom of the page. Sometimes, you may need to add extra spaces to PDF since the original margins are too narrow to add annotations or stamps, or simply because the narrow margins look ugly.

In this article, I am going to introduce how to enlarge margins in PDF without changing the page size programmatically using free .NET PDF component with C# and VB.NET. If you would like to run the code snippets provided below, do remember to reference the DLL in your own project.

2016-02-23_145806DLL

Code Snippets

Step 1: Initialize a new instance of PdfDocument class and load the sample PDF file.

PdfDocument origDoc = new PdfDocument();

origDoc.LoadFromFile(“Learn to Develop with Microsoft Developer Network _ MSDN.pdf”);

Step 2: Define four float variables that will be set as margins.

float top = 50;

float bottom = 50;

float left = 50;

float right = 50;

Step 3: Create a new object of PdfDocument, add new pages in the new PDF document based on the size and the content of the original document. Pay attention that I had reset the coordinates and scale of the graphic (body content) when I transfer the graphic to the new PDF document.

PdfDocument newDoc = new PdfDocument();

foreach (PdfPageBase page in origDoc.Pages)

{

    PdfPageBase newPage = newDoc.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 4: Save the file.

newDoc.SaveToFile(@”result.pdf”, FileFormat.PDF);

Original

2016-02-23_144803narrow

Modified

2016-02-23_144845wide

Entire Code

[C#]

using Spire.Pdf;

using System.Drawing;

using Spire.Pdf.Graphics;

 

namespace ChangeMargins

{

    class Program

    {

        static void Main(string[] args)

        {

            PdfDocument origDoc = new PdfDocument();


origDoc.LoadFromFile(“Learn to Develop with Microsoft Developer Network _ MSDN.pdf”);

            

            float top = 50;

            float bottom = 50;

            float left = 50;

            float right = 50;

 

            PdfDocument newDoc = new PdfDocument();

            foreach (PdfPageBase page in origDoc.Pages)

            {

                PdfPageBase newPage = newDoc.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));

            }


newDoc.SaveToFile(@”result.pdf”, FileFormat.PDF);

        }

    }

}

[VB.NET]

Imports Spire.Pdf

Imports System.Drawing

Imports Spire.Pdf.Graphics

 

 

Module Module1

 

    Sub Main()

        Dim origDoc As New PdfDocument()


origDoc.LoadFromFile(“Learn to Develop with Microsoft Developer Network _ MSDN.pdf”)

 

        Dim top As Single = 50

        Dim bottom As Single = 50

        Dim left As Single = 50

        Dim right As Single = 50

 

        Dim newDoc As New PdfDocument()

        For Each page As PdfPageBase In origDoc.Pages

            Dim newPage As PdfPageBase = newDoc.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


newDoc.SaveToFile(“result.pdf”, FileFormat.PDF)

 

    End Sub

 

End Module

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s