Example_79

A product catalog of two pages: the logo, a grid of products with SVG pictures drawn as vectors, the codes and the prices, the colors, the price list made from the same list of products, how to order, and a QR code of the shop; PDF/UA.

Example_79.pdf — the PDF this example creates.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using PDFjet.NET;

/**
 * Example_79.cs
 * A product catalog of two pages, on Letter paper: the logo and the title, a
 * grid of products with a picture, a name, a description, the code and the
 * price of each, the colors the products come in, the price list, how to
 * order, and a QR code of the shop online.
 *
 * A catalog is a finished document that customers receive, not a form. The
 * products are one list, and both the grid and the price list are made from
 * it, so a price is changed in one place; the products without a picture are
 * in the price list only. The grid places three products to a row, each in a
 * column of the same width, and starts the next row under the tallest
 * product of the row, as a longer description pushes it down. The codes and
 * the prices are in Cascadia Mono, a monospace font, so that the characters
 * of the codes line up from row to row of the price list.
 *
 * The pictures here are small drawings, SVG files in
 * resources/images/catalog that SVGImage draws as vectors, sharp at any
 * size, scaled to the width of a column. A real catalog has photos: new
 * Image(pdf, stream, ImageType.JPG) in place of SVGImage, with the same
 * SetAltDescription. The logo is an SVG, as logos usually are.
 *
 * The document is PDF/UA-1: the title, the sections and the products are
 * headings of three levels, every picture and the QR code is a figure that
 * says what it shows, the price list is a table, and the running header of
 * each page and the color swatches are artifacts. The layout is that of the
 * product catalog template of PDFjet Forms. The studio and its products are
 * made up.
 */
public class Example_79 {
    // A product: its code, name, description, the price in dollars, the
    // number of colors it comes in, and a picture, null for none.
    private sealed class Product {
        internal readonly String code;
        internal readonly String name;
        internal readonly String description;
        internal readonly decimal price;
        internal readonly int colors;
        internal readonly String picture;
        internal readonly String alt;

        internal Product(String code, String name, String description, String price, int colors,
                String picture, String alt) {
            this.code = code;
            this.name = name;
            this.description = description;
            this.price = decimal.Parse(price, CultureInfo.InvariantCulture);
            this.colors = colors;
            this.picture = picture;
            this.alt = alt;
        }
    }

    private const String STUDIO = "Northwind Studio";
    private const String TITLE = "Catalog 2026";
    private const String ORDER_URL = "https://example.com/order";

    // The pictures and the logo are SVG files, drawn as vectors, sharp at any size
    private const String IMAGES = "resources/images/catalog/";

    private static readonly Product[] PRODUCTS = {
        new Product("NB-A5-L", "Lined notebook, A5", "160 pages of lined paper, 100 g/m², "
                + "a navy linen cover.", "24.00", 4, "notebook-lined.svg", "A navy notebook with a cream label"),
        new Product("NB-A5-D", "Dot grid notebook, A5", "160 pages with a 5 mm dot grid, "
                + "for notes and sketches.", "24.00", 4, "notebook-dots.svg", "An open notebook of dotted pages"),
        new Product("MG-350-B", "Stoneware mug", "350 ml, thrown by hand; dishwasher safe.",
                "32.00", 4, "mug.svg", "A brown stoneware mug"),
        new Product("NB-A4-S", "Sketchbook, A4", "80 sheets of 160 g/m² paper for pencil, "
                + "ink and washes.", "36.00", 2, "sketchbook.svg",
                "A sketchbook with a kraft cover and a spiral binding"),
        new Product("MG-250-E", "Espresso cups, two", "90 ml each, in a teal glaze, with their "
                + "saucers.", "38.00", 3, "espresso-cups.svg", "Two small teal cups on saucers"),
        new Product("BW-180-S", "Serving bowl, 18 cm", "Wide and shallow, for salads and fruit; "
                + "oven safe.", "58.00", 2, "serving-bowl.svg", "A wide sand-colored bowl with a teal rim"),
        new Product("NB-A6-P", "Pocket notebook, A6, three", "", "28.00", 4, null, null),
        new Product("GC-050", "Gift card", "", "50.00", 0, null, null),
    };

    // The colors the covers and the glazes come in, as on the swatches
    private static readonly String[] COLOR_NAMES = {"Navy", "Teal", "Sand", "Clay"};
    private static readonly int[] COLORS = {0x1d3557, 0x0c727d, 0xf4eee4, 0x6e4a2e};

    private const int NAVY = 0x1d3557;
    private const int INK = 0x333333;
    private const int SAND = 0xf4eee4;
    private const int CLAY = 0x6e4a2e;
    private const int RULE = 0xdde1e8;

    private const float LEFT = 40f;
    private const float WIDTH = 532f;            // Letter, 612 wide, less the margins
    private const float GAP = 12f;
    private const int PER_ROW = 3;
    private const float COLUMN = (WIDTH - (PER_ROW - 1) * GAP) / PER_ROW;

    private Font sans;
    private Font sansBold;
    private Font serif;
    private Font serifItalic;
    private Font mono;

    public Example_79() {
        PDF pdf = new PDF(new BufferedStream(
                new FileStream("Example_79.pdf", FileMode.Create)));
        pdf.SetCompliance(Compliance.PDF_UA_1);
        pdf.SetTitle(TITLE);
        pdf.SetAuthor(STUDIO);
        pdf.SetLanguage("en-CA");

        sans = new Font(pdf, IBMPlexSans.Regular);
        sans.SetSize(10f);
        sansBold = new Font(pdf, IBMPlexSans.SemiBold);
        sansBold.SetSize(10f);                  // The headings set their own size
        serif = new Font(pdf, IBMPlexSerif.Regular);
        serif.SetSize(11f);
        serifItalic = new Font(pdf, IBMPlexSerif.Italic);
        serifItalic.SetSize(13f);
        mono = new Font(pdf, CascadiaMono.Regular);
        mono.SetSize(10f);

        List<Page> pages = new List<Page>();

        // Page 1: the logo, the title and a line about the studio, then the
        // products with a picture, three to a row
        Page page = new Page(pdf, Letter.PORTRAIT, Page.DETACHED);
        pages.Add(page);
        SVGImage logo = new SVGImage(IMAGES + "northwind-logo.svg");
        logo.SetAltDescription(STUDIO);
        logo.SetLocation(LEFT, 52f);
        logo.DrawOn(page);
        float x = LEFT + WIDTH / 4f + GAP;
        Heading(page, TITLE, StructElem.H1, 26f, x, 50f);
        TextBlock tagline = new TextBlock(serifItalic,
                "Paper goods and ceramics, made by hand in our studio by the lake.");
        tagline.SetTextColor(INK);
        tagline.SetLocation(x, 86f);
        tagline.SetWidth(LEFT + WIDTH - x);
        float y = tagline.DrawOn(page)[1] + 18f;
        Rule(page, y);

        y = Heading(page, "Notebooks and mugs", StructElem.H2, 16f, LEFT, y + 20f);
        y = Paragraph(page, "Every notebook is sewn by hand and opens flat, on paper heavy enough for "
                + "ink. Our mugs are thrown on the wheel and glazed in small batches, so no two are "
                + "quite alike. Each item has its code and its price in Canadian dollars.", LEFT, y, WIDTH);

        int column = 0;
        float rowTop = y + 18f;
        float rowEnd = rowTop;
        foreach (Product product in PRODUCTS) {
            if (product.picture == null) {
                continue;
            }
            float productEnd = DrawProduct(page, product, LEFT + column * (COLUMN + GAP), rowTop);
            rowEnd = Math.Max(rowEnd, productEnd);
            if (++column == PER_ROW) {
                column = 0;
                rowTop = rowEnd + 22f;
            }
        }

        // Page 2: the colors, the price list of all the products, and how to order
        page = new Page(pdf, Letter.PORTRAIT, Page.DETACHED);
        pages.Add(page);
        y = Heading(page, "Colors", StructElem.H2, 16f, LEFT, 52f);
        y = Paragraph(page, "Covers and glazes come in four colors; the price list says how many each "
                + "item has. Name the color with the code when you order.", LEFT, y, WIDTH);
        y = DrawSwatches(page, y + 14f);

        y = Heading(page, "Price list", StructElem.H2, 16f, LEFT, y + 30f);
        y = DrawPriceList(page, y + 4f);

        // How to order on the left, the QR code of the shop online on the right
        float qrX = LEFT + 0.75f * WIDTH + GAP;
        float top = y + 30f;
        float end = Heading(page, "Ordering", StructElem.H3, 12f, LEFT, top);
        Paragraph(page, "Prices in Canadian dollars, taxes extra. Orders over $150 ship free within "
                + "Canada. Write to orders@example.com with the codes and colors you would like, and "
                + "we send an invoice within a day.", LEFT, end, 0.75f * WIDTH - GAP);
        end = Heading(page, "Order online", StructElem.H3, 12f, qrX, top);
        QRCode qr = new QRCode(ORDER_URL, ErrorCorrectionLevel.M);
        qr.SetModuleLength(2.5f);
        qr.SetAltDescription("QR code of " + ORDER_URL);
        qr.SetLocation(qrX, end + 4f);
        qr.DrawOn(page);

        // The running header of each page, now that their number is known
        for (int i = 0; i < pages.Count; i++) {
            DrawHeader(pages[i], i + 1, pages.Count);
        }
        pdf.AddPages(pages);
        pdf.Complete();
    }

