User Tools

Site Tools


annual_school_census_pdf_developer_manual

Annual School Census PDF Developer Manual

More like notes for now. Technology for the new version of the PDF surveys are developed in iText C#.

Lookups Handling

A nice pdf way to handle these lookups would be to convert them objects of type PdfArray, and store these arrays as indirect objects. Then, in dropdown lists, you can refer to the PdfArray in the /Opt key of the dropdown, potentially reusing the array in each and every dropdown that needs them. This eliminates repetition, and simplifies the generation of the dropdowns.

Here’s some sample code from ChatGPT which transforms the JSON returned by lookups/core into a Dictionary of PdfArrays - you could then get the required lookup set from this Dictionary as a PDfArray, and assign it to the /Opt property of a dropdown

using System;
using System.Collections.Generic;
using iText.Kernel.Pdf;
using iText.Forms;
using iText.Forms.Fields;
using Newtonsoft.Json.Linq;
 
class Program
{
    static void Main()
    {
        // Sample JSON
        string json = "{\"schoolTypes\":[{\"C\":\"STECE\",\"N\":\"ECE\"},{\"C\":\"ST6-8\",\"N\":\"Grade 6-8\"},{\"C\":\"STK-9\",\"N\":\"Grade ECE-9\"}]}";
 
        // Parse JSON
        JObject parsedJson = JObject.Parse(json);
 
        // Create a PDF document
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter("DropdownWithMakeIndirect.pdf"));
 
        // Add a page for demonstration
        pdfDoc.AddNewPage();
 
        // Create a dictionary to hold the indirect references
        Dictionary<string, PdfObject> pdfArrayIndirectObjects = new Dictionary<string, PdfObject>();
 
        foreach (var property in parsedJson.Properties())
        {
            // Create a PdfArray for the current property
            PdfArray outerArray = new PdfArray();
 
            foreach (var item in property.Value)
            {
                // Create a PdfArray for each item (C, N)
                PdfArray innerArray = new PdfArray();
                innerArray.Add(new PdfString(item["C"]?.ToString()));
                innerArray.Add(new PdfString(item["N"]?.ToString()));
 
                // Add the inner array to the outer array
                outerArray.Add(innerArray);
            }
 
            // Make the outer array indirect using MakeIndirect
            PdfObject indirectObject = outerArray.MakeIndirect(pdfDoc);
            pdfArrayIndirectObjects[property.Name] = indirectObject;
        }
 
        // Example: Use the schoolTypes entry to set the /Opt key of a dropdown field
        if (pdfArrayIndirectObjects.ContainsKey("schoolTypes"))
        {
            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
 
            // Create a dropdown field
            PdfFormField dropdown = PdfFormField.CreateComboBox(pdfDoc, new iText.Kernel.Geom.Rectangle(100, 700, 200, 20), "dropdownField", "");
 
            // Assign the indirect array reference to the /Opt key
            dropdown.Put(PdfName.Opt, pdfArrayIndirectObjects["schoolTypes"]);
 
            // Add the field to the form
            form.AddField(dropdown);
        }
 
        // Close the document
        pdfDoc.Close();
 
        Console.WriteLine("PDF created with dropdown field using MakeIndirect.");
    }
}
annual_school_census_pdf_developer_manual.txt · Last modified: 2024/11/17 07:21 by ghachey