丰富的文字

此示例显示如何更改文本的外观以绘制富文本。

PdfDocument 中,文本可以填充任何颜色。 它也可以同时抚摸或填充和抚摸。

drawText的options参数具有 fillstroke 属性。 将这些属性保留为空以指示应填充文本(默认)。 将 stroke 设置为 true 以指示应该描边文本。 设置 fillstroke,两者都为true表示文本应该被描边和填充。

每次调用drawText都会启动一个新段落。 要指示后续文本应该放在上一个文本之后,请将options参数的 continue 属性设置为true:

doc.drawText("Lorem ", null, null, { continued: true });
doc.drawText("ipsum.");

设置填充颜色

填充颜色可以通过两种方式指定:

  • 将画笔直接传递给 drawText 作为 options.brush 属性:

    doc.drawText("Lorem", null, null, {
        brush: new wijmo.pdf.PdfSolidBrush("#ff000") // or _brush: "#ff000"_
    });
    
  • 首先使用 doc.setBrush 方法更改默认文档画笔。 如果您在不传递画笔的情况下调用 drawText 方法,将使用此画笔:

    doc.setBrush(new wijmo.pdf.PdfSolidBrush("#ff000")); // or _doc.setBrush("#ff000");_
    doc.drawText("Lorem");
    

设置笔触颜色

与填充颜色一样,通过将笔传递到 options.pen 属性或 doc.setPen 方法,可以以相同的方式指定笔触颜色。

有关画笔和笔的更多信息,请参阅“绘图图形”主题。

PdfDocument不区分文本颜色和矢量图形颜色,因此更改默认文档画笔或笔也会影响矢量图形API。

import 'bootstrap.css'; import './styles.css'; import * as wijmo from '@grapecity/wijmo'; 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: { height: 0 // no header }, footer: { height: 0 // no footer }, ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); let c1 = new wijmo.Color('#3173c0'); let c2 = new wijmo.Color('#e69500'); // // Use a bigger font to show fills and strokes clearly. doc.setFont(new pdf.PdfFont('times', 20)); // doc.drawText('Lorem ', null, null, { continued: true }); // doc.drawText('ipsum ', null, null, { continued: true, stroke: true }); // doc.drawText('dolor ', null, null, { continued: true, brush: new pdf.PdfSolidBrush(c1), fill: true, // Override the stroke property. The text settings are retained between drawText calls if the continued property is used. stroke: false }); // doc.drawText('sit ', null, null, { continued: true, pen: new pdf.PdfPen(c2), fill: false, stroke: true }); // doc.drawText('amet.', null, null, { // A shorthand equivalent of new wijmo.pdf.PdfSolidBrush(c1) brush: c1, // A shorthand equivalent of new wijmo.pdf.PdfPen(c2) pen: c2, fill: true, stroke: true }); // doc.end(); }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Rich text</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 wijmo from '@grapecity/wijmo'; 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: { height: 0 // no header }, footer: { height: 0 // no footer }, ended: (sender: pdf.PdfDocument, args: pdf.PdfDocumentEndedEventArgs) => pdf.saveBlob(args.blob, 'Document.pdf') }); let c1 = new wijmo.Color('#3173c0'); let c2 = new wijmo.Color('#e69500'); // // Use a bigger font to show fills and strokes clearly. doc.setFont(new pdf.PdfFont('times', 20)); // doc.drawText('Lorem ', null, null, { continued: true }); // doc.drawText('ipsum ', null, null, { continued: true, stroke: true }); // doc.drawText('dolor ', null, null, { continued: true, brush: new pdf.PdfSolidBrush(c1), fill: true, // Override the stroke property. The text settings are retained between drawText calls if the continued property is used. stroke: false }); // doc.drawText('sit ', null, null, { continued: true, pen: new pdf.PdfPen(c2), fill: false, stroke: true }); // doc.drawText('amet.', null, null, { // A shorthand equivalent of new wijmo.pdf.PdfSolidBrush(c1) brush: c1, // A shorthand equivalent of new wijmo.pdf.PdfPen(c2) pen: c2, fill: true, stroke: true }); // 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>Rich text</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 wijmo from '@grapecity/wijmo'; import * as pdf from '@grapecity/wijmo.pdf'; // let App = Vue.extend({ name: 'app', methods: { exportPDF() { let doc = new pdf.PdfDocument({ header: { height: 0 // no header }, footer: { height: 0 // no footer }, ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); let c1 = new wijmo.Color('#3173c0'); let c2 = new wijmo.Color('#e69500'); // // Use a bigger font to show fills and strokes clearly. doc.setFont(new pdf.PdfFont('times', 20)); // doc.drawText('Lorem ', null, null, { continued: true }); // doc.drawText('ipsum ', null, null, { continued: true, stroke: true }); // doc.drawText('dolor ', null, null, { continued: true, brush: new pdf.PdfSolidBrush(c1), fill: true, // Override the stroke property. The text settings are retained between drawText calls if the continued property is used. stroke: false }); // doc.drawText('sit ', null, null, { continued: true, pen: new pdf.PdfPen(c2), fill: false, stroke: true }); // doc.drawText('amet.', null, null, { // A shorthand equivalent of new wijmo.pdf.PdfSolidBrush(c1) brush: c1, // A shorthand equivalent of new wijmo.pdf.PdfPen(c2) pen: c2, fill: true, stroke: true }); // 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>Rich text</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"; import * as wijmo from '@grapecity/wijmo'; 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: { height: 0 // no header }, footer: { height: 0 // no footer }, ended: (sender, args) => pdf.saveBlob(args.blob, 'Document.pdf') }); let c1 = new wijmo.Color('#3173c0'); let c2 = new wijmo.Color('#e69500'); // // Use a bigger font to show fills and strokes clearly. doc.setFont(new pdf.PdfFont('times', 20)); // doc.drawText('Lorem ', null, null, { continued: true }); // doc.drawText('ipsum ', null, null, { continued: true, stroke: true }); // doc.drawText('dolor ', null, null, { continued: true, brush: new pdf.PdfSolidBrush(c1), fill: true, // Override the stroke property. The text settings are retained between drawText calls if the continued property is used. stroke: false }); // doc.drawText('sit ', null, null, { continued: true, pen: new pdf.PdfPen(c2), fill: false, stroke: true }); // doc.drawText('amet.', null, null, { // A shorthand equivalent of new wijmo.pdf.PdfSolidBrush(c1) brush: c1, // A shorthand equivalent of new wijmo.pdf.PdfPen(c2) pen: c2, fill: true, stroke: true }); // 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; }