    // Draws a product in its column, its top at y: the picture, the name, a
    // heading under the section, the description, and the code and the
    // price. Returns where it ended.
    private float DrawProduct(Page page, Product product, float x, float y) {
        // The drawings are 300 wide, scaled to the width of the column
        SVGImage picture = new SVGImage(IMAGES + product.picture);
        picture.ScaleBy(COLUMN / picture.GetWidth());
        picture.SetAltDescription(product.alt);
        picture.SetLocation(x, y);
        y = picture.DrawOn(page)[1];
        y = Heading(page, product.name, StructElem.H3, 12f, x, y + 14f);
        y = Paragraph(page, product.description, x, y, COLUMN);
        // The code padded to ten characters, so the prices line up from
        // product to product in the monospace font
        String text = String.Format(CultureInfo.InvariantCulture, "{0,-10} ${1}",
                product.code, Money(product.price));
        TextLine price = new TextLine(mono, text);
        price.SetTextColor(INK);
        price.SetLocation(x, y + 18f);
        price.DrawOn(page);
        return y + 18f;
    }

    // Draws a swatch of each color with its name under it, in four columns,
    // its top at y, and returns where the names end. The swatches are
    // decoration; the names say the colors.
    private float DrawSwatches(Page page, float y) {
        float width = (WIDTH - 3 * GAP) / 4f;
        for (int i = 0; i < COLORS.Length; i++) {
            float x = LEFT + i * (width + GAP);
            Rect swatch = new Rect(x, y, width, 40f);
            swatch.SetCornerRadius(6f);
            swatch.SetFillColor(COLORS[i]);
            // The sand swatch is near the color of the paper, so it has a border
            swatch.SetBorderColor(COLORS[i] == SAND ? CLAY : COLORS[i]);
            swatch.DrawOn(page);
            TextLine name = new TextLine(sans, COLOR_NAMES[i]);
            name.SetLocation(x + (width - name.GetWidth()) / 2f, y + 58f);
            name.DrawOn(page);
        }
        return y + 58f;
    }

    // Draws the price list of all the products, its top at y, and returns
    // where it ended: the header in white on navy, the rows on sand, no
    // borders, the codes and the prices in the monospace font.
    private float DrawPriceList(Page page, float y) {
        List<List<Cell>> rows = new List<List<Cell>>();
        rows.Add(Row(sansBold, "Code", "Item", "Colors", "Price"));
        foreach (Product product in PRODUCTS) {
            rows.Add(Row(mono, product.code, product.name,
                    (product.colors == 0) ? "" : product.colors.ToString(),
                    "$" + Money(product.price)));
        }
        for (int i = 1; i < rows.Count; i++) {
            foreach (Cell cell in rows[i]) {
                cell.SetBackgroundColor(SAND);
            }
        }

        Table table = new Table();
        table.SetTableData(rows, 1);
        table.SetHeaderRowStyle(sansBold, Color.white, NAVY);
        table.SetCellBorders(false);
        table.SetCellPadding(4f, 3f);
        table.SetWidth(WIDTH);
        table.SetColumnWidthsInPercent(20f, 50f, 15f, 15f);
        table.SetTextAlignmentInColumn(2, Alignment.CENTER);
        table.SetTextAlignmentInColumn(3, Alignment.RIGHT);
        table.SetLocation(LEFT, y);
        return table.DrawOn(page)[1];
    }

    // The studio and the catalog on the left and the page number on the
    // right, above the content, in small gray type. It repeats on every
    // page, so it is an artifact.
    private void DrawHeader(Page page, int number, int count) {
        TextLine left = new TextLine(sans, STUDIO + " · " + TITLE);
        left.SetFontSize(9f);
        left.SetTextColor(Color.dimgray);
        left.SetLocation(LEFT, 26f);
        TextLine right = new TextLine(sans, "Page " + number + " of " + count);
        right.SetFontSize(9f);
        right.SetTextColor(Color.dimgray);
        right.SetLocation(LEFT + WIDTH - right.GetWidth(), 26f);
        page.AddArtifactBMC();
        left.DrawOn(page);
        right.DrawOn(page);
        page.AddEMC();
    }

    // Draws a heading in navy, its top at y, and returns where the text
    // under it starts.
    private float Heading(Page page, String text, StructElem level, float size, float x, float y) {
        TextLine heading = new TextLine(sansBold, text);
        heading.SetStructureType(level);
        heading.SetFontSize(size);
        heading.SetTextColor(NAVY);
        heading.SetLocation(x, y + heading.GetAscent());
        heading.DrawOn(page);
        return y + heading.GetAscent() + 10f;
    }

    // Draws a paragraph in the serif font, its top at y, of the width, and
    // returns where it ended.
    private float Paragraph(Page page, String text, float x, float y, float width) {
        TextBlock block = new TextBlock(serif, text);
        block.SetTextColor(INK);
        block.SetLineSpacing(1.15f);
        block.SetLocation(x, y);
        block.SetWidth(width);
        return block.DrawOn(page)[1];
    }

    // A thin line across the page, decoration only, so an artifact that a
    // screen reader skips.
    private static void Rule(Page page, float y) {
        Line line = new Line(LEFT, y, LEFT + WIDTH, y);
        line.SetStrokeColor(RULE);
        page.AddArtifactBMC();
        line.DrawOn(page);
        page.AddEMC();
    }

    // A row of the table, a cell in the font for each text.
    private static List<Cell> Row(Font font, params String[] texts) {
        List<Cell> row = new List<Cell>();
        foreach (String text in texts) {
            row.Add(new Cell(font, text));
        }
        return row;
    }

    // The amount with two decimals and commas between the thousands, as
    // 1,200.00. The culture is given, or a computer set to French writes
    // 1 200,00.
    private static String Money(decimal amount) {
        return amount.ToString("N2", CultureInfo.InvariantCulture);
    }

    public static void Main(String[] args) {
        Stopwatch sw = Stopwatch.StartNew();
        long time0 = sw.ElapsedMilliseconds;
        new Example_79();
        long time1 = sw.ElapsedMilliseconds;
        sw.Stop();
        Console.WriteLine($"Example_79 => {time1 - time0,4} ms");
    }
}   // End of Example_79.cs

©2026 PDFjet Software.