如何用ColdFusion与iText的onEndPage自动为PDF添加页眉页脚
Got it, since you already understand the Java side of iText page events, translating this to ColdFusion is straightforward—we'll just use ColdFusion's ability to extend Java classes and register the event handler with your PdfWriter instance. Here's a step-by-step breakdown with working code:
1. Create a ColdFusion Page Event Component
First, we'll make a ColdFusion component that extends iText's PdfPageEventHelper and overrides the onEndPage method (this is where we'll inject our header/footer logic).
component extends="com.itextpdf.text.pdf.PdfPageEventHelper" { // Override the onEndPage method to add header/footer when a page finishes rendering public void function onEndPage( com.itextpdf.text.pdf.PdfWriter writer, com.itextpdf.text.Document document ) { // Get current page number var pageNum = writer.getPageNumber(); // Initialize a standard font (adjust style/size as needed) var baseFont = createObject("java", "com.itextpdf.text.FontFactory").getFont( createObject("java", "com.itextpdf.text.FontFactory").HELVETICA, 10, createObject("java", "com.itextpdf.text.Font").NORMAL ); // --- Add Footer (Centered Page Number) --- var footerText = "Page #pageNum#"; writer.getDirectContent().showTextAligned( createObject("java", "com.itextpdf.text.Element").ALIGN_CENTER, footerText, document.getPageSize().getWidth() / 2, // X: horizontal center of page document.bottom() - 15, // Y: 15px below page bottom margin 0 // No rotation ); // --- Add Header (Left-Aligned Custom Text) --- var headerText = "My Official Document"; writer.getDirectContent().showTextAligned( createObject("java", "com.itextpdf.text.Element").ALIGN_LEFT, headerText, document.left(), // X: match left page margin document.top() + 15, // Y: 15px above page top margin 0 ); } }
2. Register the Event Handler in Your PDF Generation Code
Now, in your main ColdFusion code where you generate the PDF, create an instance of your custom page event component and attach it to the PdfWriter using setPageEvent().
// Initialize core iText objects var document = createObject("java", "com.itextpdf.text.Document").init(); var outputPath = expandPath("./generatedDocument.pdf"); var outputStream = createObject("java", "java.io.FileOutputStream").init(outputPath); var writer = createObject("java", "com.itextpdf.text.pdf.PdfWriter").getInstance(document, outputStream); // Create and register our custom page event handler var pageEventHandler = createObject("component", "MyHeaderFooterPageEvent"); writer.setPageEvent(pageEventHandler); // Open the document to start writing content document.open(); // Add content that triggers automatic pagination (example: repeated text) var sampleContent = repeatString("This is test content that will force the document to paginate. ", 120); document.add(createObject("java", "com.itextpdf.text.Paragraph").init(sampleContent)); // Clean up resources document.close(); outputStream.close();
Key Tips for Tweaks
- Coordinate System: PDF uses a bottom-left origin, so adjust X/Y values using
document.left(),document.right(),document.top(), anddocument.bottom()to align with your page margins. - Font Customization: Use
BaseFont.createFont()instead ofFontFactoryif you need to load custom TrueType fonts. - Dynamic Content: You can pass variables (like a document title) to your event component via its constructor or setter methods if you need personalized headers/footers.
- Event Timing:
onEndPagefires automatically whenever iText finishes a page—whether it's an automatic page break from overflow or a manualdocument.newPage()call.
This approach mirrors the Java implementation you already know, adapted to ColdFusion's component model and syntax. Test with your own content to fine-tune positioning and styling!
内容的提问来源于stack exchange,提问作者Jordan Lundgren

