Articles in this section
Category / Section

How to extract a part of the image from a PDF using C# and VB.NET?

2 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can extract a part of the image from a PDF document using C# and VB.NET.

Steps to extract a part of the images from a PDF programmatically:

  1. Create a new C# WPF application project. Create new WPF application
  2. Install the Syncfusion.Pdf.Wpf NuGet package as reference to your .NET Framework application from NuGet.org. Install required Nuget packages
  3. Include the following namespaces in the Form1.Designer.cs file.

C#

using Syncfusion.Parsing;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

 

VB.NET

Imports System.Drawing
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing

 

  1. Use the following code snippet to extract a part of the image from PDF document.

C#

//Load the PDF document
PdfLoadedDocument doc = new PdfLoadedDocument("input.pdf");
//Create a new instance for unit converter to convert points to pixels
PdfUnitConvertor unitconverter = new PdfUnitConvertor();
//Calculate the extraction bounds from the bounds of loaded annotation
float x = unitconverter.ConvertToPixels((doc.Pages[0].Annotations[0] as PdfLoadedSquareAnnotation).Bounds.X, PdfGraphicsUnit.Point);
float y = unitconverter.ConvertToPixels((doc.Pages[0].Annotations[0] as PdfLoadedSquareAnnotation).Bounds.Y, PdfGraphicsUnit.Point);
float width = unitconverter.ConvertToPixels((doc.Pages[0].Annotations[0] as PdfLoadedSquareAnnotation).Bounds.Width, PdfGraphicsUnit.Point);
float height = unitconverter.ConvertToPixels((doc.Pages[0].Annotations[0] as PdfLoadedSquareAnnotation).Bounds.Height, PdfGraphicsUnit.Point);
System.Drawing.Rectangle newRect = new System.Drawing.Rectangle((int)x, (int)y, (int)width, (int)height);
//Extract the first image in the document
Bitmap bitmap = doc.ExportAsImage(0);
//Get clone of the image within the calculated bounds
Bitmap cloneBitmap = bitmap.Clone(newRect, bitmap.PixelFormat);
//Save image
cloneBitmap.Save("ExportedImage.png");

 

VB.NET

'Load the PDF document
Dim doc As PdfLoadedDocument = New PdfLoadedDocument("input.pdf")
'Create a new instance for unit converter to convert points to pixels
Dim unitconverter As PdfUnitConvertor = New PdfUnitConvertor()
'Calculate the extraction bounds from the bounds of loaded annotation
Dim x As Single = unitconverter.ConvertToPixels(CType(doc.Pages(0).Annotations(0), PdfLoadedSquareAnnotation).Bounds.X, PdfGraphicsUnit.Point)
Dim y As Single = unitconverter.ConvertToPixels(CType(doc.Pages(0).Annotations(0), PdfLoadedSquareAnnotation).Bounds.Y, PdfGraphicsUnit.Point)
Dim width As Single = unitconverter.ConvertToPixels(CType(doc.Pages(0).Annotations(0), PdfLoadedSquareAnnotation).Bounds.Width, PdfGraphicsUnit.Point)
Dim height As Single = unitconverter.ConvertToPixels(CType(doc.Pages(0).Annotations(0), PdfLoadedSquareAnnotation).Bounds.Height, PdfGraphicsUnit.Point)
Dim newRect As System.Drawing.Rectangle = New System.Drawing.Rectangle(CType(x, Integer), CType(y, Integer), CType(width, Integer), CType(height, Integer))
'Extract the first image in the document
Dim bitmap As Bitmap = doc.ExportAsImage(0)
'Get clone of the image within the calculated bounds
Dim cloneBitmap As Bitmap = bitmap.Clone(newRect, bitmap.PixelFormat)
'Save image
cloneBitmap.Save("ExportedImage.png")

 

A complete working sample can be downloaded from ExportPartOfImageFromPDF.zip.

By executing the program, you will get the image as follows. Screenshot of output image

Take a moment to peruse the documentation for working with images, where you will find other options like inserting, replacing image in PDF document, image pagination, and image masking.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to inserting images into PDF document

An online sample link to replacing images in PDF document

An online sample link to image extraction from PDF document

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied