How to Insert Image and Merge with Multiple PDF in C# in WinForms PDF?
The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can insert an image into a PDF and merge it with multiple PDF documents with PDF version 1.7 using C# and VB.NET.
Steps to insert an image into the PDF and merge it with multiple PDF documents with PDF version 1.7 programmatically:
- Create a new Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespace in the Form1.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
- Add the following code in the button1_Click to insert an image into the PDF and merge it with multiple PDF documents with PDF version 1.7.
C#
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Set the page size to A4
document.PageSettings.Size = PdfPageSize.A4;
// Set margin
document.PageSettings.Margins.All = 0;
// Set the PDF version
document.FileStructure.Version = PdfVersion.Version1_7;
// Set the incremental update as false
document.FileStructure.IncrementalUpdate = false;
// Add a page to the document
PdfPage page = document.Pages.Add();
// Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
// Load the image from the disk
PdfBitmap image = new PdfBitmap("../../Data/Autumn Leaves.jpg");
// Draw the image
graphics.DrawImage(image, new RectangleF(0,0,page.GetClientSize().Width, page.GetClientSize().Height));
// File path to merge the PDF file
string file1 = "../../Data/Input1.pdf";
string file2 = "../../Data/Input2.pdf";
// Create the templates from an existing PDF document page and draw it in on a new PDF document
DrawPDFTemplate(file1, document);
DrawPDFTemplate(file2, document);
// Save the document
document.Save("Sample.pdf");
// Close the document
document.Close(true);
// This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Sample.pdf");
Helper method:
/// <summary>
/// Create the templates from an existing PDF document page and draw it on a new PDF document
/// </summary>
/// <param name="file"></param>
/// <param name="document"></param>
private void DrawPDFTemplate(string file, PdfDocument document)
{
// Load a PDF document
PdfLoadedDocument ldoc = new PdfLoadedDocument(file);
for (int i = 0; i < ldoc.Pages.Count; i++)
{
// Get loaded page as a template
PdfTemplate template = ldoc.Pages[i].CreateTemplate();
// Create a new page
PdfPage page = document.Pages.Add();
// Create PDF graphics for the page
PdfGraphics g = page.Graphics;
// Draw template with the size as page size
g.DrawPdfTemplate(template, new PointF(0, 0), new SizeF(page.GetClientSize().Width, page.GetClientSize().Height));
}
}
VB.NET
'Create a new PDF document
Dim document As PdfDocument = New PdfDocument()
'Set the page size to A4
document.PageSettings.Size = PdfPageSize.A4
'Set margin
document.PageSettings.Margins.All = 0
'Set the PDF version
document.FileStructure.Version = PdfVersion.Version1_7
'Set the incremental update as false
document.FileStructure.IncrementalUpdate = False
'Add a page to the document
Dim page As PdfPage = document.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/Autumn Leaves.jpg")
'Draw the image
graphics.DrawImage(image, New RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height))
'File path to merge the PDF file
Dim file1 As String = "../../Data/Input1.pdf"
Dim file2 As String = "../../Data/Input2.pdf"
'Create the templates from an existing PDF document page and draw it on a new PDF document
DrawPDFTemplate(file1, document)
DrawPDFTemplate(file2, document)
'Save the document
document.Save("Sample.pdf")
'Close the document
document.Close(True)
'This will open the PDF file and the result will be seen in the default PDF Viewer
Process.Start("Sample.pdf")
Helper method:
''' <summary>
''' Create the templates from an existing PDF document page and draw it in on a new PDF document
''' </summary>
''' <param name="file"></param>
''' <param name="document"></param>
Private Sub DrawPDFTemplate(ByVal file As String, ByVal document As PdfDocument)
'Load a PDF document
Dim ldoc As PdfLoadedDocument = New PdfLoadedDocument(file)
For i As Integer = 0 To ldoc.Pages.Count - 1
'Get loaded page as a template
Dim template As PdfTemplate = ldoc.Pages(i).CreateTemplate()
'Create a 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 page size
g.DrawPdfTemplate(template, New PointF(0, 0), New SizeF(page.GetClientSize().Width, page.GetClientSize().Height))
Next
End Sub
A complete working sample can be downloaded from MergePDFSample.zip.
By executing the program, you will get the output document as follows:
Take a moment to peruse the documentation. You can find features like insert image into PDF document, creating templates from an existing PDF document and add to the new PDF document, Merge PDF documents, adding text to a PDF, and more.
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 product. Refer to this 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 insert an image and merge it with multiple PDFs in C# in WinForms PDF.
You can refer to our WinForms 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 WinForms PDF example 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!
See Also: