Skip to content
Home » OpenAI and PDF

OpenAI and PDF

Rambo once said “It’s a long story,” which was a short summary of the entire Rambo movies.

With Open AI, this should now be possible.
So go for nostalgic reasons I will make an ajax with a web service – quisi almost a micoservice 🙂
So let’s do the upload.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Services.Description;
using System.Web.UI.WebControls;

namespace GPT3PDF
    /// <summary>
    /// Zusammenfassungsbeschreibung für AnaylseService
    /// </summary>
    [WebService(Namespace = "")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // Wenn der Aufruf dieses Webdiensts aus einem Skript zulässig sein soll, heben Sie mithilfe von ASP.NET AJAX die Kommentarmarkierung für die folgende Zeile auf. 
    public class AnaylseService : System.Web.Services.WebService

        public string HelloWorld()
            return "Hello World";

        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        public PDFResponse Upload()

            HttpFileCollection Files = HttpContext.Current.Request.Files;

            HttpPostedFile File = Files[0];
            byte[] pdfBytes;

            using (var binaryReader = new BinaryReader(File.InputStream))
                pdfBytes = binaryReader.ReadBytes(File.ContentLength);

                PDFAnalysis pDFAnalysis = new PDFAnalysis();
                string extract = pDFAnalysis.AnalyzePDF(pdfBytes);
                return new PDFResponse() { Message = extract };
            catch (Exception ex)
              //Error do something with it or leave it :)  


            return new PDFResponse() { Message = "Error! " };


    public class PDFResponse
        public string Message { get; set; } 

Now we just need to get the text from the PDF. Fortunately, we don’t have to reinvent the wheel and can use PdfPig | Read and extract text and other content from PDFs in C# (port of PDFBox) ( for example.
Extracting is quite simple:

  private string ExtractTextFromPDF(byte[] file)

            string extractedText;
            using (var pdf = PdfDocument.Open(file))
                var sb = new StringBuilder();
                for (var i = 0; i < pdf.NumberOfPages; i++)
                    var page = pdf.GetPage(i + 1);
                extractedText = sb.ToString();

            return extractedText;

We pass the file as byte[] so that we don’t have to store it somewhere on the server.

Nostalgic upload and extract text is easy right?

So next comes the fun part. The call to the OpenAI Api.

  private string AnalyzeTextWithChatGpt(string extractedText)
            string responseContent = string.Empty;
                List<MessageObj> roles = new List<MessageObj>();
                roles.Add(new MessageObj() { role = "system", content = "Summarize the text" });
                roles.Add(new MessageObj() { role = "user", content = extractedText });
                //MessageObj[] msg;

                // Set up the OpenAI API client
                var content = new StringContent(
                model = "gpt-3.5-turbo",
                messages = roles


                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + chatGptApiKey);
                //var response = client.PostAsync("", content).Result;

                var response = client.PostAsync("", content).Result;

                responseContent = response.Content.ReadAsStringAsync().Result;

                var explanation = JsonConvert.DeserializeObject<dynamic>(responseContent).choices[0].message.content;
                return explanation;
            catch (Exception ex)

                Exception exception = new Exception(responseContent, ex);
                throw exception;


The whole source is at gpiwonka/GPT3PDF ( Of course, it lacks proper error handling and also a procedure if the PDF is too large. For this, the PDF would have to be split and then each individual part would have to be summarized. View live at:


Leave a Reply

Your email address will not be published. Required fields are marked *