//! 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", .{}); }