Articles in this section
Category / Section

How to add cover page to an existing PDF document?

3 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add cover page to an existing PDF document using C# and VB.NET.

Steps to add cover page to an existing PDF programmatically:

  1. Create a new C# Windows Forms application project. Create new Windows forms application
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. Install Nuget packages
  3. Include the following namespaces in Program.cs file.

C#

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

 

VB.NET

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

 

  1. Use the following code snippet to add cover page to an existing PDF document.

C#

//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//Create a new instance of PdfDocument
PdfDocument document = new PdfDocument();
//Add a section to PDF document
PdfSection section1 = document.Sections.Add();
//Add pages to the section
PdfPage coverpage = section1.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = coverpage.Graphics;
//Set the standard font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Set the string format with horizontal and vertical alignment
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
//Add vertical distance between the baselines of adjacent lines of text
format.LineSpacing = 2;
RectangleF bounds = new RectangleF(PointF.Empty, coverpage.GetClientSize());
//Draw the text
graphics.DrawString("Essential PDF provides support to add barcodes to the PDF document.", font, PdfBrushes.Black, bounds, format);
PdfSection section2 = document.Sections.Add();
//Add a section to PDF document
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
    //Get loaded page as template
    PdfTemplate template = loadedDocument.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, PointF.Empty, page.GetClientSize());
}
//Save and close the document
document.Save("AddCoverPage.pdf");
document.Close(true);
//Close the loaded PDF docuemnt
loadedDocument.Close(true);

 

VB.NET

'Load the PDF document
Dim loadedDocument As New PdfLoadedDocument("..\..\Data\input.pdf")
'Create a new instance of PdfDocument
Dim document As New PdfDocument()
'Add a section to PDF document
Dim section1 As PdfSection = document.Sections.Add()
'Add pages to the section
Dim coverpage As PdfPage = section1.Pages.Add()
'Create PDF graphics for the page
Dim graphics As PdfGraphics = coverpage.Graphics
'Set the standard font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20)
'Set the string format with horizontal and vertical alignment
Dim format As New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)
'Add vertical distance between the baselines of adjacent lines of text
format.LineSpacing = 2
Dim bounds As New RectangleF(PointF.Empty, coverpage.GetClientSize())
'Draw the text
graphics.DrawString("Essential PDF provides support to add barcodes to the PDF document.", font, PdfBrushes.Black, bounds, format)
Dim section2 As PdfSection = document.Sections.Add()
'Add a section to PDF document
For i As Integer = 0 To loadedDocument.Pages.Count - 1
    'Get loaded page as template
    Dim template As PdfTemplate = loadedDocument.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, PointF.Empty, page.GetClientSize())
Next
'Save and close the document
document.Save("AddCoverPage.pdf")
document.Close(True)
'Close the loaded PDF docuemnt
loadedDocument.Close(True)

 

A complete working sample can be downloaded from AddCoverPage.zip.

By executing the program, you will get the PDF document as follows. Screenshot of output PDF document

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.

Note:

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.

 

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