Summary

Takes a list of buzzwords as input and uses them to generate randomly-populated Bingo cards in SVG (and optionally PDF) format.

Code (C#)

// Set output directory to which the .svg files are saved
const string outputFolder = @"C:\Output\Buzzword Bingo\";

// Set number of Bingo cards/files to create
const int numberOfCards = 5;

// Define card characteristics (DO NOT CHANGE without modifying
// the SVG template code first)
const int fieldCntX = 5;
const int fieldCntY = 5;
var fieldSize = new System.Drawing.Size(140, 80);
var fieldPosOffset = new System.Drawing.Point(22, 63);

// Create output directory (if necessary)
if (!Directory.Exists(outputFolder))
    Directory.CreateDirectory(outputFolder);

// Make sure enough buzzwords are available to fill a full card
if (Input.Lines.Count < fieldCntX * fieldCntY - 1)
    throw new Exception("Not enough buzzwords available.");

// Create the output files
for (int cardIndex = 0; cardIndex < numberOfCards; cardIndex ++)
{
    // Randomize input list of buzzwords
    Random rnd = new Random();
    Stack<string> buzzwords = new Stack<string>(Input.Lines.OrderBy(a => rnd.NextDouble()));

    // Construct SVG 'text' elements for buzzword
    string fieldsSvgCode = "";
    for (int y = 0; y < fieldCntY; y++)
    {
        for (int x = 0; x < fieldCntX; x++)
        {
            if (x != fieldCntX / 2 || y != fieldCntY / 2)  // Skip the middle "BINGO" field
            {
                int fieldIndex = y * fieldCntY + x;
                
                // Retrieve the next buzzword
                string fieldText = buzzwords.Pop();                    
                
                // Find the maximum text size that still fits into the field bounds
                int fontSize = 26;
                System.Drawing.Size textSize;
                do
                {
                    fontSize--;  // Reduce font size until the buzzword text fits
                    textSize = System.Windows.Forms.TextRenderer.MeasureText(fieldText,
                        new System.Drawing.Font("Sans-serif", fontSize, System.Drawing.GraphicsUnit.Pixel));
                } while (textSize.Width > fieldSize.Width && fontSize >= 1);
                
                // Calculate the coordinates at which to place the text
                int yPos = Convert.ToInt32(fieldPosOffset.Y + (y + 0.5) * fieldSize.Height + textSize.Height / 4);
                int xPos = Convert.ToInt32(fieldPosOffset.X + (x + 0.5) * fieldSize.Width);
                
                // Construct and append SVG code for the card field
                fieldsSvgCode += String.Format("  <text fill=\"#000000\" stroke=\"#000000\""
                    + " stroke-width=\"0\" x=\"{0}\" y=\"{1}\" id=\"text_field_{2}\" font-size=\"{3}\""
                    + " font-family=\"Sans-serif\" text-anchor=\"middle\" xml:space=\"preserve\">{4}</text>",
                    xPos, yPos, fieldIndex, fontSize, fieldText)
                    + Environment.NewLine;
            }
        }
    }

    // Insert 'text' elements into SVG code template (which is provided in the Output editor)
    string svgCode = Output.Text.Replace("<!-- Placeholder -->", fieldsSvgCode.Trim());
    
    // Write SVG code to a .svg file
    string outputFilePath = Path.Combine(outputFolder,
        String.Format("buzzword_bingo_card_{0:00}.svg", cardIndex + 1));
    File.WriteAllText(outputFilePath, svgCode);
    
    // (Optional) Use Inkscape to convert the SVG file into a PDF document
    /*
    Process p = Process.Start(@"C:\Program Files (x86)\Imaging\Inkscape\inkscape.exe",
        String.Format(@"-f ""{0}"" -A ""{0}"".pdf", outputFilePath));
    p.WaitForInputIdle();  // Wait for Inkscape to finish loading
    p.WaitForExit();  // Wait for the process to end
     */
}

// Open output folder
Process.Start(outputFolder); 
Input Type: Plain Text, Output Type: Plain Text

Download Project File

File: Buzzword_Bingo_card_generator.dvp (5.17 KB)

To open this file, DataVoila must be installed on your computer. If this is not yet the case, please click here to download the free demo version.