//
// 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 System.Collections.Generic;
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
// and generate an outline tree (bookmarks) based on inferred headings, which is then added to the PDF.
//
// To run this sample locally, set your OpenAI credentials
// using Util.OpenAIToken.
public class AiBuildOutlines
{
public int CreatePDF(Stream stream, int paramsIdx = 0)
{
return CreatePDF(stream, GetSampleParamsList()[paramsIdx]);
}
public int CreatePDF(Stream stream, string[] sampleParams)
{
var doc = new GcPdfDocument();
using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", sampleParams[3]));
doc.Load(fs);
try
{
var a = new OpenAIDocumentAssistant(Util.OpenAIToken);
var task = a.BuildOutlines(doc);
task.Wait();
doc.PageMode = PageMode.UseOutlines;
}
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;
}
public static List<string[]> GetSampleParamsList()
{
// Items are: name, description, info, PDF name:
return new List<string[]>()
{
new string[] { "@ai-outlines/Newsletter", "Use DsPdfAI to generate outlines for a newsletter", "",
"Newsletter.pdf" },
new string[] { "@ai-outlines/Financial Report", "Use DsPdfAI to generate outlines for a financial report", "",
"FinancialReport.pdf" },
new string[] { "@ai-outlines/Lease Proposal", "Use DsPdfAI to generate outlines for a lease proposal", "",
"Commercial_Real_Estate_Lease_Proposal.pdf" },
new string[] { "@ai-outlines/Lease Agreement", "Use DsPdfAI to generate outlines for a lease agreement", "",
"LeaseAgreement.pdf" },
};
}
}
}