Table of Contents
Installed via NuGet
PM> Install-Package Spire.PDF
Related Links
Compared with text-only documents, documents containing images are undoubtedly more vivid and engaging to readers. When generating or editing a PDF document, you may sometimes need to insert images to improve its appearance and make it more appealing. In this article, you will learn how to insert, replace or delete images in PDF documents in C# and VB.NET using Spire.PDF for .NET.
- Insert an Image into a PDF Document
- Replace an Image with Another Image in a PDF Document
- Delete a Specific Image in a PDF Document
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Insert an Image into a PDF Document in C# and VB.NET
The following steps demonstrate how to insert an image into an existing PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the desired page in the PDF document through PdfDocument.Pages[pageIndex] property.
- Load an image using PdfImage.FromFile() method.
- Specify the width and height of the image area on the page.
- Specify the X and Y coordinates to start drawing the image.
- Draw the image on the page using PdfPageBase.Canvas.DrawImage() method.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Graphics; namespace InsertImage { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Input.pdf"); //Get the first page in the PDF document PdfPageBase page = pdf.Pages[0]; //Load an image PdfImage image = PdfImage.FromFile("image.jpg"); //Specify the width and height of the image area on the page float width = image.Width * 0.50f; float height = image.Height * 0.50f; //Specify the X and Y coordinates to start drawing the image float x = 180f; float y = 70f; //Draw the image at a specified location on the page page.Canvas.DrawImage(image, x, y, width, height); //Save the result document pdf.SaveToFile("AddImage.pdf", FileFormat.PDF); } } }
Replace an Image with Another Image in a PDF Document in C# and VB.NET
The following steps demonstrate how to replace an image with another image in a PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the desired page in the PDF document through PdfDocument.Pages[pageIndex] property.
- Load an image using PdfImage.FromFile() method.
- Initialize an instance of the PdfImageHelper class.
- Get the image information from the page using PdfImageHelper.GetImagesInfo() method.
- Replace a specific image on the page with the loaded image using PdfImageHelper.ReplaceImage() method.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Utilities; namespace ReplaceImage { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument doc = new PdfDocument(); //Load a PDF document doc.LoadFromFile("AddImage.pdf"); //Get the first page PdfPageBase page = doc.Pages[0]; //Load an image PdfImage image = PdfImage.FromFile("image1.jpg"); //Create a PdfImageHelper instance PdfImageHelper imageHelper = new PdfImageHelper(); //Get the image information from the page PdfImageInfo[] imageInfo = imageHelper.GetImagesInfo(page); //Replace the first image on the page with the loaded image imageHelper.ReplaceImage(imageInfo[0], image); //Save the result document doc.SaveToFile("ReplaceImage.pdf", FileFormat.PDF); } } }
Delete a Specific Image in a PDF Document in C# and VB.NET
The following steps demonstrate how to delete an image from a PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the desired page in the PDF document through PdfDocument.Pages[pageIndex] property.
- Delete a specific image on the page using PdfPageBase.DeleteImage() method.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; namespace DeleteImage { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a PDF document pdf.LoadFromFile("AddImage.pdf"); //Get the first page PdfPageBase page = pdf.Pages[0]; //Delete the first image on the page page.DeleteImage(0); //Save the result document pdf.SaveToFile("DeleteImage.pdf", FileFormat.PDF); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.