Created
March 5, 2026 10:06
-
-
Save whatsmate/04337598ff7f437ce88a657ffc56fb01 to your computer and use it in GitHub Desktop.
Converting PDF file to text in VB.NET in VS2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Imports System.IO | |
| Imports System.Net | |
| Imports System.Text | |
| Imports System.Text.Json | |
| Namespace PdfTextExtractor | |
| Public Class Program | |
| ' When you have your own client ID and secret, specify them here: | |
| Private Const CLIENT_ID As String = "FREE_TRIAL_ACCOUNT" | |
| Private Const CLIENT_SECRET As String = "PUBLIC_SECRET" | |
| Private Const API_URL As String = "https://api.whatsmate.net/v1/pdf/extract?url=" | |
| Shared Sub Main(ByVal args As String()) | |
| ' TODO: Specify the URL of your small PDF document (less than 1MB and 10 pages) | |
| ' To extract text from bigger PDF document, you need to use the async method. | |
| Dim pdfUrl As String = "https://ospi.k12.wa.us/sites/default/files/2023-08/jokes.pdf" | |
| Try | |
| Dim text As String = ExtractText(pdfUrl) | |
| Console.WriteLine("===============================") | |
| Console.WriteLine("PDF TEXT IS AS FOLLOWS:") | |
| Console.WriteLine(text) | |
| Console.WriteLine("===============================") | |
| Catch ex As Exception | |
| Console.WriteLine($"Error: {ex.Message}") | |
| End Try | |
| Console.WriteLine("Press Enter to exit.") | |
| Console.ReadLine() | |
| End Sub | |
| Shared Function ExtractText(ByVal pdfUrl As String) As String | |
| Dim fullUrl As String = API_URL & Uri.EscapeDataString(pdfUrl) | |
| Dim request As HttpWebRequest = WebRequest.Create(fullUrl) | |
| request.Method = "GET" | |
| request.Headers("X-WM-CLIENT-ID") = CLIENT_ID | |
| request.Headers("X-WM-CLIENT-SECRET") = CLIENT_SECRET | |
| Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse) | |
| Using reader As New StreamReader(response.GetResponseStream()) | |
| Dim jsonResponse As String = reader.ReadToEnd() | |
| ' Parse JSON response using System.Text.Json (modern .NET 6+) | |
| Using doc As JsonDocument = JsonDocument.Parse(jsonResponse) | |
| Dim root As JsonElement = doc.RootElement | |
| If root.TryGetProperty("text", root) Then | |
| Return root.GetString() | |
| ElseIf root.TryGetProperty("error", root) Then | |
| Throw New Exception($"API Error: {root.GetString()}") | |
| Else | |
| Return jsonResponse | |
| End If | |
| End Using | |
| End Using | |
| End Using | |
| End Function | |
| End Class | |
| End Namespace |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment