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.
    1.    private static void InsertImageIntoPDF()  
    2.    {  
    3.        try  
    4.        {  
    5.            //Path to Store PDF file  
    6.           string outputFile = "result.pdf";  
    7.            //Create a PDF document using Spire.PDF.dll  
    8.      PdfDocument doc = new PdfDocument();  
    9.            //Add a page  
    10.            PdfPageBase page = doc.Pages.Add();  
    11.   
    12.            //Create a pdf grid  
    13.            PdfGrid grid = new PdfGrid();  
    14.   
    15.            //Set the cell padding of pdf grid  
    16.            grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);  
    17.   
    18.            //Add a row for pdf grid  
    19.            PdfGridRow row = grid.Rows.Add();  
    20.   
    21.            //Add two columns for pdf grid   
    22.            grid.Columns.Add(4);  
    23.            float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);  
    24.   
    25.            //Set the width of the first column  
    26.            grid.Columns[0].Width = width * 0.20f;  
    27.            grid.Columns[1].Width = width * 0.20f;  
    28.            grid.Columns[2].Width = width * 0.20f;  
    29.            grid.Columns[3].Width = width * 0.20f;  
    30.              
    31.            //Add a image  
    32.            PdfGridCellTextAndStyleList lst = new PdfGridCellTextAndStyleList();  
    33.            PdfGridCellTextAndStyle textAndStyle = new PdfGridCellTextAndStyle();  
    34.            textAndStyle.Image = PdfImage.FromFile("ccorner.jpg");  
    35.   
    36.            //Set the size of image  
    37.  textAndStyle.ImageSize = new SizeF(70,70);  
    38.            lst.List.Add(textAndStyle);  
    39.   
    40.            //Add a image into the first cell.   
    41.            row.Cells[1].Value = lst;  
    42.   
    43.            //Draw pdf grid into page at the specific location  
    44.            PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));  
    45.   
    46.            //Save to a pdf file   
    47. doc.SaveToFile(outputFile, FileFormat.PDF);  
    48.              
    49.        }  
    50.        catch (Exception)  
    51.        {  
    52.            throw;  
    53.        }  
    54.  } 
  6. The following code encrypts the PDF document.
    1.   private static void EncryptPDF()  
    2.   {  
    3.       try  
    4.       {  
    5.           //Create a pdf document.  
    6.           PdfDocument doc = new PdfDocument();  
    7.           doc.LoadFromFile(@"result.pdf");  
    8.   
    9.           //encrypt  
    10.           doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit;  
    11.           doc.Security.OwnerPassword = "test";  
    12.           doc.Security.UserPassword = "test";  
    13.           doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;  
    14.   
    15.           doc.SaveToFile(@"Result_Encrypted.pdf");  
    16.           doc.Close();  
    17.           System.Diagnostics.Process.Start(@"Result_Encrypted.pdf");  
    18.       }  
    19.       catch (Exception)  
    20.       {                  
    21.           throw;  
    22.       }  

  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.

enter password

It will show like the following image.

warning

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.

Up Next
    Ebook Download
    View all
    Learn
    View all