Articles in this section
Category / Section

How to add page as background in an existing PDF document using C# in WinForms?

6 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 a page as a background in an existing PDF document.

Steps to add a page as background in and 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;

 

VB.NET

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

 

  1. Use the following code sample to add a page as a background in an existing PDF document.

C#

//Load the background document 
PdfLoadedDocument backgroundDocument = new PdfLoadedDocument("../../Data/Essential_Pdf.pdf");
 
//Load the PDF page as background 
PdfTemplate backgroundTemplate = (backgroundDocument.Pages[0] as PdfLoadedPage).CreateTemplate();
 
//Load the PDF file 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/Barcode.pdf");
 
foreach (PdfPageBase page in loadedDocument.Pages)
{
    //Create graphics for the page 
    PdfGraphics graphics = page.Graphics;
 
    //Save the graphics state 
    PdfGraphicsState state = graphics.Save();
 
    //Set trasnaparency 
    graphics.SetTransparency(0.25f);
 
    //Draw the PDF page template 
    page.Graphics.DrawPdfTemplate(backgroundTemplate, new PointF(0, 0), page.Size);
 
    graphics.Restore();
}
 
//Save the document 
loadedDocument.Save("Sample.pdf");
 
//Close the document 
loadedDocument.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

'Load the Background document 
Dim backgroundDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Data/Essential_Pdf.pdf")
 
'Load the PDF page as background  
Dim backgroundTemplate As PdfTemplate = (TryCast(backgroundDocument.Pages(0), PdfLoadedPage)).CreateTemplate()
 
'Load the PDF file  
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Data/Barcode.pdf")
 
For Each page As PdfPageBase In loadedDocument.Pages
    'Create graphics for the page 
    Dim graphics As PdfGraphics = page.Graphics
 
    'Save the graphics state  
    Dim state As PdfGraphicsState = graphics.Save()
 
    'Set trasnaparency  
    graphics.SetTransparency(0.25F)
 
    'Draw the PDF page template  
    page.Graphics.DrawPdfTemplate(backgroundTemplate, New PointF(0, 0), page.Size)
    graphics.Restore()
Next
 
'Save the document  
loadedDocument.Save("Sample.pdf")
 
'Close the document 
loadedDocument.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 PageAsBackground.zip.

 

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

Output document screenshot

 

Take a moment to peruse the documentation. You can find options like create a new PDF template, creating a template from an existing PDF, and creating document overlays.

 

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:

Add watermark to a PDF page with opaque image

Add watermark text to PDF document

Conclusion

I hope you enjoyed learning about how to add page as background in an existing PDF document using C# in WinForms.

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, 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 check out 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 forumsDirect-Trac, 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