zcatpdf/examples/pagination_demo.zig
reugenio 236cf7f328 feat: v0.4 - Utilities (Table, Pagination, Headers/Footers, Links)
Phase 4 Implementation:
- Table helper (src/table.zig):
  - Table.init() with TableOptions (col_widths, colors, fonts)
  - header(), row(), rowStyled(), footer() methods
  - setColumnAlign() for per-column alignment
  - separator() and space() utilities

- Pagination module (src/pagination.zig):
  - Pagination.addPageNumbers() with {PAGE}/{PAGES} format
  - addHeader() with optional separator line
  - addFooter() and addFooterWithLine()
  - Position enum (bottom_left, bottom_center, etc.)

- Links visual styling (src/links.zig, src/page.zig):
  - PageLinks struct for link storage
  - Page.drawLink() - blue underlined text
  - Page.writeLink() - link at current position

- Examples:
  - table_demo.zig - 3 table styles (product, invoice, employee)
  - pagination_demo.zig - 5 pages with headers/footers/numbers

~70 tests passing, 6 examples working.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-08 20:45:33 +01:00

115 lines
3.8 KiB
Zig

//! Pagination Demo - Demonstrates automatic page numbering and footers
//!
//! Shows how to add page numbers and footers to multi-page documents.
const std = @import("std");
const pdf = @import("zpdf");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
std.debug.print("zpdf - Pagination Demo\n", .{});
var doc = pdf.Pdf.init(allocator, .{});
defer doc.deinit();
doc.setTitle("Pagination Demo");
doc.setAuthor("zpdf");
// Create multiple pages with content
const num_pages: usize = 5;
for (0..num_pages) |i| {
var page = try doc.addPage(.{});
page.setMargins(50, 50, 50);
// Page title
try page.setFont(.helvetica_bold, 24);
page.setFillColor(pdf.Color.rgb(41, 98, 255));
page.setXY(50, 780);
var title_buf: [64]u8 = undefined;
const title = std.fmt.bufPrint(&title_buf, "Chapter {d}", .{i + 1}) catch "Chapter";
try page.cell(0, 30, title, pdf.Border.none, .center, false);
page.ln(50);
// Content
try page.setFont(.helvetica, 12);
page.setFillColor(pdf.Color.black);
const lorem =
\\Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor
\\incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
\\nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\\Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
\\eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
\\in culpa qui officia deserunt mollit anim id est laborum.
;
// Multiple paragraphs to fill the page
for (0..4) |_| {
try page.multiCell(500, null, lorem, pdf.Border.none, .left, false);
page.ln(15);
}
// Section heading
try page.setFont(.helvetica_bold, 14);
page.setFillColor(pdf.Color.rgb(51, 51, 51));
try page.cell(0, 20, "Key Points", pdf.Border.none, .left, false);
page.ln(25);
// Bullet points
try page.setFont(.helvetica, 11);
page.setFillColor(pdf.Color.black);
const points = [_][]const u8{
"First important point about this chapter",
"Second key consideration to remember",
"Third aspect worth noting",
"Fourth element of significance",
};
for (points) |point| {
var point_buf: [256]u8 = undefined;
const bullet_text = std.fmt.bufPrint(&point_buf, " * {s}", .{point}) catch point;
try page.cell(0, 16, bullet_text, pdf.Border.none, .left, false);
page.ln(18);
}
}
// Add header to all pages (with line separator)
try pdf.addHeader(&doc, "zpdf Library - Multi-Page Document Example", .{
.alignment = .center,
.margin = 30,
.font_size = 9,
.color = pdf.Color.medium_gray,
.draw_line = true,
.line_color = pdf.Color.light_gray,
});
// Add page numbers to all pages
try pdf.Pagination.addPageNumbers(&doc, .{
.format = "Page {PAGE} of {PAGES}",
.position = .bottom_center,
.font = .helvetica,
.font_size = 9,
.color = pdf.Color.medium_gray,
});
// Add footer text to all pages (with line separator)
try pdf.addFooterWithLine(&doc, "Confidential Document - For Internal Use Only", .{
.alignment = .left,
.margin = 30,
.font_size = 8,
.color = pdf.Color.light_gray,
.draw_line = true,
});
// Save
const filename = "pagination_demo.pdf";
try doc.save(filename);
std.debug.print("Created: {s} ({d} pages)\n", .{ filename, num_pages });
std.debug.print("Done!\n", .{});
}