Articles in this section
Category / Section

How to add the custom border design to an existing PDF document using C#

7 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add the custom border design to an existing PDF document.

Steps to add the custom border design to an existing PDF document

  1. Create a new C# console application project.

Create console application project

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.

NuGet package reference

  1. Include the following namespaces in Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using System.Drawing;
using System.IO;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Parsing
Imports System.Drawing
Imports System.IO

 

  1. Use the following code sample to add the custom border design to an existing PDF document.

C#

//Create a new PDF document
PdfDocument doc = new PdfDocument();
doc.PageSettings.Margins.All = 0;
 
//Add a page to the document
PdfPage page = doc.Pages.Add();
 
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
 
//Load the image from the disk
PdfBitmap image = new PdfBitmap("../../Data/certificateborder.png");
 
//Draw the image
graphics.DrawImage(image, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);
 
//Save the document
MemoryStream ms = new MemoryStream();
doc.Save(ms);
 
//Close the document
doc.Close(true);
 
//Load the border design PDF document 
PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(ms);
 
//Load the existing PDF document 
PdfLoadedDocument loadedDocument2 = new PdfLoadedDocument("../../Data/Input.pdf");
 
//Create the new document
PdfDocument document = new PdfDocument();
document.PageSettings.Margins.All = 0;
 
//Add the border design in each page of an existing PDF document 
for (int i = 0; i < loadedDocument2.Pages.Count; i++)
{
    //Add PDF page
    PdfPage pdfPage = document.Pages.Add();
 
    //Create a template from the first document
    PdfPageBase loadedPage = loadedDocument1.Pages[0];
    PdfTemplate template = loadedPage.CreateTemplate();
 
    //Draw the loaded template into new document
    pdfPage.Graphics.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
 
    //Create a template from the second document
    loadedPage = loadedDocument2.Pages[i];
    template = loadedPage.CreateTemplate();
 
    //Draw the loaded template into new document
    pdfPage.Graphics.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
}
 
//Save the PDF document 
document.Save("Sample.pdf");
 
//Close the PDF documents 
document.Close(true);
loadedDocument1.Close(true);
loadedDocument2.Close(true);
 
//This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Sample.pdf");

 

VB.NET

'Create a new PDF document
Dim doc As PdfDocument = New PdfDocument()
doc.PageSettings.Margins.All = 0
 
'Add a page to the document 
Dim page As PdfPage = doc.Pages.Add()
 
'Create PDF graphics for the page 
Dim graphics As PdfGraphics = page.Graphics
 
'Load the image from the disk 
Dim image As PdfBitmap = New PdfBitmap("../../Data/certificateborder.png")
 
'Draw the image 
graphics.DrawImage(image, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height)
 
'Save the document
Dim ms As MemoryStream = New MemoryStream()
doc.Save(ms)
 
'Close the document
doc.Close(True)
 
'Load the border design PDF document 
Dim loadedDocument1 As PdfLoadedDocument = New PdfLoadedDocument(ms)
 
'Load the existing PDF document 
Dim loadedDocument2 As PdfLoadedDocument = New PdfLoadedDocument("../../Data/Input.pdf")
 
'Create the new document
Dim document As PdfDocument = New PdfDocument()
document.PageSettings.Margins.All = 0
 
'Add the border design in each page of an existing PDF document 
For i As Integer = 0 To loadedDocument2.Pages.Count - 1
    'Add PDF page
    Dim pdfPage As PdfPage = document.Pages.Add()
 
    'Create a template from the first document
    Dim loadedPage As PdfPageBase = loadedDocument1.Pages(0)
    Dim template As PdfTemplate = loadedPage.CreateTemplate()
 
    'Draw the loaded template into new document
    pdfPage.Graphics.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize())
 
    'Create a template from the second document
    loadedPage = loadedDocument2.Pages(i)
    template = loadedPage.CreateTemplate()
 
    'Draw the loaded template into new document
    pdfPage.Graphics.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize())
Next
 
'Save the PDF document 
document.Save("Sample.pdf")
 
'Close the PDF documents 
document.Close(True)
loadedDocument1.Close(True)
loadedDocument2.Close(True)
 
'This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Sample.pdf")

 

A complete working sample can be download from CustomBarcodeSample.zip.

 

By executing the program, you will get the output document as follows,

Output document screenshot

 

Task a moment to peruse the documentation. You can find options like creating a template in the new and existing PDF document, creating document overlays. Also, you can find a feature like inserting an image in a new PDF document, inserting an image in an existing PDF document.

 

An online sample link to overlay two different PDF documents into a single PDF document.

 

Refer to here to explore a rich set of Syncfusion Essential® PDF features.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your product. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.

 

See Also:

Insert an image to PDF

Superimpose PDF pages

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied