AiBuildOutlines.cs
// 
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// © 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
{
    // このサンプルは PDF AIアシスタント (DsPdfAI) を使用して文書内容を解析し、
    // 推定された見出しに基づいてアウトラインツリー(ブックマーク)を生成し、
    // それをPDFに追加します。
    //
    // このサンプルをローカルで実行するには、
    // Util.OpenAIToken、Util.AzureOpenAIToken、および Util.AzureEndPoint プロパティを使用して
    // OpenAI の認証情報と Azure のエンドポイントを設定してください。

    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 AzureOpenAIDocumentAssistant(Util.AzureEndPoint, Util.AzureOpenAIToken);
                var task = a.BuildOutlines(doc);
                task.Wait();
                doc.PageMode = PageMode.UseOutlines;
            }
            catch (Exception ex)
            {
                // エンドポイント、APIキーの割り当てを確認してください。
                Util.CreatePdfError(doc, ex.Message);
            }
            doc.Save(stream);
            return doc.Pages.Count;
        }

        public static List<string[]> GetSampleParamsList()
        {
            // 項目は次の通りです: name, description, info, PDF name:
            return new List<string[]>()
            {
                new string[] { "@ai-outlines/Newsletter", "DsPdfAIを使ってニュースレターの目次を作成する", "",
                    "Newsletter.pdf" },
                new string[] { "@ai-outlines/Financial Report", "DsPdfAIを使って財務報告書の目次を作成する", "",
                    "FinancialReport.pdf" },
                new string[] { "@ai-outlines/Lease Proposal", "DsPdfAIを使って賃貸契約提案書の概要を作成する", "",
                    "Commercial_Real_Estate_Lease_Proposal.pdf" },
                new string[] { "@ai-outlines/Lease Agreement", "DsPdfAIを使って賃貸借契約書の概要を作成する", "",
                    "LeaseAgreement.pdf" },
            };
        }
    }
}