How to extract a part of the image from a PDF using C# and VB.NET?
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:
- Create a new C# WPF application project.
- Install the Syncfusion.Pdf.Wpf NuGet package as reference to your .NET Framework application from NuGet.org.
- 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
- 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.
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 the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to extract a part of the image from a PDF using C# and VB.NET.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!