页面结构

此示例显示 PdfDocument 的页面结构。

页面部分

PdfDocument 中,每个页面分为三个部分:页眉,正文和页脚。 这些部分中的每一部分表示绘图区域,由 PdfPageArea 类表示,具有用于绘制文本和图形的自己的方法。

绘图区域不是相互隔离的,因此在一个区域中绘制的图形可能会影响另一个区域。

每个绘图区域都有自己的坐标系,其中(0,0)坐标指向左上角。

PdfDocument类实例表示body部分本身,而其 headerfooter 属性分别代表页眉和页脚部分:

var doc = new wijmo.pdf.PdfDocument();

doc.header.drawText("header");
doc.drawText("body");
doc.footer.drawText("footer");

要隐藏页脚或标题,请将区域的** height **属性设置为0:

var doc = new wijmo.pdf.PdfDocument({
    header: { height: 0 },
    footer: { height: 0 }
});

页面设置

在初始化期间,您可以通过将页面设置传递到 pageSettings 对象中的PdfDocument构造函数来定义页面设置,例如 layoutsizemargin

  • 布局确定页面,纵向或横向的方向。
  • 尺寸决定了页面的尺寸。 支持预定义和自定义尺寸。
  • 边距决定了页面的边距。

例如:

var doc = new wijmo.pdf.PdfDocument({
    pageSettings: {
        layout: wijmo.pdf.PdfPageOrientation.Portrait,
        margins: {
            left: 72,
            top: 72,
            right: 72,
            bottom: 72
        }
    }
});

对于现有页面,在运行时无法更改这些设置。

使用 doc.pageSettings 属性为将自动添加的页面或通过调用 doc.addPage 方法提供设置。 使用 doc.currentPageSettings 属性获取当前页面设置。

在此示例中,显式声明了布局,大小和边距属性。 为了澄清目的,每个页面部分都被描述。

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({ pageSettings: { layout: pdf.PdfPageOrientation.Portrait, size: pdf.PdfPageSize.Letter, margins: { left: 72, top: 72, right: 72, bottom: 72 } }, ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); // // stroke areas doc.header.paths .rect(0, 0, doc.header.width, doc.header.height) .stroke(); // doc.paths .rect(0, 0, doc.width, doc.height) .stroke(); // doc.footer.paths .rect(0, 0, doc.footer.width, doc.footer.height) .stroke(); // // write descriptions doc.header.drawText('Header'); doc.drawText('Body'); doc.footer.drawText('Footer'); // doc.end(); }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Page structure</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 '@grapecity/wijmo.styles/wijmo.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({ pageSettings: { layout: pdf.PdfPageOrientation.Portrait, size: pdf.PdfPageSize.Letter, margins: { left: 72, top: 72, right: 72, bottom: 72 } }, ended: (sender: pdf.PdfDocument, args: pdf.PdfDocumentEndedEventArgs) => pdf.saveBlob(args.blob, 'Document.pdf') }); // // stroke areas doc.header.paths .rect(0, 0, doc.header.width, doc.header.height) .stroke(); // doc.paths .rect(0, 0, doc.width, doc.height) .stroke(); // doc.footer.paths .rect(0, 0, doc.footer.width, doc.footer.height) .stroke(); // // write descriptions doc.header.drawText('Header'); doc.drawText('Body'); doc.footer.drawText('Footer') // 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>Page structure</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({ pageSettings: { layout: pdf.PdfPageOrientation.Portrait, size: pdf.PdfPageSize.Letter, margins: { left: 72, top: 72, right: 72, bottom: 72 } }, ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); // // stroke areas doc.header.paths .rect(0, 0, doc.header.width, doc.header.height) .stroke(); // doc.paths .rect(0, 0, doc.width, doc.height) .stroke(); // doc.footer.paths .rect(0, 0, doc.footer.width, doc.footer.height) .stroke(); // // write descriptions doc.header.drawText('Header'); doc.drawText('Body'); doc.footer.drawText('Footer') // 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>Page structure</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({ pageSettings: { layout: pdf.PdfPageOrientation.Portrait, size: pdf.PdfPageSize.Letter, margins: { left: 72, top: 72, right: 72, bottom: 72 } }, ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); // // stroke areas doc.header.paths .rect(0, 0, doc.header.width, doc.header.height) .stroke(); // doc.paths .rect(0, 0, doc.width, doc.height) .stroke(); // doc.footer.paths .rect(0, 0, doc.footer.width, doc.footer.height) .stroke(); // // write descriptions doc.header.drawText('Header'); doc.drawText('Body'); doc.footer.drawText('Footer'); // 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; }