//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Pdf.AI;
using DsPdfWeb.Demos.Common;
using System.Threading.Tasks;
namespace DsPdfWeb.Demos
{
// This sample uses the PDF AI Assistant (DsPdfAI) to analyze the document content,
// generate a summary of the PDF, and insert it as the first page.
//
// A summary provides a general overview of the document's main points and structure,
// offering a broader and more narrative digest than an abstract.
// See the AiGetAbstract sample for how to generate an abstract of a PDF.
//
// To run this sample locally, set your OpenAI credentials
// using Util.OpenAIToken.
public class AiGetSummary
{
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf"));
doc.Load(fs);
try
{
var a = new OpenAIDocumentAssistant(Util.OpenAIToken);
var task = a.GetSummary(doc);
task.Wait();
var page = doc.Pages.Insert(0);
Util.AddNote("Summary generated using DsPdf AI Assistant\n\n" + task.Result, page);
}
catch (Exception ex)
{
// Make sure you have assigned your OpenAI API key to Util.OpenAIToken:
Util.CreatePdfError(doc, ex.Message);
}
doc.Save(stream);
return doc.Pages.Count;
}
}
}