How to Automatically Search for Text After Loading a PDF from a Base64 String in Blazor
How to Automatically Search for Text After Loading a PDF from a Base64 String in Blazor
Description:
This article explains the steps to automatically search for text in a PDF after loading it from a base64 string in a Blazor application using Syncfusion’s PDF Viewer. It walks through loading a PDF from a base64-encoded string, searching for specific text, and displaying the results.
Solution:
To automatically search for text in a PDF after loading it from a base64 string in Blazor, first load the PDF using the LoadAsync method with the base64 string. After the document is loaded, use SearchTextAsync to search for specific text within the PDF, allowing for an automated text search as soon as the document is ready.
Prerequisites:
- Syncfusion Blazor PDF Viewer Setup: Ensure you have installed and set up the Syncfusion Blazor PDF Viewer component in your Blazor project. If you haven’t already done so, you can follow the Syncfusion Blazor PDF Viewer - Getting Started guide.
- Basic Knowledge of Blazor: Familiarity with Blazor components and how to use them will be helpful in understanding and implementing this solution.
Code Snippet
Index.razor
Use this code snippet to automatically search for text in a PDF after loading it from a base64 string in a Blazor application.
@page "/"
@inject HttpClient Http
@using Syncfusion.Blazor.SfPdfViewer
@using Syncfusion.Blazor.Buttons
<SfButton @onclick="clicked">Load Document</SfButton>
<SfPdfViewer2 DocumentPath="@DocumentPath"
Height="100%"
Width="100%"
@ref="Viewer">
<PdfViewerEvents DocumentLoaded="@DocumentLoaded"></PdfViewerEvents>
</SfPdfViewer2>
@code {
SfPdfViewer2 Viewer;
private string DocumentPath { get; set; } = "";
public async void clicked()
{
byte[] byteArray = System.IO.File.ReadAllBytes("wwwroot/pdf-succinctly.pdf");
string base64String = Convert.ToBase64String(byteArray);
await Viewer.LoadAsync("data:application/pdf;base64," + base64String, null);
}
public async void DocumentLoaded()
{
await Viewer.SearchTextAsync("book", false);
}
}
Key Features Used in the Code:
SearchTextAsync: This feature allows you to search for text within the PDF programmatically after it is loaded.
Document Loaded Event: This event ensures that the search only occurs after the document has fully loaded into the viewer.
Sample:
You can find the full sample code in our GitHub repository
Conclusion:
I hope this article has guided you on how to load a PDF from a base64 string in Blazor and automatically search for text within it using Blazor PDF Viewer. With this setup, you can easily manipulate PDFs in your Blazor applications and add functionality such as automatic text searching to enhance the user experience.
You can refer to Blazor 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 Blazor PDF Viewer 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, Direct-Trac, or feedback portal. We are always happy to assist you!