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:
- Open Visual Studio.
- "File" -> "New" -> "Project...".
- Select C# Language then select Console Application and name it “InsertImageToPDF”.
- Click OK.
- 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; } } - 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; } } - 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.

It will show like the following image.

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.

Former memberPosted Dec 18, 2019, 1:35 PM
I have downloaded and executed in Visual studio and pdf document asking password. Can you please share the encrypted password.
Dileep SreepathiPosted Jul 6, 2018, 1:52 PM
What is the password
Santosh MaharanaPosted May 4, 2018, 10:53 AM
This is not adding image to an existing pdf.
Rathrola Prem KumarPosted Dec 31, 2015, 2:47 AM
Thanq very much for sharing :)
Michal HabalcikPosted Jan 21, 2015, 1:30 AM
Thanks for sharing. I am missing an information whether the library is for free or you have actually buy it.
Santhakumar MunuswamyPosted Jan 20, 2015, 3:39 AM
nice article
Praveen KumarPosted Jan 20, 2015, 2:59 AM
superb
Jagdev MishraPosted Jan 20, 2015, 2:49 AM
good article.