How to Search 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 Blazor 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:
We hope this article has guided you on how to search text after loading a PDF from a base64 string in a Blazor application using a Blazor PDF Viewer.
You can refer to Blazor PDF Viewer feature tour page to learn about its other groundbreaking features, documentation, and how to quickly get started with configuration specifications. You can also explore our Blazor PDF Viewer example to understand how to create and manipulate data.
For current customers, our Blazor components are available on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to evaluate our Blazor PDF Viewer and other Blazor components.
If you have any questions 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!