zcatpdf/examples/bookmarks_demo.zig
reugenio f09922076f refactor: Rename zpdf to zcatpdf for consistency with zcat* family
- Renamed all references from zpdf to zcatpdf
- Module import: @import("zcatpdf")
- Consistent with zcatui, zcatgui naming convention
- All lowercase per Zig standards

Note: Directory rename (zpdf -> zcatpdf) and Forgejo repo rename
should be done manually after this commit.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-09 02:10:57 +01:00

178 lines
6.6 KiB
Zig

//! Bookmarks Demo - PDF Document Outline
//!
//! Demonstrates how to add bookmarks (outline) to a PDF document.
//! The bookmarks appear in the sidebar of PDF readers for navigation.
const std = @import("std");
const pdf = @import("zcatpdf");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
std.debug.print("zcatpdf - Bookmarks Demo\n", .{});
var doc = pdf.Pdf.init(allocator, .{});
defer doc.deinit();
doc.setTitle("Bookmarks Demo");
doc.setAuthor("zcatpdf");
// =========================================================================
// Page 1: Introduction
// =========================================================================
{
const page = try doc.addPage(.{});
page.setMargins(50, 50, 50);
try page.setFont(.helvetica_bold, 28);
page.setFillColor(pdf.Color.rgb(41, 98, 255));
page.setXY(50, 800);
try page.cell(0, 35, "Document with Bookmarks", pdf.Border.none, .center, false);
try doc.addBookmarkAt("Introduction", 0, 800);
page.ln(50);
try page.setFont(.helvetica, 12);
page.setFillColor(pdf.Color.black);
const intro_text =
\\This document demonstrates the bookmark/outline feature of zcatpdf.
\\
\\Bookmarks (also called "outlines") appear in the sidebar of PDF readers
\\and allow quick navigation to different sections of the document.
\\
\\Click on the bookmarks in the sidebar to jump to each section.
;
try page.multiCell(500, null, intro_text, pdf.Border.none, .left, false);
// Footer
page.setXY(50, 50);
try page.setFont(.helvetica, 9);
page.setFillColor(pdf.Color.medium_gray);
try page.cell(0, 15, "Page 1 of 4 - zcatpdf Bookmarks Demo", pdf.Border.none, .center, false);
}
// =========================================================================
// Page 2: Chapter 1
// =========================================================================
{
const page = try doc.addPage(.{});
page.setMargins(50, 50, 50);
try page.setFont(.helvetica_bold, 24);
page.setFillColor(pdf.Color.rgb(0, 128, 0));
page.setXY(50, 800);
try page.cell(0, 30, "Chapter 1: Getting Started", pdf.Border.none, .left, false);
try doc.addBookmarkAt("Chapter 1: Getting Started", 1, 800);
page.ln(40);
try page.setFont(.helvetica, 12);
page.setFillColor(pdf.Color.black);
const chapter1_text =
\\This is Chapter 1 of our document.
\\
\\In this chapter, we cover the basics of using zcatpdf to create
\\PDF documents with bookmarks for easy navigation.
\\
\\Key topics:
\\ - Creating a new PDF document
\\ - Adding pages
\\ - Setting fonts and colors
\\ - Adding bookmarks
;
try page.multiCell(500, null, chapter1_text, pdf.Border.none, .left, false);
// Footer
page.setXY(50, 50);
try page.setFont(.helvetica, 9);
page.setFillColor(pdf.Color.medium_gray);
try page.cell(0, 15, "Page 2 of 4 - zcatpdf Bookmarks Demo", pdf.Border.none, .center, false);
}
// =========================================================================
// Page 3: Chapter 2
// =========================================================================
{
const page = try doc.addPage(.{});
page.setMargins(50, 50, 50);
try page.setFont(.helvetica_bold, 24);
page.setFillColor(pdf.Color.rgb(128, 0, 128));
page.setXY(50, 800);
try page.cell(0, 30, "Chapter 2: Advanced Features", pdf.Border.none, .left, false);
try doc.addBookmarkAt("Chapter 2: Advanced Features", 2, 800);
page.ln(40);
try page.setFont(.helvetica, 12);
page.setFillColor(pdf.Color.black);
const chapter2_text =
\\This is Chapter 2 of our document.
\\
\\In this chapter, we explore more advanced features including:
\\ - Images (JPEG and PNG with transparency)
\\ - Tables with custom styling
\\ - Links (external URLs and internal navigation)
\\ - Compression for smaller file sizes
\\
\\These features combine to create professional-quality PDF documents.
;
try page.multiCell(500, null, chapter2_text, pdf.Border.none, .left, false);
// Footer
page.setXY(50, 50);
try page.setFont(.helvetica, 9);
page.setFillColor(pdf.Color.medium_gray);
try page.cell(0, 15, "Page 3 of 4 - zcatpdf Bookmarks Demo", pdf.Border.none, .center, false);
}
// =========================================================================
// Page 4: Conclusion
// =========================================================================
{
const page = try doc.addPage(.{});
page.setMargins(50, 50, 50);
try page.setFont(.helvetica_bold, 24);
page.setFillColor(pdf.Color.rgb(255, 128, 0));
page.setXY(50, 800);
try page.cell(0, 30, "Conclusion", pdf.Border.none, .left, false);
try doc.addBookmarkAt("Conclusion", 3, 800);
page.ln(40);
try page.setFont(.helvetica, 12);
page.setFillColor(pdf.Color.black);
const conclusion_text =
\\This concludes our demonstration of PDF bookmarks.
\\
\\Summary of what we've learned:
\\ 1. Bookmarks provide easy document navigation
\\ 2. They appear in the PDF reader sidebar
\\ 3. Each bookmark links to a specific page and position
\\
\\Use doc.addBookmark() or doc.addBookmarkAt() to add bookmarks
\\to your zcatpdf documents.
;
try page.multiCell(500, null, conclusion_text, pdf.Border.none, .left, false);
// Footer
page.setXY(50, 50);
try page.setFont(.helvetica, 9);
page.setFillColor(pdf.Color.medium_gray);
try page.cell(0, 15, "Page 4 of 4 - zcatpdf Bookmarks Demo", pdf.Border.none, .center, false);
}
// Save
const filename = "bookmarks_demo.pdf";
try doc.save(filename);
std.debug.print("Created: {s} ({d} pages, {d} bookmarks)\n", .{
filename,
doc.pageCount(),
doc.bookmarkCount(),
});
std.debug.print("Done! Open the PDF and check the sidebar for bookmarks.\n", .{});
}