Skip to content

ui::PrintJob

Header: winui/ui/Printing.hpp.

A document to print: how many pages it has, and how to draw each.

Example:

const std::vector<std::string> lines = {"Sales report", "Q3: 1240 units", "Q4: 1385 units"};
const ui::PrintResult result = ui::print(window, ui::PrintJob{
    .title      = "Report",
    .page_count = [&](const ui::PrintPage&, const ui::PrintContext&)
    { return (lines.size() + 49) / 50; },
    .draw_page  = [&](ui::Renderer& renderer, const ui::PrintPage& page, const ui::PrintContext& context)
    {
        // 50 lines a page, an inch from the edges.
        ui::TextStyle style = context.theme.text_style;
        style.horizontal    = ui::TextAlign::Leading;
        for (std::size_t i = 0; i < 50 && page.index * 50 + i < lines.size(); ++i)
        {
            renderer.draw_text(lines[page.index * 50 + i],
                               ui::Rect{96, 96 + 18.0f * i, page.size.width - 192, 18},
                               ui::colors::Black,
                               style);
        }
    },
});

Field Type Default
title std::string The document's name in the print queue.
page_count Callback<std::size_t(const PrintPage& page, const PrintContext& context)> How many pages the document has on sheets like page (index 0). Called before the dialog shows, for its page range, and again when the user picks other paper.
draw_page Callback<void(Renderer& renderer, const PrintPage& page, const PrintContext& context)> Draws page page.index, from the sheet's top-left. Called for each page the user chose, in order, once per job (the printer makes the copies).
printer std::optional<std::string> A printer to print to without asking, by its name ("Microsoft Print to PDF"); unset shows the print dialog.
output_file std::optional<std::filesystem::path> A file the printer's output goes to instead of the device, as "Print to file" does, such as a PDF from a PDF printer. Unset prints to the device. Setting it shows the classic dialog, since the preview dialog prints only to devices.
dialog PrintDialog PrintDialog::Preview The dialog to ask with. The one with a preview by default. Where the system has no preview dialog, the classic one shows.