How to Find Page Number for the Text in WinForms PDF library?
Syncfusion Essential® WinForms PDF is a .NET PDF Library used to create, read, and edit PDF documents. Using this library, you can find the page number for the text in a PDF using C# and VB.NET.
Steps to find the page number for a text in a PDF programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
-
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf.Parsing; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf.Parsing Imports System.Drawing
- Use the following code snippet to find page number for the text in an existing PDF document.
C#
// Load an existing PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Sample.pdf");
// Load page collections
PdfLoadedPageCollection loadedPages = loadedDocument.Pages;
int pageIndex = -1;
string extractedText = string.Empty;
// Extract text from existing PDF document pages
for (int i = 0; i < loadedPages.Count; i++)
{
PdfPageBase loadedPage = loadedPages[i];
extractedText = loadedPage.ExtractText();
// Check if the extracted text from the page contains the required text
if (extractedText.Contains("Code39Ext"))
{
// Set the page index value
pageIndex = i;
break;
}
}
// Close the document
loadedDocument.Close(true);
VB.NET
' Load an existing PDF document
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Sample.pdf")
' Load page collections
Dim loadedPages As PdfLoadedPageCollection = loadedDocument.Pages
Dim pageIndex As Integer = -1
Dim extractedText As String = String.Empty
' Extract text from an existing PDF document pages
Dim i As Integer = 0
Do While (i < loadedPages.Count)
Dim loadedPage As PdfPageBase = loadedPages(i)
extractedText = loadedPage.ExtractText()
' Check if the extracted text from the page contains the required text
If extractedText.Contains("Code39Ext") Then
' Set the page index value
pageIndex = i
Exit Do
End If
i = i + 1
Loop
' Close the document
loadedDocument.Close(True)
In the previously given code sample, the "pageIndex" value will hold the page number in which the given text is located.
A complete working sample can be downloaded from PdfPageNumber.zip
Take a moment to peruse the documentation, where you will find options like basic layout and bounds based text extraction with code examples.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to find the page number for the text in the WinForms PDF library.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example 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 check out 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!