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

Insert Image and Text at Specified Position of PDF Document in C#

Image and text are basic but also essential elements of PDF documents. Sometimes, we want to insert an image or some text at the specified position which is apart from body content, for example, you may need to place your company logo at the bottom of a page. In this post, you will learn how to insert image and text at a location with specified X, Y coordinate in a PDF document in C#.

Tools we need

Using following namespaces

using Spire.Pdf;

using Spire.Pdf.Graphics;

using System.Drawing;

Using the code

Step 1: Initialize a new instance of PdfDocument class, add a new page to PDF document.

PdfDocument doc = new PdfDocument();

PdfPageBase page = doc.Pages.Add();

Step 2: Insert a resized image at the specified position in PDF document.

PdfImage image = PdfImage.FromFile(“Dragon.bmp”);

float width = image.Width * 0.9f;

float height = image.Height * 0.9f;

float x1 = 400;

float y1 = 650;

page.Canvas.DrawImage(image, x1, y1, width, height);

Step 3: Add text at the specified location with the specified Brush and Font objects.

String s = “DRAGON MEDIA”;

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f);

PdfBrush brush=new PdfSolidBrush(Color.Red);

float x2 = 400;

float y2 = 730;

page.Canvas.DrawString(s, font, brush, x2, y2);

Step 4: Save the changes to file.

doc.SaveToFile(“result.pdf”);

Output

blog2016-1-4

Find and Highlight Text with Custom Color in PDF in C#, VB.NET

Find function is essential to any PDF reader, which makes it easy for user to locate the specified content in a PDF document. In this post, I’ll show you how to find and highlight searched text with custom color programmatically using .NET PDF library with C# and VB.NET. By using this component, you can also create the find and highlight feature in your own .NET application.

Step 1: Download Spire.PDF and add following namespace to your own project.

[C#]

using Spire.Pdf;

using Spire.Pdf.General.Find;

using System.Drawing;

[VB.NET]

Imports Spire.Pdf

Imports Spire.Pdf.General.Find

Imports System.Drawing

Step 2: Use following code snippet to find and highlight text in the PDF document.

[C#]

PdfDocument pdf = new PdfDocument(@”sample.pdf”);

PdfTextFind[] result = null;

foreach (PdfPageBase page in pdf.Pages)

{

    result = page.FindText(“SQL”).Finds;

    foreach (PdfTextFind find in result)

    {

        find.ApplyHighLight(Color.BlueViolet);

    }

}

pdf.SaveToFile(“result.pdf”);

System.Diagnostics.Process.Start(“result.pdf”);

[VB.NET]

Dim pdf As New PdfDocument(“sample.pdf”)

Dim result As PdfTextFind() = Nothing

For Each page As PdfPageBase In pdf.Pages

    result = page.FindText(“SQL”).Finds

    For Each find As PdfTextFind In result

        find.ApplyHighLight(Color.BlueViolet)

    Next

Next

pdf.SaveToFile(“result.pdf”)

System.Diagnostics.Process.Start(“result.pdf”)

Output

result-find&highlight