wps文字怎么打印,Delphi打印Wps文字
在软件开发中,打印文档是任何一个软件都有可能的硬需求,对于一些特殊的打印,可以采用特殊的方法进行处理,但大部分的打印需求不会很特殊,我们都可以借助办公软件来轻松地实现,今天,我们来探索一下借助 wps 文字进行打印的实现。
在 Wps 开放平台 https://open.wps 网上可以查到所有的客户端开发 API,我们重点来了解一下关于 wps 文字打印部分相关的 API。
4.1 Application
代表 WPS 应用程序。Application 对象包含可返回顶级对象的属性和方法。例如,ActiveDocument 属性返回 Document 对象。
整个 wps 应用程序的 api 对象结构是一个树状结构,而 Application 对象是树状结构的根对象,同时,Application 对象也提供了对于应用程序相关的各种访问接口,例如:
- 应用程序的设置选项、环境、版本号等相关信息
- 一些常见的属性,如ActiveWindow、ActiveDocument、Name等
一般情况下,在 Delphi 或 Lazarus 中创建代码:
Try
WpsApp := CreateOleObject('Kwps.Application');
Except
ShowMessage('没有安装 wps');
Exit;
end;
相关的属性:
Application.Documents
返回一个 Documents 集合,该集合代表所有打开的文档。只读。
示例代码:
/* 新增一篇文档,并显示“另存为”对话框 */
Application.Documents.Add().Save()
Application.Visible
如果指定对象可见,则该属性值为 True。Boolean 类型,可读写。
示例代码:
/*本示例隐藏 WPS 。*/
Application.Visible = false
Application.ActivePrinter
返回或设置活动打印机名称。可读/写 String 类型。
示例代码:
/*本示例将网络 HP LaserJet IIISi 打印机设置为活动打印机。*/
Application.ActivePrinter = "HP LaserJet IIISi on \printerslaser"
相关的方法:
Application.Quit
退出 WPS ,并可选择保存或传送打开的文档。
语法
express.Quit(SaveChanges, OriginalFormat, RouteDocument)
express 一个代表 Application 对象的变量。
参数
名称 | 必选/可选 | 数据类型 | 说明 |
SaveChanges | 可选 | Variant | 指定 WPS 在关闭前是否保存更改过的文档。可以是 WdSaveOptions 常量之一。 |
OriginalFormat | 可选 | Variant | 指定 WPS 保存其原格式不是 WPS 文档格式的文档的方式。可以是 WdOriginalFormat 常量之一。 |
RouteDocument | 可选 | Variant | 如果为 True,则将文档传送给下一个收件人。如果文档没有附加传送名单,则忽略该参数。 |
WdSaveOptions 枚举
指定应该如何处理待定的更改。
名称 | 值 | 说明 |
wdDoNotSaveChanges | 0 | 不保存待定的更改。 |
wdPromptToSaveChanges | -2 | 提示用户保存待定更改。 |
wdSaveChanges | -1 | 自动保存待定更改,而不提示用户。 |
4.2 Documents
当前在 WPS 中打开的所有 Document 对象的集合。
相关的方法:
Documents.Open
打开指定的文档并将其添加到 Documents 集合。返回一个 Document 对象。
语法
express.Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenConflictDocument, OpenAndRepair, DocumentDirection, NoEncodingDialog)
express 一个代表 Documents 对象的变量。
常用参数
名称 | 必选/可选 | 数据类型 | 说明 |
FileName | 必选 | Variant | 文档名(可包含路径)。 |
ConfirmConversions | 可选 | Variant | 如果该属性为 True,则当文件不是 WPS 格式时,将显示“文件转换”对话框。 |
ReadOnly | 可选 | Variant | 如果该属性值为 True,则以只读方式打开文档。该参数不会覆盖保存的文档的只读建议设置。例如,如果文档在只读建议启用的情况下保存,则将 ReadOnly 参数设置为 False 不会导致文件以可读写方式打开。 |
示例代码:
/* 本示例以只读方式打开文档 MyDoc.doc*/
Application.Documents.Open("C:\MyDoc.doc", null, true)
4.3 Document
代表一个文档。Document 对象是 Documents 集合的成员。Documents 集合中包含当前在 WPS 中打开的所有 Document 对象。
使用 Documents(Index) 返回单个 Document 对象(其中 Index 是文档名称或索引编号)。
相关的属性:
Document.PageSetup
返回一个与指定文档相关联的 PageSetup 对象。
示例代码:
/* 本示例将活动文档的右边距设置为 72 磅(1 英寸) */
Application.ActiveDocument.PageSetup.RightMargin = Application.InchesToPoints(2)
相关的方法:
Document.PrintPreview
将视图切换到打印预览。
除了使用 PrintPreview 方法外,还可以将PrintPreview属性设置为 True 或 False,前者切换到打印预览,后者从打印预览切回。还可以通过将 View 对象的 Type 属性设置为 wdPrintPreview来更改视图。
示例代码:
/* 切换到打印预览模式 */
Application.ActiveDocument.PrintPreview()
Document.PrintOut
打印指定文档的全部或部分内容。
语法
express.PrintOut(Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)
示例代码:
/* 本示例打印活动文档的当前页面 */
Application.ActiveDocument.PrintOut(null, null, wdPrintCurrentPage)
/* 打印当前活动文档的第1-3页 */
Application.ActiveDocument.PrintOut(null,null,wdPrintFromTo,"","1","3")
4.4 PageSetup
代表页面设置说明。PageSetup 对象包含文档的所有页面设置属性(如左边距、下边距和纸张大小)。
相关的属性:
PageSetup.Orientation
返回或设置页面的方向。可读写 WdOrientation 类型。
WdOrientation 枚举
指定页面布局方向。
名称 | 值 | 说明 |
wdOrientLandscape | 1 | 横向。 |
wdOrientPortrait | 0 | 纵向。 |
示例代码:
/*以下示例先改变文档“MyDocument.doc”的方向,再打印该文档。然后本示例将文档的方向恢复为纵向。*/
function test()
{
let myDoc = Documents.Item("MyDocument.doc")
myDoc.PageSetup.Orientation = wdOrientLandscape
myDoc.PrintOut()
myDoc.PageSetup.Orientation = wdOrientPortrait
}
PageSetup.PageHeight
返回或设置页面高度(以磅为单位)。Single 类型,可读写。
如果设置了 PageHeight 属性,则 PaperSize 属性会改为 wdPaperCustom。使用 PaperSize 属性可设置预定义大小的纸张的页面高度和宽度,例如 Letter 或 A4 纸。
示例代码:
/*本示例将活动文档的页面高度设置为 9 英寸。*/
function test() {
Application.ActiveDocument.PageSetup.PageHeight = InchesToPoints(9)
Application.ActiveDocument.PageSetup.PageWidth = InchesToPoints(7)
}
PageSetup.PageWidth
返回或设置页面宽度(以磅为单位)。Single 类型,可读写。
如果设置了 PageWidth 属性,则 PaperSize 属性将改为 wdPaperCustom。使用 PaperSize 属性可设置预定义大小的纸张的页面高度和宽度,例如 Letter 或 A4 纸。
示例代码:
/*本示例返回 Document1 的页面宽度。可用 PointsToInches 方法将磅值转换为英寸。*/
function test() {
let doc1set = Application.Documents.Item("Document1.docx").PageSetup
alert("The page width is "+PointsToInches(doc1set.PageWidth)+" inches.")
}
PageSetup.PaperSize
返回或设置纸张大小。WdPaperSize 类型,可读写。
设置 PageHeight 或 PageWidth 属性会将 PaperSize 属性更改为 wdPaperCustom。
示例代码:
/*本示例将第一个文档的纸张大小设置为 legal 型。*/
Application.Documents.Item(1).PageSetup.PaperSize = wdPaperLegal
WdPaperSize 枚举
名称 | 值 | 说明 |
wdPaper10x14 | 0 | 10 英寸(25.4 厘米)宽,14 英寸(35.56 厘米)长。 |
wdPaper11x17 | 1 | 正规 11 英寸(27.94 厘米)宽,17 英寸(43.18 厘米)长。 |
wdPaperA3 | 6 | A3 尺寸。 |
wdPaperA4 | 7 | A4 尺寸。 |
wdPaperA4Small | 8 | 小 A4 尺寸。 |
wdPaperA5 | 9 | A5 尺寸。 |
wdPaperB4 | 10 | B4 尺寸。 |
wdPaperB5 | 11 | B5 尺寸。 |
wdPaperCSheet | 12 | C Sheet 尺寸。 |
wdPaperCustom | 41 | 自定义纸张大小。 |
wdPaperDSheet | 13 | D Sheet 尺寸。 |
wdPaperEnvelope10 | 25 | 正规信封,尺寸 10。 |
wdPaperEnvelope11 | 26 | 信封,尺寸 11。 |
wdPaperEnvelope12 | 27 | 信封,尺寸 12。 |
wdPaperEnvelope14 | 28 | 信封,尺寸 14。 |
wdPaperEnvelope9 | 24 | 信封,尺寸 9。 |
wdPaperEnvelopeB4 | 29 | B4 信封。 |
wdPaperEnvelopeB5 | 30 | B5 信封。 |
wdPaperEnvelopeB6 | 31 | B6 信封。 |
wdPaperEnvelopeC3 | 32 | C3 信封。 |
wdPaperEnvelopeC4 | 33 | C4 信封。 |
wdPaperEnvelopeC5 | 34 | C5 信封。 |
wdPaperEnvelopeC6 | 35 | C6 信封。 |
wdPaperEnvelopeC65 | 36 | C65 信封。 |
wdPaperEnvelopeDL | 37 | DL 信封。 |
wdPaperEnvelopeItaly | 38 | 意大利式信封。 |
wdPaperEnvelopeMonarch | 39 | 君主式信封。 |
wdPaperEnvelopePersonal | 40 | 私人信封。 |
wdPaperESheet | 14 | E Sheet 尺寸。 |
wdPaperExecutive | 5 | 行政型尺寸。 |
wdPaperFanfoldLegalGerman | 15 | 德国正规复写簿尺寸。 |
wdPaperFanfoldStdGerman | 16 | 德国标准复写簿尺寸。 |
wdPaperFanfoldUS | 17 | 美国复写簿尺寸。 |
wdPaperFolio | 18 | 对开尺寸。 |
wdPaperLedger | 19 | 分类帐尺寸。 |
wdPaperLegal | 4 | 正规尺寸。 |
wdPaperLetter | 2 | 信函尺寸。 |
wdPaperLetterSmall | 3 | 小型信函尺寸。 |
wdPaperNote | 20 | 便笺尺寸。 |
wdPaperQuarto | 21 | 四开尺寸。 |
wdPaperStatement | 22 | 报表尺寸。 |
wdPaperTabloid | 23 | 文摘尺寸。 |
PageSetup.TopMargin
返回或设置页面的上边缘与正文文本的上边界之间的距离(以磅为单位)。可读写 Single 类型。
示例代码:
/*以下示例将活动文档中第一节的上边距设置为 72 磅(1 英寸)。*/
Application.ActiveDocument.Sections.Item(1).PageSetup.TopMargin = 72
另外还有 BottomMargin、LeftMargin、RightMargin 属性用于设置边距。
4.5 在 Delphi 中操作 Wps 实现文字的打印
为了使程序的可用性增强,我们在开发中专门编写了 wps 打印单元,代码如下:
unit UnitWpsPrint;
interface
uses Vcl.Dialogs, Comobj;
type
TCustomPaper = Record
Width: Double;
Height: Double;
TopMargin, BottomMargin, LeftMargin, RightMargin: Double;
End;
procedure PrintDocxStandard(DocxFileName: String; PrinterName: String; PageSize: Integer; Layout:Integer; Copies: Integer);
procedure PrintDocxCustom(DocxFileName: String; PrinterName: String; CustomPaper: TCustomPaper; Layout:Integer; Copies: Integer);
implementation
procedure PrintDocxStandard(DocxFileName: String; PrinterName: String; PageSize: Integer; Layout:Integer; Copies: Integer);
var
WpsApp: variant; // Wps 应用
Doc: variant; // 文档
i: Integer;
begin
// Wps
Try
WpsApp := CreateOleObject('Kwps.Application');
Except
ShowMessage('没有安装 wps');
Exit;
end;
WpsApp.Visible := True;
Doc := WpsApp.Documents.Open(DocxFileName, False, True);
// 设置打印机
WpsApp.ActivePrinter:=PrinterName;
// 设置纸张
Doc.PageSetup.PaperSize:=PageSize; // A4-7, A5-9, B4-10, B5-11, A3-6
// 设置纸张方向
if (Layout >= 0) and (Layout <= 1) then
Doc.PageSetup.Orientation:=Layout; // 0-纵向 1-横向
// 打印预览
Doc.PrintPreview;
// 打印输出
// for i := 1 to Copies do Doc.PrintOut;
// WpsApp.Quit(0);
end;
procedure PrintDocxCustom(DocxFileName: String; PrinterName: String; CustomPaper: TCustomPaper; Layout:Integer; Copies: Integer);
const
R=28.35;
var
WpsApp: variant; // Wps 应用
Doc: variant; // 文档
i: Integer;
begin
// Wps
Try
WpsApp := CreateOleObject('Kwps.Application');
Except
ShowMessage('没有安装 wps');
Exit;
end;
WpsApp.Visible := True;
Doc := WpsApp.Documents.Open(DocxFileName, False, True);
// 设置打印机
WpsApp.ActivePrinter:=PrinterName;
// 设置纸张
Doc.PageSetup.PageWidth := CustomPaper.Width*R;
Doc.PageSetup.PageHeight := CustomPaper.Height*R;
Doc.PageSetup.TopMargin := CustomPaper.TopMargin*R;
Doc.PageSetup.BottomMargin := CustomPaper.BottomMargin*R;
Doc.PageSetup.LeftMargin := CustomPaper.LeftMargin*R;
Doc.PageSetup.RightMargin := CustomPaper.RightMargin*R;
// 设置纸张方向
if (Layout >= 0) and (Layout <= 1) then
Doc.PageSetup.Orientation:=Layout; // 0-纵向 1-横向
// 打印预览
Doc.PrintPreview;
// 打印输出
// for i := 1 to Copies do Doc.PrintOut;
// WpsApp.Quit(0);
end;
end.
代码中涉及到一个类型和两个过程,其中:
- TCustomPaper 类型
该类型表示自定义纸张类型,其值包括:纸张宽度、高度和上下左右边距值。
- PrintDocxStandard 过程
该过程实现在标准打印纸上的打印,也就是使用 Doc.PageSetup.PaperSize 来设置纸张,常用的纸张:A4 对应的 PaperSize 为 7, A5 对应的 PaperSize 为 9, B4 对应的 PaperSize 为 10, B5 对应的 PaperSize 为 11, A3 对应的PaperSize 为 6。
- PrintDocxCustom 过程
该过程实现在自定义纸张上的打印,也就是使用 TCustomPaper 类型的变量值进行纸张的设置。
过程参数:
- DocxFileName: String; 文档的文件名
- PrinterName: String; 打印机名称
- PageSize: Integer; 标准纸张的尺寸值
- Layout:Integer; 纸张方向
- Copies: Integer 打印份数
- CustomPaper: TCustomPaper; 自定义纸张尺寸