An example of using GitHub Copilot to generate code. In particular to generate Pdf documents from tabular data in a WPF app.

This code is part of the AthStitcher app that is being developed for photo-timing of athletics meets. The app manages data for Meets, Events, Heats and Lane Results. The data is stored in a Sqlite database via Entity Framework Core.


Nb: With the code links below, when clicked upon, when the browser shows, click on the contents. Wait a moment whilst it navigates to the method.


Given app generates the results for a Heat as text:

string PrintHeat(Meet CurrentMeet, AthStitcher.Data.Event CurrentEvent, Heat CurrentHeat)
{
    if (athStitcherViewModel.DataContext is not AthStitcherGUI.ViewModels.AthStitcherModel vm)
        return "";
    string printHeader = "\n";

    printHeader = $"{CurrentEvent}\t" +
    $"Heat No: {CurrentHeat.HeatNo}\n" +
     "------------------------------------------------------";
    bool tabbed = vm.Scheduling.UseTabbedPrinting;
    List<LaneResult> results = CurrentHeat.Results
        .OrderBy(r => r.ResultSeconds ?? double.MaxValue)  // nulls last
        .ToList();

        printHeader += $"\nPosn\t{LaneResult.TabHeader()}";
        int posn = 1;
        foreach (var lr in results)
        {
            printHeader += "\n" + $"{posn++}\t{lr.ToTab()}";
        }


    printHeader += "\n\n";
    return printHeader;
}

The Heat Pdf

GitHub Copilot in Visual Studio was asked:

Query: Please generate the Heat result as a PDf

Response:

Install-Package QuestPDF

Code:

     public static void ExportHeatToPdf( ... )
    {
        /// <summary>
        /// Exports a single heat to a PDF containing header, optional stitched image and a results table.
        /// </summary>
        public static void ExportHeatToPdf()
        {
           ...
        }

Code in Data/topdf.cs in ExportHeatToPdf()
Note this is the final version of the class file.

There was then code for MainWindow.xaml.cs in PrintOneHeatAsPdf() that calls ExportHeatToPdf() after prompting via a dialog to get the pdf filename and location; the prompt makes a suggestion for the filename given the Meet/Event/Heat context.

The page Header was tweaked for my context (by me):

    page.Header().PaddingBottom(5).Column(column =>
    {
        column.Item().Text($"{meet}").SemiBold().FontSize(16);
        column.Item().Text($"{ev} — Heat {heat.HeatNo}").FontSize(12);
        column.Item().PaddingTop(6).LineHorizontal(1).LineColor(Colors.Grey.Lighten1);
    });

Note that Meet and Event classes have an Override toString() method that presents the required class method properties in one string.

Query: Does this page Header appear on al pages if the context was spread over multiple pages. Answer YES

Response Table headers defined with table.Header(…) are also repeated automatically on subsequent pages when the table breaks across pages**

Query: Requested adding a Posn (Position) column and sorting of Results on Time

Was implemented. in ExportHeatToPdf()
:)

The Event Pdf

Query: OK we can ow print one heat to pdf. Can we now print all heats in aan event, Part of the infrastructure is done. For PDF we call Print_PDF_Click() which has a switch statement for each pdf type. Heat branch is complete calling PrintOneHeatAsPdf(vm.CurrentMeet, vm.CurrentEvent, heat);() When printing heats for Current Event want them to be continuous PrintOneHeatAsPdf(vm.CurrentMeet, vm.CurrentEvent, heat);()

The method was generated:

        /// <summary>
        /// Export all heats for a given event into a single continuous PDF.
        /// Each heat starts on a fresh page; table headers repeat inside each heat's table.
        /// </summary>
        public static void ExportEventToPdf( ... )
        {

Complete Code in Data/ToPdf.cs in ExportEventToPdf()
Note this is the final version of the class file.`

There was then code for MainWindow.xaml.cs in PrintOneHeatAsPdf() that calls ExportEventToPdf().

Meet Pdf

The method was generated:

        /// <summary>
        /// Export all heats for a given event into a single continuous PDF.
        /// Each heat starts on a fresh page; table headers repeat inside each heat's table.
        /// </summary>
        public static void ExportMeetToPdf( ... )
        {

Complete Code in Data/ToPdf.cs in ExportMeetToPdf()
Note this is the final version of the class file.`

There was then code for MainWindow.xaml.cs in PrintOneMeetAsPdf()) that calls ExportMeetToPdf().

More Coming Soon …

Did

`


 TopicSubtopic
   
 This Category Links 
Category:Application Dev Index:Application Dev
<  Prev:   CommunityToolkit.Mvvm