//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Drawing;usingSystem.Collections.Generic;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Text;usingGrapeCity.Documents.Pdf.AI;usingGrapeCity.Documents.Layout;usingGrapeCity.Documents.Drawing;usingDsPdfWeb.Demos.Common;usingGCTEXT = GrapeCity.Documents.Text;usingGCDRAW = GrapeCity.Documents.Drawing; namespaceDsPdfWeb.Demos{// This sample uses the PDF AI Assistant (DsPdfAI) to extract data from a specified table in a PDF.// The extracted data is then inserted into the first page using TableRenderer, // following the approach demonstrated in the PlainTable sample.//// To run this sample locally, set your OpenAI credentials// using Util.OpenAIToken. publicclassAiGetTable {publicintCreatePDF(Stream stream, int paramsIdx = 0) {returnCreatePDF(stream, GetSampleParamsList()[paramsIdx]); } publicintCreatePDF(Stream stream, string[] sampleParams) {var doc = newGcPdfDocument();usingvar fs = File.OpenRead(Path.Combine("Resources", "PDFs", sampleParams[3])); doc.Load(fs);try {var a = newOpenAIDocumentAssistant(Util.OpenAIToken);var task = a.GetTable(doc, sampleParams[4]); task.Wait(); var page = doc.Pages.Insert(0);var rc = Util.AddNote("Table data extracted using DsPdf AI Assistant", page);DrawTable(task.Result, page.Graphics, rc.X, rc.Bottom + 18, page.Size.Width, page.Size.Height); }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; } staticvoidDrawTable(Table table, GcGraphics g, float x, float y, float pageWidth, float pageHeight) {var host = newLayoutHost();var view = host.CreateView(pageWidth, pageHeight); var rt = view.CreateRect(); rt.AnchorTopLeft(null, y, x); var ta = newTableRenderer(g, rt, FixedTableSides.TopLeft, rowCount: table.Rows.Count, columnCount: table.Cols.Count, gridLineColor: Color.DarkGray, gridLineWidth: 1, rowMinHeight: 10); ta.DefaultCellStyle = newCellStyle {MaxWidth = pageWidth / table.Cols.Count, // A guess assuming that some columns will be narrowTextAlignment = TextAlignment.Leading,PaddingTopBottom = 3,PaddingLeftRight = 4,FixedWidth = false,TextFormat = newTextFormat {Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf")),FontSize = 16,FontSizeInGraphicUnits = true } }; for (int i = 0; i < table.Rows.Count; ++i) {for (int j = 0; j < table.Cols.Count; ++j) { ta.AddCell(i, j, table.Rows[i].Cells[j].Text); } } ta.ApplyCellConstraints(); ta.Render(); } publicstaticList<string[]> GetSampleParamsList() {// Items are: name, description, info, PDF name, AI request:return [newstring[] { "@ai-table/Invoice Items", "Get items data from a table in an invoice document", "","zugferd-invoice.pdf", "Get rows 1 to 5 from the table in the document." },newstring[] { "@ai-table/Projected Payments", "Get 'Projected Payments' table from a loan closure document", "","LoanClosure.pdf", "Get the 'Projected Payments' table from the document." },newstring[] { "@ai-table/Expenses (Top 3 Rows)", "Get the first three rows of a table in an expenses report", "","ExpenseReport.pdf", "Get the first three rows of 'Expenses' table from the document." }, ]; } }}