1e41f4b71Sopenharmony_ci# @ohos.app.ability.PrintExtensionAbility (打印扩展能力)(系统接口)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci该模块为打印扩展能力的操作API,提供调用打印扩展能力的接口。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**  
6e41f4b71Sopenharmony_ci> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。  
7e41f4b71Sopenharmony_ci> 本模块接口为系统接口。  
8e41f4b71Sopenharmony_ci> 本模块接口仅可在Stage模型下使用。
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## 导入模块
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci```ts
13e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
14e41f4b71Sopenharmony_ci```
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## onCreate
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_cionCreate(want: Want): void
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci初始化扩展能力,会在系统首次连接打印扩展时调用。
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Print.PrintFramework
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**参数:**
25e41f4b71Sopenharmony_ci| **参数名** | **类型** | **必填** | **说明** |
26e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
27e41f4b71Sopenharmony_ci| want | Want | 是 | 表示调用打印页面需要参数 |
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**错误码:**
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                    |
34e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
35e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API. |
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**示例:**
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci```ts
40e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
41e41f4b71Sopenharmony_ciimport Want from '@ohos.app.ability.Want';
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ciexport default class HWPrintExtension extends PrintExtensionAbility {
44e41f4b71Sopenharmony_ci    onCreate(want: Want): void {
45e41f4b71Sopenharmony_ci        console.log('onCreate');
46e41f4b71Sopenharmony_ci        // ...
47e41f4b71Sopenharmony_ci    }
48e41f4b71Sopenharmony_ci}
49e41f4b71Sopenharmony_ci```
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci## onStartDiscoverPrinter
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_cionStartDiscoverPrinter(): void
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci开始发现与设备连接的打印机时调用。
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Print.PrintFramework
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci**错误码:**
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                    |
64e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
65e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API. |
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci**示例:**
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci```ts
70e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ciexport default class HWPrintExtension extends PrintExtensionAbility {
73e41f4b71Sopenharmony_ci    onStartDiscoverPrinter(): void {
74e41f4b71Sopenharmony_ci        console.log('onStartDiscoverPrinter enter');
75e41f4b71Sopenharmony_ci        // ...
76e41f4b71Sopenharmony_ci    }
77e41f4b71Sopenharmony_ci}
78e41f4b71Sopenharmony_ci```
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci## onStopDiscoverPrinter
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_cionStopDiscoverPrinter(): void
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci停止发现打印机时调用。
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Print.PrintFramework
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**错误码:**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                    |
93e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
94e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API. |
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci**示例:**
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci```ts
99e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ciexport default class HWPrintExtension extends PrintExtensionAbility {
102e41f4b71Sopenharmony_ci    onStopDiscoverPrinter(): void {
103e41f4b71Sopenharmony_ci        console.log('onStopDiscoverPrinter enter');
104e41f4b71Sopenharmony_ci        // ...
105e41f4b71Sopenharmony_ci    }
106e41f4b71Sopenharmony_ci}
107e41f4b71Sopenharmony_ci```
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci## onConnectPrinter
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_cionConnectPrinter(printerId: number): void
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci连接到特定打印机时调用。
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Print.PrintFramework
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci**参数:**
118e41f4b71Sopenharmony_ci| **参数名** | **类型** | **必填** | **说明** |
119e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
120e41f4b71Sopenharmony_ci| printerId | number | 是 | 表示打印机ID |
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci**错误码:**
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                    |
127e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
128e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API. |
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci**示例:**
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci```ts
133e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ciexport default class HWPrintExtension extends PrintExtensionAbility {
136e41f4b71Sopenharmony_ci    onConnectPrinter(printerId: number): void {
137e41f4b71Sopenharmony_ci        console.log('onConnectPrinter enter');
138e41f4b71Sopenharmony_ci        // ...
139e41f4b71Sopenharmony_ci    }
140e41f4b71Sopenharmony_ci}
141e41f4b71Sopenharmony_ci```
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci## onDisconnectPrinter
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_cionDisconnectPrinter(printerId: number): void
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci断开与特定打印机的连接时调用。
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Print.PrintFramework
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci**参数:**
152e41f4b71Sopenharmony_ci| **参数名** | **类型** | **必填** | **说明** |
153e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
154e41f4b71Sopenharmony_ci| printerId | number | 是 | 表示打印机ID |
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci**错误码:**
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                    |
161e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
162e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API. |
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci**示例:**
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci```ts
167e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ciexport default class HWPrintExtension extends PrintExtensionAbility {
170e41f4b71Sopenharmony_ci    onDisconnectPrinter(printerId: number): void {
171e41f4b71Sopenharmony_ci        console.log('onDisconnectPrinter enter');
172e41f4b71Sopenharmony_ci        // ...
173e41f4b71Sopenharmony_ci    }
174e41f4b71Sopenharmony_ci}
175e41f4b71Sopenharmony_ci```
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci## onStartPrintJob
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_cionStartPrintJob(jobInfo: print.PrintJob): void
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci开始打印任务时调用。
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Print.PrintFramework
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci**参数:**
186e41f4b71Sopenharmony_ci| **参数名** | **类型** | **必填** | **说明** |
187e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
188e41f4b71Sopenharmony_ci| jobInfo | print.PrintJob | 是 | 表示打印任务的信息 |
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci**错误码:**
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                    |
195e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
196e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API. |
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci**示例:**
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci```ts
201e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
202e41f4b71Sopenharmony_ciimport print from '@ohos.print';
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ciexport default class HWPrintExtension extends PrintExtensionAbility {
205e41f4b71Sopenharmony_ci    onStartPrintJob(jobInfo: print.PrintJob): void {
206e41f4b71Sopenharmony_ci        console.log('onStartPrintJob, jobId is: ' + jobInfo.jobId);
207e41f4b71Sopenharmony_ci        // ...
208e41f4b71Sopenharmony_ci    }
209e41f4b71Sopenharmony_ci}
210e41f4b71Sopenharmony_ci```
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci## onCancelPrintJob
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_cionCancelPrintJob(jobInfo: print.PrintJob): void
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci移除已开始的打印任务时调用。
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Print.PrintFramework
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci**参数:**
221e41f4b71Sopenharmony_ci| **参数名** | **类型** | **必填** | **说明** |
222e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
223e41f4b71Sopenharmony_ci| jobInfo | print.PrintJob | 是 | 表示打印任务的信息 |
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci**错误码:**
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                    |
230e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
231e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API. |
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci**示例:**
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ci```ts
236e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
237e41f4b71Sopenharmony_ciimport print from '@ohos.print';
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ciexport default class HWPrintExtension extends PrintExtensionAbility {
240e41f4b71Sopenharmony_ci    onCancelPrintJob(jobInfo: print.PrintJob): void {
241e41f4b71Sopenharmony_ci        console.log('onCancelPrintJob, jobId is: ' + jobInfo.jobId);
242e41f4b71Sopenharmony_ci        // ...
243e41f4b71Sopenharmony_ci    }
244e41f4b71Sopenharmony_ci}
245e41f4b71Sopenharmony_ci```
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ci## onRequestPrinterCapability
248e41f4b71Sopenharmony_ci
249e41f4b71Sopenharmony_cionRequestPrinterCapability(printerId: number): print.PrinterCapability
250e41f4b71Sopenharmony_ci
251e41f4b71Sopenharmony_ci请求打印机能力时调用。
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Print.PrintFramework
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ci**参数:**
256e41f4b71Sopenharmony_ci| **参数名** | **类型** | **必填** | **说明** |
257e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
258e41f4b71Sopenharmony_ci| printerId | number | 是 | 表示打印机ID |
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci**返回值:**
261e41f4b71Sopenharmony_ci| **类型** | **说明** |
262e41f4b71Sopenharmony_ci| -------- | -------- |
263e41f4b71Sopenharmony_ci| print.PrinterCapability | 表示打印能力 |
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci**错误码:**
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
268e41f4b71Sopenharmony_ci
269e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                    |
270e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
271e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API. |
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci**示例:**
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ci```ts
276e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
277e41f4b71Sopenharmony_ciimport print from '@ohos.print';
278e41f4b71Sopenharmony_ci
279e41f4b71Sopenharmony_ciexport default class HWPrintExtension extends PrintExtensionAbility {
280e41f4b71Sopenharmony_ci    onRequestPrinterCapability(printerId: number): print.PrinterCapability {
281e41f4b71Sopenharmony_ci        console.log('onRequestPrinterCapability enter');
282e41f4b71Sopenharmony_ci        // ...
283e41f4b71Sopenharmony_ci        let tmp : print.PrinterCapability = {
284e41f4b71Sopenharmony_ci            colorMode : 1,
285e41f4b71Sopenharmony_ci            duplexMode : 1,
286e41f4b71Sopenharmony_ci            pageSize : []
287e41f4b71Sopenharmony_ci        };
288e41f4b71Sopenharmony_ci        return tmp;
289e41f4b71Sopenharmony_ci    }
290e41f4b71Sopenharmony_ci}
291e41f4b71Sopenharmony_ci```
292e41f4b71Sopenharmony_ci
293e41f4b71Sopenharmony_ci## onRequestPreview
294e41f4b71Sopenharmony_ci
295e41f4b71Sopenharmony_cionRequestPreview(jobInfo: print.PrintJob): string
296e41f4b71Sopenharmony_ci
297e41f4b71Sopenharmony_ci请求预览时调用,并将结果返回到Print SA。
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Print.PrintFramework
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_ci**参数:**
302e41f4b71Sopenharmony_ci| **参数名** | **类型** | **必填** | **说明** |
303e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
304e41f4b71Sopenharmony_ci| jobInfo | print.PrintJob | 是 | 表示打印任务信息 |
305e41f4b71Sopenharmony_ci
306e41f4b71Sopenharmony_ci**返回值:**
307e41f4b71Sopenharmony_ci| **类型** | **说明** |
308e41f4b71Sopenharmony_ci| -------- | -------- |
309e41f4b71Sopenharmony_ci| string | 返回的预览结果 |
310e41f4b71Sopenharmony_ci
311e41f4b71Sopenharmony_ci**错误码:**
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                    |
316e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
317e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API. |
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci**示例:**
320e41f4b71Sopenharmony_ci
321e41f4b71Sopenharmony_ci```ts
322e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
323e41f4b71Sopenharmony_ciimport print from '@ohos.print';
324e41f4b71Sopenharmony_ci
325e41f4b71Sopenharmony_ciexport default class HWPrintExtension extends PrintExtensionAbility {
326e41f4b71Sopenharmony_ci    onRequestPreview(jobInfo: print.PrintJob): string {
327e41f4b71Sopenharmony_ci        console.log('onRequestPreview enter');
328e41f4b71Sopenharmony_ci        // ...
329e41f4b71Sopenharmony_ci        let tmp : string = '';
330e41f4b71Sopenharmony_ci        return tmp;
331e41f4b71Sopenharmony_ci    }
332e41f4b71Sopenharmony_ci}
333e41f4b71Sopenharmony_ci```
334e41f4b71Sopenharmony_ci
335e41f4b71Sopenharmony_ci## onDestroy
336e41f4b71Sopenharmony_ci
337e41f4b71Sopenharmony_cionDestroy(): void
338e41f4b71Sopenharmony_ci
339e41f4b71Sopenharmony_ci结束打印扩展时调用。
340e41f4b71Sopenharmony_ci
341e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Print.PrintFramework
342e41f4b71Sopenharmony_ci
343e41f4b71Sopenharmony_ci**错误码:**
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[打印服务错误码](./errorcode-print.md)。
346e41f4b71Sopenharmony_ci
347e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                    |
348e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- |
349e41f4b71Sopenharmony_ci| 202 | Permission verification failed. A non-system application calls a system API. |
350e41f4b71Sopenharmony_ci
351e41f4b71Sopenharmony_ci**示例:**
352e41f4b71Sopenharmony_ci
353e41f4b71Sopenharmony_ci```ts
354e41f4b71Sopenharmony_ciimport PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
355e41f4b71Sopenharmony_ci
356e41f4b71Sopenharmony_ciexport default class HWPrintExtension extends PrintExtensionAbility {
357e41f4b71Sopenharmony_ci    onDestroy(): void {
358e41f4b71Sopenharmony_ci        console.log('onDestroy');
359e41f4b71Sopenharmony_ci    }
360e41f4b71Sopenharmony_ci}
361e41f4b71Sopenharmony_ci```