PDF has become one of the most popular and widely used file formats. Images are a vital part of PDF files. Adding, removing, extracting, and replacing images in a PDF file is usually very hard without Adobe Acrobat Pro DC. In this article, we are going to learn how to do this in a PDF document using C# with the help of our Syncfusion PDF Library.
The Syncfusion PDF Library is a .NET PDF library that allows users to add and manipulate images in PDF files without any reference to Adobe dependencies.
The following are the steps we will see in this article:
Install-Package Syncfusion.Pdf.Net.Core |
Follow these steps to add an image to a PDF file using the Syncfusion PDF library:
The following code example shows how to add an image to a PDF file using C#.
using System.IO; using Syncfusion.Drawing; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing; namespace Add_image { class Program { static void Main(string[] args) { //Load a PDF document. PdfLoadedDocument doc = new PdfLoadedDocument(new FileStream("../../../../../Data/input.pdf",FileMode.Open)); //Get first page from document. PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage; //Create PDF graphics for the page. PdfGraphics graphics = page.Graphics; //Load the image from the disk. PdfBitmap image = new PdfBitmap(new FileStream("../../../../../Data/Sample.jpg",FileMode.Open)); //Draw the image. graphics.DrawImage(image, new RectangleF(50, 150, 400, 250)); //Create stream object to save file. FileStream stream = new FileStream("Output.pdf", FileMode.Create); //Save the document. doc.Save(stream); //Close the document. doc.Close(true); //Close stream. stream.Close(); } } }
By executing this code example, you will get a PDF document similar to the following screenshot.
Let’s see how to extract all the images from a PDF file by following these steps:
Note: Install the Syncfusion.Pdf.Imaging.Net.Core NuGet Package to extract images in a .NET Core application.
The following code example shows how to extract images from an existing PDF document using C#.
using System.Drawing; using System.IO; using Syncfusion.Pdf; using Syncfusion.Pdf.Exporting; using Syncfusion.Pdf.Parsing; namespace Extract_images { class Program { static void Main(string[] args) { //Load the template document. PdfLoadedDocument doc = new PdfLoadedDocument(new FileStream("../../../../../Data/ImageInput.pdf",FileMode.Open)); //Load the first page. PdfPageBase pageBase = doc.Pages[0]; //Extract images from first page. Image[] extractedImages = pageBase.ExtractImages(); //Save images to file. for (int i = 0; i < extractedImages.Length; i++) { extractedImages[i].Save("Image" + i + ".jpg"); } //Close the document. doc.Close(true); } } }
You can easily remove the unwanted images from a PDF document with the help of our Syncfusion PDF Library. This will reduce the file size considerably.
Follow these steps to remove images from a PDF document:
Note: Install the Syncfusion.Pdf.Imaging.Net.Core NuGet package to remove an image in a .NET Core application.
The following code example shows how to remove an image from an existing PDF document using C#.
using Syncfusion.Pdf; using Syncfusion.Pdf.Exporting; using Syncfusion.Pdf.Parsing; using System.IO; namespace Remove_images { class Program { static void Main(string[] args) { //Load the template document. PdfLoadedDocument doc = new PdfLoadedDocument(new FileStream("../../../../../Data/ImageInput.pdf",FileMode.Open)); //Get first page of the document. PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage; //Remove first image in the page. page.RemoveImage(page.GetImagesInfo()[0]); FileStream stream = new FileStream("output.pdf", FileMode.Create); //Save the modified document to file. doc.Save(stream); //Close the PDF document. doc.Close(true); stream.Close(); } } }
By executing this code example, you will get an output similar to the following screenshot.
You can replace an existing image with a new image or modify an existing image in a PDF document.
To do so, we have to:
Note: Please install the Syncfusion.Pdf.Imaging.Net.Core NuGet package to replace an image in a .NET Core application.
In the following example, we have replaced an existing image in a PDF document with a pixelated image.
using Syncfusion.Pdf; using Syncfusion.Pdf.Exporting; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing; using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace Replace_image { class Program { static void Main(string[] args) { //Load the template document. PdfLoadedDocument doc = new PdfLoadedDocument(new FileStream("../../../../../../Data/ImageInput.pdf",FileMode.Open)); //Get first page of the document. PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage; //Get image info of the first image. PdfImageInfo pdfImageInfo = page.GetImagesInfo()[0]; RectangleF bounds = pdfImageInfo.Bounds; //Remove first image in the page. page.RemoveImage(pdfImageInfo); //Pixlate the existing image. Stream pixlated = Pixelate((Bitmap)pdfImageInfo.Image, new Rectangle(0, 0, pdfImageInfo.Image.Width, pdfImageInfo.Image.Height), 15); //Draw the pixlated image in the existing image bounds. page.Graphics.DrawImage(new PdfBitmap(pixlated), bounds.X,bounds.Y,bounds.Width,bounds.Height); FileStream stream = new FileStream("output.pdf", FileMode.Create); //Save the modified document to file. doc.Save(stream); //Close the PDF document. doc.Close(true); } private static Stream Pixelate(Bitmap image, Rectangle rectangle, Int32 pixelateSize) { Bitmap pixelated = new System.Drawing.Bitmap(image.Width, image.Height); // Make an exact copy of the bitmap provided. using (Graphics graphics = System.Drawing.Graphics.FromImage(pixelated)) graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); // look at every pixel in the rectangle while making sure we're within the image bounds. for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize) { for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy += pixelateSize) { Int32 offsetX = pixelateSize / 2; Int32 offsetY = pixelateSize / 2; // Make sure that the offset is within the boundry of the image. while (xx + offsetX >= image.Width) offsetX--; while (yy + offsetY >= image.Height) offsetY--; // Get the pixel color in the center. Color pixel = pixelated.GetPixel(xx + offsetX, yy + offsetY); // For each pixel in the pixelate size, set it to the center color. for (Int32 x = xx; x < xx + pixelateSize && x < image.Width; x++) for (Int32 y = yy; y < yy + pixelateSize && y < image.Height; y++) pixelated.SetPixel(x, y, pixel); } } MemoryStream stream = new MemoryStream(); pixelated.Save(stream, ImageFormat.Jpeg); stream.Position = 0; pixelated.Dispose(); return stream; } } }
By executing this code example, you will get an image similar to the following screenshot.
Also, you can get the complete example on GitHub: Add, Extract, Remove, and Replace Images from PDF documents using C#.
Thanks for reading! In this blog post, you have learned how to add, extract, remove, and replace images in a PDF document using C#. Our Syncfusion PDF Library offers PDF viewer controls to view, review, and print PDF files. Its powerful conversion APIs make it easy to convert HTML, Word, Excel, PowerPoint, and images to PDF format. Take a moment to peruse our documentation, where you’ll find other options and features, all with accompanying code examples.
A single image can say a thousand words. So, let’s use our Syncfusion PDF Library to easily handle images in a PDF document using C#!
If you have any questions about these features, please let us know in the comments section of this blog post. You can also contact us through our support forum, Direct-Trac, or feedback portal. We are happy to assist you!