以声明方式自定义页眉和页脚

此示例说明如何以声明方式自定义页眉和页脚。

文档呈现中一个非常常见的任务是通过添加静态文本(如文档标题)或页码来自定义页眉和页脚。为了帮助完成这项工作,PdfDocument提供了一种以声明方式配置这些部分的方法。

PdfRunningTitle 是一个表示页面页眉和页脚的类。它提供了声明属性,反过来又具有以下属性:

  • text:确定正在运行的标题的文本。
  • brush:确定用于填充文本的画笔。
  • font:确定文本的字体。

可以在创建 PdfDocument 类实例时定义这些属性。完成文档渲染后,将迭代页面,并使用给定的画笔和字体将指定的文本添加到每个页面。

例如,以下代码定义用红色填充的“Title”字符串将添加到每个页面的标题中:

var doc = new wijmo.pdf.PdfDocument({
    header: {
        declarative: {
            text: "Title",
            brush: "#ff0000"
        }
    }
});

文本最多可包含3个表格字符(“\ t”),用于将文本分隔为将使用左,中和右对齐在页面区域内对齐的部分。 支持两种宏,"&\ [Page ]”和“&\ [Pages ]"。 前者指定当前页面索引,而后者指定页面计数。

例如:

  • "&\ [Page ]":添加左侧对齐的当前页面索引。
  • &\ [[Pages ]的"\ t&\ [Page ]":添加格式为“X of Y”的页码编号(其中“X”表示当前页面索引,“Y”表示页面 计数)并将其与中心对齐。
  • "\ tTitle \ t&\ [Page ]":添加“标题”字符串居中,并将当前页面索引添加到右侧。

有关使用字体和画笔的详细信息,请参阅“字体”和“绘图图形”主题。

import 'bootstrap.css'; import './styles.css'; import * as pdf from '@grapecity/wijmo.pdf'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { document.querySelector('#btnExport').addEventListener('click', () => { let doc = new pdf.PdfDocument({ header: { declarative: { text: '\tTitle\t&[Page]', font: new pdf.PdfFont('helvetica', 10, 'normal', 'bold') } }, footer: { declarative: { text: '\t&[Page]\\&[Pages]', brush: '#3173c0', font: new pdf.PdfFont('helvetica', 10, 'normal', 'bold') } }, ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); // doc.drawText('Lorem ipsum'); // doc.addPage(); doc.drawText('Lorem ipsum'); // doc.end(); }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Customizing headers and footers declaratively</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" id="btnExport">Export</button> </div> </body> </html> body { margin-bottom: 24px; } import 'bootstrap.css'; import './styles.css'; // import * as pdf from '@grapecity/wijmo.pdf'; // import { Component, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { export() { let doc = new pdf.PdfDocument({ header: { declarative: { text: '\tTitle\t&[Page]', font: new pdf.PdfFont('helvetica', 10, 'normal', 'bold') } }, footer: { declarative: { text: '\t&[Page]\\&[Pages]', brush: '#3173c0', font: new pdf.PdfFont('helvetica', 10, 'normal', 'bold') } }, ended: (sender: pdf.PdfDocument, args: pdf.PdfDocumentEndedEventArgs) => pdf.saveBlob(args.blob, 'Document.pdf') }); // doc.drawText('Lorem ipsum'); // doc.addPage(); doc.drawText('Lorem ipsum'); // doc.end(); } } // @NgModule({ imports: [BrowserModule], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } // enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Customizing headers and footers declaratively</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" (click)="export()">Export</button> </div> body { margin-bottom: 24px; } <template> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" @click="exportPDF">Export</button> </div> </template> <script> import 'bootstrap.css'; import Vue from 'vue'; import * as pdf from '@grapecity/wijmo.pdf'; // let App = Vue.extend({ name: 'app', methods: { exportPDF() { let doc = new pdf.PdfDocument({ header: { declarative: { text: '\tTitle\t&[Page]', font: new pdf.PdfFont('helvetica', 10, 'normal', 'bold') } }, footer: { declarative: { text: '\t&[Page]\\&[Pages]', brush: '#3173c0', font: new pdf.PdfFont('helvetica', 10, 'normal', 'bold') } }, ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); // doc.drawText('Lorem ipsum'); // doc.addPage(); doc.drawText('Lorem ipsum'); // doc.end(); } } }) // new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> body { margin-bottom: 24px; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Customizing headers and footers declaratively</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"></div> </body> </html> import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import "./app.css"; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; // import * as pdf from "@grapecity/wijmo.pdf"; class App extends React.Component { constructor(props) { super(props); } render() { return <div className="container-fluid"> <button className="btn btn-default" onClick={this.exportPDF.bind(this)}>Export</button> </div>; } exportPDF() { let doc = new pdf.PdfDocument({ header: { declarative: { text: '\tTitle\t&[Page]', font: new pdf.PdfFont('helvetica', 10, 'normal', 'bold') } }, footer: { declarative: { text: '\t&[Page]\\&[Pages]', brush: '#3173c0', font: new pdf.PdfFont('helvetica', 10, 'normal', 'bold') } }, ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); // doc.drawText('Lorem ipsum'); // doc.addPage(); doc.drawText('Lorem ipsum'); // doc.end(); } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Grapecity Wijmo OLAP Pivot Chart Overview</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> body { margin-bottom: 24px; }