How to Crop the White Spaces in a PDF Page From the PDF Document Using C# and VB.NET
The Syncfusion Essential PDF is a feature-rich and high-performance .NET PDF library that is used to create, read, and edit the PDF documents programmatically without Adobe dependencies. Using this library, you can crop the white spaces in a PDF page from the PDF document using C# and VB.NET.
Steps to crop the white spaces in a PDF page from the PDF document using C# programmatically
- Create a new C# console application project.
- Install the Syncfusion.PDF.WinForms and EmguCV NuGet packages as a reference to your .NET Framework application from NuGet.org.
- Using the following namespaces in the Program.cs file.
C#
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
Imports Emgu.CV
Imports Emgu.CV.CvEnum
Imports Emgu.CV.Structure
Imports Emgu.CV.Util
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Parsing
- Add the following code sample in Program.cs file to crop the white spaces in a PDF page from the PDF document .
C#
//Load the existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/InputTemplate.pdf");
//Convert page into bitmap.
Bitmap images = loadedDocument.ExportAsImage(0);
//Find the image content area.
Image<Gray, byte> grayImage = new Image<Gray, byte>(images);
var grayScaleImage = grayImage.Convert<Gray, byte>();
var blurredImage = grayScaleImage.SmoothGaussian(5, 5, 0, 0);
UMat cannyImage = new UMat();
CvInvoke.Canny(blurredImage, cannyImage, 50, 150);
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(cannyImage, contours, null, RetrType.Tree, ChainApproxMethod.LinkRuns);
int minX = int.MaxValue, minY = int.MinValue, maxX = 0, maxY = 0;
//Group all rectangle region.
for (int i = 0; i < contours.Size; i++)
{
Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);
if (i == 0)
minY = rect.Y;
if (rect.X < minX)
minX = rect.X;
if (rect.Y < minY)
minY = rect.Y;
if (rect.X + rect.Width > maxX)
maxX = rect.X + rect.Width;
if (rect.Y + rect.Height > maxY)
maxY = rect.Y + rect.Height;
Rectangle group = new Rectangle(minX, minY, minX + maxX, minY + maxY);
}
RectangleF pixelBounds = new RectangleF(minX, minY, maxX - minX, maxY - minY);
//Convert pixel to point.
PdfUnitConvertor pdfUnitConvertor = new PdfUnitConvertor();
RectangleF pointRect = pdfUnitConvertor.ConvertFromPixels(pixelBounds, PdfGraphicsUnit.Point);
//Loaded the existing page.
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
//Create the template from the page.
PdfTemplate template = loadedPage.CreateTemplate();
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Set the document margin.
document.PageSettings.Margins.All = 0;
// Set section orientation based on image size (by default Portrait).
if (pointRect.Width > pointRect.Height)
document.PageSettings.Orientation = PdfPageOrientation.Landscape;
document.PageSettings.Size = new SizeF(pointRect.Width, pointRect.Height);
//Add the page.
PdfPage page = document.Pages.Add();
//Create the graphics.
PdfGraphics graphics = page.Graphics;
//Draw the template.
graphics.DrawPdfTemplate(template, new PointF(-pointRect.X, -pointRect.Y));
//Save the new document.
document.Save("Output.pdf");
//Close the documents.
loadedDocument.Close(true);
Process.Start("Output.pdf");
'Load the existing PDF document.
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Data/InputTemplate.pdf")
'Convert the page into bitmap.
Dim images As Bitmap = loadedDocument.ExportAsImage(0)
'Find the image content area.
Dim grayImage As Image(Of Gray, Byte) = New Image(Of Gray, Byte)(images)
Dim grayScaleImage = grayImage.Convert(Of Gray, Byte)()
Dim blurredImage = grayScaleImage.SmoothGaussian(5, 5, 0, 0)
Dim cannyImage As UMat = New UMat()
CvInvoke.Canny(blurredImage, cannyImage, 50, 150)
Dim contours As VectorOfVectorOfPoint = New VectorOfVectorOfPoint()
CvInvoke.FindContours(cannyImage, contours, Nothing, RetrType.Tree, ChainApproxMethod.LinkRuns)
Dim minX As Integer = Integer.MaxValue, minY As Integer = Integer.MinValue, maxX As Integer = 0, maxY As Integer = 0
'Group all rectangle region.
For i As Integer = 0 To contours.Size - 1
Dim rect As Rectangle = CvInvoke.BoundingRectangle(contours(i))
If i = 0 Then minY = rect.Y
If rect.X < minX Then minX = rect.X
If rect.Y < minY Then minY = rect.Y
If rect.X + rect.Width > maxX Then maxX = rect.X + rect.Width
If rect.Y + rect.Height > maxY Then maxY = rect.Y + rect.Height
Dim group As Rectangle = New Rectangle(minX, minY, minX + maxX, minY + maxY)
Next
Dim pixelBounds As RectangleF = New RectangleF(minX, minY, maxX - minX, maxY - minY)
'Convert pixel to point.
Dim pdfUnitConvertor As PdfUnitConvertor = New PdfUnitConvertor()
Dim pointRect As RectangleF = pdfUnitConvertor.ConvertFromPixels(pixelBounds, PdfGraphicsUnit.Point)
'Loaded the existing page.
Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
'Create the template from the page.
Dim template As PdfTemplate = loadedPage.CreateTemplate()
'Create a new PDF document.
Dim document As PdfDocument = New PdfDocument()
'Set the document margin.
document.PageSettings.Margins.All = 0
'Set section orientation based on image size (by default Portrait).
If pointRect.Width > pointRect.Height Then _
document.PageSettings.Orientation = PdfPageOrientation.Landscape
document.PageSettings.Size = New SizeF(pointRect.Width, pointRect.Height)
'Add the page.
Dim Page As PdfPage = document.Pages.Add()
'Create the graphics.
Dim Graphics As PdfGraphics = Page.Graphics
'Draw the template.
Graphics.DrawPdfTemplate(template, New PointF(-pointRect.X, -pointRect.Y))
'Save the New document.
document.Save("Output.pdf")
'Close the documents.
loadedDocument.Close(True)
By executing the program, you will get the PDF document as follows.
You can download a complete working sample from RemoveWhiteSpaces.zip
Refer to link to explore a rich set of Syncfusion Essential PDF features.
Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or NuGet feed, include a license key in your projects. Refer to this linkto learn about generating and registering the Syncfusion license key in your application to use the components without trail message.
See Also:
https://help.syncfusion.com/file-formats/pdf/working-with-text
https://help.syncfusion.com/file-formats/pdf/working-with-pages
https://www.syncfusion.com/kb/9795/how-to-crop-a-pdf-page-using-c-and-vb-net
https://www.syncfusion.com/kb/9821/how-to-create-pdf-page-based-on-image-size-using-c-and-vb-net