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
- {
-
- string outputFile = "result.pdf";
-
- PdfDocument doc = new PdfDocument();
-
- PdfPageBase page = doc.Pages.Add();
-
-
- PdfGrid grid = new PdfGrid();
-
-
- grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
-
-
- PdfGridRow row = grid.Rows.Add();
-
-
- grid.Columns.Add(4);
- float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
-
-
- 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;
-
-
- PdfGridCellTextAndStyleList lst = new PdfGridCellTextAndStyleList();
- PdfGridCellTextAndStyle textAndStyle = new PdfGridCellTextAndStyle();
- textAndStyle.Image = PdfImage.FromFile("ccorner.jpg");
-
-
- textAndStyle.ImageSize = new SizeF(70,70);
- lst.List.Add(textAndStyle);
-
-
- row.Cells[1].Value = lst;
-
-
- PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));
-
-
- doc.SaveToFile(outputFile, FileFormat.PDF);
-
- }
- catch (Exception)
- {
- throw;
- }
- }
- The following code encrypts the PDF document.
- private static void EncryptPDF()
- {
- try
- {
-
- PdfDocument doc = new PdfDocument();
- doc.LoadFromFile(@"result.pdf");
-
-
- 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.