Articles in this section
Category / Section

How to insert image in PDF and merge with multiple PDF documents with PDF version 1.7 using C#

3 mins read

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 in the PDF and merge it with the multiple PDF documents with PDF version 1.7 using C# and VB.NET.

Steps to insert image in the PDF and merge it with the multiple PDF documents with PDF version 1.7 programmatically:

  1. Create a new Windows Forms application project.

Create a new Windows Forms 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 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

 

  1. Add the following code in the button1_Click to insert an image in the PDF and merge it with the 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 in 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 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 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,

Output document screenshot

 

Take a moment to peruse the documentation. You can find features like insert image to PDF document, Creating templates from an existing PDF document and add to the new PDF document, Merge PDF documents, adding text to PDF, and more.

 

Refer to this link 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 this link to learn about generating and registering the Syncfusion license key in your application to use the components without trail message.

 

See Also:

Image to PDF in Windows Forms

Create PDF in Windows Forms

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