Insert an Image Into a PDF in C#

Introduction

We will create a simple PDF grid and show how to insert an image into a specific PDF grid cell in C#. Images are more attractive for reading documents. Images are related to the contents. Sometimes, an image can describe some content more clearly, like using a chart to show how data changes in a period.

Spire.PDF for .NET is a professional .NET PDF component to quickly generate, open, modify and save PDF documents without using Office Automation and enables users to insert an image into a PDF and set its size depending on the page using C#. How to draw a nested grid in a PDF document and set a grid row & cell format. In the second part you understand how to Encrypt a PDF document with a password. This guide introduces an easy way to insert an image via Spire.PDF for .NET.

Create a Console Application for the demo. Use the following procedure:

  1. Open Visual Studio.
  2. "File" -> "New" -> "Project...".
  3. Select C# Language then select Console Application and name it “InsertImageToPDF”.
  4. Click OK.
  5. Insert the following code for inserting an image into the PDF.
    private static void InsertImageIntoPDF()  
    {  
        try  
        {  
            //Path to Store PDF file  
           string outputFile = "result.pdf";  
            //Create a PDF document using Spire.PDF.dll  
            PdfDocument doc = new PdfDocument();  
            //Add a page  
            PdfPageBase page = doc.Pages.Add();  
      
            //Create a pdf grid  
            PdfGrid grid = new PdfGrid();  
      
            //Set the cell padding of pdf grid  
            grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);  
      
            //Add a row for pdf grid  
            PdfGridRow row = grid.Rows.Add();  
      
            //Add two columns for pdf grid   
            grid.Columns.Add(4);  
            float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);  
      
            //Set the width of the first column  
            grid.Columns[0].Width = width * 0.20f;  
            grid.Columns[1].Width = width * 0.20f;  
            grid.Columns[2].Width = width * 0.20f;  
            grid.Columns[3].Width = width * 0.20f;  
              
            //Add a image  
            PdfGridCellTextAndStyleList lst = new PdfGridCellTextAndStyleList();  
            PdfGridCellTextAndStyle textAndStyle = new PdfGridCellTextAndStyle();  
            textAndStyle.Image = PdfImage.FromFile("ccorner.jpg");  
      
            //Set the size of image  
            textAndStyle.ImageSize = new SizeF(70,70);  
            lst.List.Add(textAndStyle);  
      
            //Add a image into the first cell.   
            row.Cells[1].Value = lst;  
      
            //Draw pdf grid into page at the specific location  
            PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));  
      
            //Save to a pdf file   
            doc.SaveToFile(outputFile, FileFormat.PDF);  
              
        }  
        catch (Exception)  
        {  
            throw;  
        }  
    }
  6. The following code encrypts the PDF document.
    private static void EncryptPDF()  
    {  
        try  
        {  
            //Create a pdf document.  
            PdfDocument doc = new PdfDocument();  
            doc.LoadFromFile(@"result.pdf");  
      
            //encrypt  
            doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit;  
            doc.Security.OwnerPassword = "test";  
            doc.Security.UserPassword = "test";  
            doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;  
      
            doc.SaveToFile(@"Result_Encrypted.pdf");  
            doc.Close();  
            System.Diagnostics.Process.Start(@"Result_Encrypted.pdf");  
        }  
        catch (Exception)  
        {                  
            throw;  
        }  
    }
  7. Save and run it. It will show output like the following.

Output

First it will show like the following to enter a password and click OK.

Insert an Image Into a PDF in C#

It will show like the following image.

Insert an Image Into a PDF in C#

Note: For detailed code please download the Zip file attached above.

Summary

I hope you now understand how to insert images into PDF documents programmatically and encrypt a PDF and make it password protected. If you have any suggestion regarding this article then please contact me.

The contents used are from Spire.PDF; for more information click here.


Recommended Free Ebook
Similar Articles