zcatpdf/examples/bookmarks_demo.zig
reugenio 3826cbaed4 Release v1.0 - Feature Complete PDF Generation Library
Major features added since v0.5:
- PNG support with alpha/transparency (soft masks)
- FlateDecode compression via libdeflate-zig
- Bookmarks/Outline for document navigation
- Bezier curves, circles, ellipses, arcs
- Transformations (rotate, scale, translate, skew)
- Transparency/opacity (fill and stroke alpha)
- Linear and radial gradients (Shading Patterns)
- Code128 (1D) and QR Code (2D) barcodes
- TrueType font parsing (metrics, glyph widths)
- RC4 encryption module (40/128-bit)
- AcroForms module (TextField, CheckBox)
- SVG import (basic shapes and paths)
- Template system (reusable layouts)
- Markdown styling (bold, italic, links, headings, lists)

Documentation:
- README.md: Complete API reference with code examples
- FUTURE_IMPROVEMENTS.md: Detailed roadmap for future development
- CLAUDE.md: Updated to v1.0 release status

Stats:
- 125+ unit tests passing
- 16 demo examples
- 46 source files

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

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

178 lines
6.5 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("zpdf");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
std.debug.print("zpdf - Bookmarks Demo\n", .{});
var doc = pdf.Pdf.init(allocator, .{});
defer doc.deinit();
doc.setTitle("Bookmarks Demo");
doc.setAuthor("zpdf");
// =========================================================================
// 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 zpdf.
\\
\\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 - zpdf 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 zpdf 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 - zpdf 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 - zpdf 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 zpdf 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 - zpdf 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", .{});
}