PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Starting with 3.2.0, ComPDF Flutter SDK supports managing headers and footers through CPDFDocument. In these examples, document is an opened document. When using a reader, obtain it from controller.document.
import 'package:compdfkit_flutter/document/cpdf_document.dart';
import 'package:compdfkit_flutter/document/cpdf_header_footer.dart';
import 'package:flutter/material.dart';Use updateHeaderFooter() to set content, positions, and styles. pages contains zero-based page indexes. Margins are measured in PDF points (1/72 inch).
final pageCount = await document.getPageCount();
final success = await document.updateHeaderFooter(
CPDFHeaderFooter(
pages: List<int>.generate(pageCount, (index) => index),
margin: const EdgeInsets.all(36),
slots: const [
CPDFHeaderFooterSlot(
position: CPDFHeaderFooterPosition.headerLeft,
text: 'ComPDF',
fontName: 'Helvetica',
fontSize: 12,
),
CPDFHeaderFooterSlot(
position: CPDFHeaderFooterPosition.footerCenter,
text: 'Page <<1>>',
),
],
),
);
if (success) {
await document.save();
}<<1>> is a page-number placeholder starting at 1. CPDFHeaderFooterPosition provides six positions: left, center, and right in both the header and footer. Each position has its own text, fontName, fontSize, and textColor.
An update replaces the configuration for all six positions. Include every position you want to retain in slots. Use empty text to clear a specific position.
final current = await document.getHeaderFooter();
if (current != null) {
for (final slot in current.slots) {
debugPrint('${slot.position.name}: ${slot.text}');
}
}The result is null when no header or footer is set. To make changes, pass a new CPDFHeaderFooter to updateHeaderFooter() and save the document after a successful update.
final cleared = await document.clearHeaderFooter();
if (cleared) {
await document.save();
}Setting, updating, and clearing affect the document in memory. Call save() to persist changes to the original file, or saveAs() to write a copy. See Save Document.