字体

此示例演示如何使用 PdfFont 类使用不同的字体绘制文本。

标准字体

开箱即用 PdfDocument 支持以下字体:Courier,Helvetica,Times,Symbol,ZapfDingbats。前三种字体各有四种字体,“正常”,“粗体”,“倾斜”和“粗斜”。这为我们提供了一组称为“14标准PDF字体”的字体。

设置字体

PdfFont 类描述了一个特定的字体,它的构造函数接受以下参数:

  • 字体系列:字体系列名称或由逗号分隔的字体系列名称的优先级列表,包括常规字体系列名称,如“monospace”,“serif”等.PdfDocument将使用最接近的注册字体。例如,“时代”,“Roboto,serif”。标准字体由以下值表示:“courier”,“helvetica”,“times”,“symbol”,“zapfdingbats”。
  • 字体大小
  • 字体样式:以下值之一:“normal”,“italic”,“oblique”。
  • 字体重量:以下值之一:“正常”,“粗体”,“100”,“200”,“300”,“400”,“500”,“600”,“700”, “800”,“900”。

以下实例是PdfDocument方面的默认文档字体:

new wijmo.pdf.PdfFont("times", 10, "normal", "normal");

使用字体

PdfDocument 中,可以通过两种方式指定字体:

  • 通过将PdfFont实例直接传递给** drawText 作为 options.font **属性。

    doc.drawText("Lorem", null, null, {
        font: new wijmo.pdf.PdfFont("times", 10, "normal", "bold")
    });
    
  • 首先使用 ** doc.setFont** 方法更改默认文档字体。 如果你调用 drawText 方法而不传递字体,它将被使用。 当需要绘制大量具有相同字体的文本时,此方法很有用。

    doc.setFont(new wijmo.pdf.PdfFont("times", 10, "normal", "bold"));
    doc.drawText("Lorem");
    

如果找不到具有确切给定样式和权重属性的字体,则将使用最接近的注册字体。

此示例演示如何使用 drawTextdoc.setFont 方法使用不同的字体绘制文本。

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({ ended: (sender, args) => pdf.saveBlob(args.blob, 'FlexGrid.pdf') }); // doc.drawText('This text uses the default document font, Times 10.'); // doc.drawText('This text uses Times Bold Oblique 20.', null, null, { font: new pdf.PdfFont('times', 20, 'oblique', 'bold') }); // doc.setFont(new pdf.PdfFont('helvetica')); doc.drawText('This text uses Helvetica 10.'); doc.drawText('This text also uses Helvetica 10.'); // doc.end(); }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Fonts</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({ ended: (sender: pdf.PdfDocument, args: pdf.PdfDocumentEndedEventArgs) => pdf.saveBlob(args.blob, 'FlexGrid.pdf') }); // doc.drawText('This text uses the default document font, Times 10.'); // doc.drawText('This text uses Times Bold Oblique 20.', null, null, { font: new pdf.PdfFont('times', 20, 'oblique', 'bold') }); // doc.setFont(new pdf.PdfFont('helvetica')); doc.drawText('This text uses Helvetica 10.'); doc.drawText('This text also uses Helvetica 10.'); // 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>Fonts</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({ ended: (sender, args) => pdf.saveBlob(args.blob, 'FlexGrid.pdf') }); // doc.drawText('This text uses the default document font, Times 10.'); // doc.drawText('This text uses Times Bold Oblique 20.', null, null, { font: new pdf.PdfFont('times', 20, 'oblique', 'bold') }); // doc.setFont(new pdf.PdfFont('helvetica')); doc.drawText('This text uses Helvetica 10.'); doc.drawText('This text also uses Helvetica 10.'); // 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>Fonts</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({ ended: (sender, args) => pdf.saveBlob(args.blob, 'FlexGrid.pdf') }); // doc.drawText('This text uses the default document font, Times 10.'); // doc.drawText('This text uses Times Bold Oblique 20.', null, null, { font: new pdf.PdfFont('times', 20, 'oblique', 'bold') }); // doc.setFont(new pdf.PdfFont('helvetica')); doc.drawText('This text uses Helvetica 10.'); doc.drawText('This text also uses Helvetica 10.'); // 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; }