Add, Replace, or Delete Images PDF in C#
Images in PDF can help readers better understand the text content, especially when it comes to complex data, charts or concepts. In addition, high-quality images can also make the document look more professional and attractive. This article will focus on how to use C# to deal with images in a PDF document, including inserting, replacing and deleting images.
- Add an Image to a PDF Document in C#
- Replace an Existing Image in PDF in C#
- Delete Images from PDF in C#
Free .NET Library for Processing PDFs
Free Spire.PDF for .NET is a third-party free library for performing various PDF processing operations (with some page limitations).
The free library can be installed directly via NuGet, or you can download its product package via the link below and then reference the dlls manually.
Add an Image to a PDF Document in C#
The free PDF API provides the PdfPageBase.Canvas.DrawImage() method to draw an image at the specified coordinates on a PDF page.
C# code:
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace InsertImage
{
class Program
{
static void Main(string[] args)
{
//Create a Pdf document
PdfDocument pdf = new PdfDocument();
//Add a page
PdfPageBase page = pdf.Pages.Add();
//Load an image
PdfImage image = PdfImage.FromFile("img.jpg");
//Specify the width and height of the image
float width = image.Width * 0.5f;
float height = image.Height * 0.5f;
//Draw the image at a specified location on the page
page.Canvas.DrawImage(image, 100, 50, width, height);
//Save the PDF document
pdf.SaveToFile("PDFImage.pdf", FileFormat.PDF);
}
}
}
Result:
Replace an Existing Image in PDF in C#
To replace an existing image with a new image in the PDF, you can use the PdfPageBase.ReplaceImage() method.
C# code:
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Utilities;
namespace ReplaceImage
{
class Program
{
static void Main(string[] args)
{
//Load a PDF document
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("PDFImage.pdf");
//Get the first page
PdfPageBase page = pdf.Pages[0];
//Load an image
PdfImage image = PdfImage.FromFile("code.png");
//Create a PdfImageHelper instance
PdfImageHelper imageHelper = new PdfImageHelper();
//Get the image information on 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 PDF document
pdf.SaveToFile("ReplaceImage.pdf", FileFormat.PDF);
}
}
}
Result:
Delete Images from PDF in C#
To delete a specified image from a PDF document, simply use the PdfPageBase.DeleteImage(index) method.
C# code:
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Utilities;
namespace ReplaceImage
{
class Program
{
static void Main(string[] args)
{
//Load a PDF document
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("PDFImage.pdf");
//Get the first page
PdfPageBase page = pdf.Pages[0];
//Delete the first image on the page
page.DeleteImage(0);
//Save the PDF document
pdf.SaveToFile("DeleteImage.pdf", FileFormat.PDF);
}
}
}
By executing the above three sample code, you can add, replace, delete images in a PDF document.
If you are interested in exploring more PDF processing features in .NET C#, check below documentation: