How to increase margin of an existing PDF using C# and VB.NET
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can increase margin of the PDF document using C# and VB.NET.
Steps to increase margin of existing PDF programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.Designer.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Graphics; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf.Graphics Imports System.Drawing
- Use the following code snippet to increase margin of the existing PDF document.
C#
//Load the PDF document PdfLoadedDocument ldoc = new PdfLoadedDocument("input.pdf"); //Create a new instance of PdfDocument class PdfDocument document = new PdfDocument(); //Set increasing value of the page margin as margin of the PDF document document.PageSettings.Margins.Left = 40; document.PageSettings.Margins.Right = 40; document.PageSettings.Margins.Top = 20; document.PageSettings.Margins.Bottom = 20; for (int i = 0; i < ldoc.Pages.Count; i++) { //Get loaded page as template PdfTemplate template = ldoc.Pages[i].CreateTemplate(); //Create new page PdfPage page = document.Pages.Add(); //Create Pdf graphics for the page PdfGraphics g = page.Graphics; //Draw template with the size as loaded page size g.DrawPdfTemplate(template, new PointF(0, 0), new SizeF(page.GetClientSize().Width, page.GetClientSize().Height)); } //Save and Close document document.Save("IncreaseMargin.pdf"); document.Close(true);
VB.NET
'Load a PDF document Dim ldoc As PdfLoadedDocument = New PdfLoadedDocument("input.pdf") 'Create a new instance of PdfDocument class Dim document As PdfDocument = New PdfDocument() 'Set increasing value of the page margin as margin of the PDF document document.PageSettings.Margins.Left = 40 document.PageSettings.Margins.Right = 40 document.PageSettings.Margins.Top = 20 document.PageSettings.Margins.Bottom = 20 Dim i As Integer = 0 Do While (i < ldoc.Pages.Count) 'Get loaded page as template Dim template As PdfTemplate = ldoc.Pages(i).CreateTemplate() 'Create new page Dim page As PdfPage = document.Pages.Add() 'Create Pdf graphics for the page Dim g As PdfGraphics = page.Graphics 'Draw template with the size as loaded page size g.DrawPdfTemplate(template, New PointF(0, 0), New SizeF(page.GetClientSize.Width, page.GetClientSize.Height)) i = (i + 1) Loop 'Save and Close document document.Save("IncreaseMargin.pdf") document.Close(True)
A complete working sample can be downloaded from IncreaseMargin.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with pages, where you will find other options like inserting, removing and rearranging pages in PDF document, adding margin, and importing pages from the existing PDF document.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.