Articles in this section
Category / Section

How to add cover page to an existing PDF document?

4 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 the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

 

Conclusion:

I hope you enjoyed learning about how to add cover page to an existing PDF document.

 

You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!


 

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