Print Documents From Delphi - Print PDF, DOC, XLS, HTML, RTF, DOCX, TXT

Delphi 打印 Tprinter(1) 打印          打印对于许多 Windows 程序员来说是十分棘手的问题。         Delphi 简化了打印时用户所必须了解的大部分内容。用户可以很轻松地写出简单的打印程序来输出文本和位图化了的图像。 对于更为复杂的打印,仅需了解几个要领和技术,用户就可以实现任何类型的自定义打印。 当了解了这些后,打印也就不那么困难了。 Delphi 的TPrinter对象封装了 W 阅读详情

 

If your Delphi application needs to operate on various types of files, one of the tasks you might have for your application is to allow the user of the application to print a file, whatever the file type is.

Most document oriented applications, like MS Word, MS Excel or Adobe "know" how to print documents they are "in charge of". For example, Word saves the text you write in documents with DOC extension. Since Word (Microsoft) determines what is the "raw" contents of a .DOC file it knows how to print .DOC files. The same applies for any "known" file type holding some printable information.

What if you need to print various types of documents / files from your application? Can you know how to send the file to the printer in order for it to be printed correctly? I guess the answer is no. At least I do not know :)

Print Any Type of Document (PDF, DOC, XLS, HTML, RTF, DOCX) Using Delphi

So, how do you print any type of document, programmatically using Delphi code?

Well I guess we should "ask" Windows: what application knows how to print, for example, a PDF file. Or even better we should tell to Windows: here's one PDF file, send it to the application associated / in charge of printing PDF files.

Open up Windows Explorer, navigate to a directory containing some printable files. For most of the file types on your system, when you right click a file in Windows Explorer, you will locate the "Print" command. Executing the Print shell command, will result in the file being sent to the default printer.

Well, that's exactly what we want - for a file type, call a method that will send the file to the associated application for printing.

The function we are after is the ShellExecute API function.

ShellExecute: Print / PrintTo

At its simplest, ShellExecute lets you programmatically start any application / open any file which is installed on the user's machine.

However, ShellExecute can do much more. ShellExecute can be used to launch application, open Windows Explorer, initiate a search beginning in the specified directory - and what's of most importance for us right now: prints the specified file.

Specify Printer for ShellExecute / Print

Here's how to print a file using the ShellExecute function:

ShellExecute(Handle, ' print', PChar('c:/document.doc'), nil, nil, SW_HIDE) ;

Note the second parameter : "print".

Using the above call, a document "document.doc" located on the root of the C drive will be sent to the Windows default printer.

ShellExecute always uses the default printer for the "print" action.

What if you need to print to a different printer, what if you want to allow the user to change the printer?

The PrintTo Shell Command

Some applications support the 'printto' action. PrintTo can be used to specify the name of the printer used for the print action. Printer is determined by 3 parameter: printer name, drive name and port.

Programmatically Printing Files

Ok, enough theory. Time for some real code:

Before you copy and paste: the Printer global variable (TPrinter type) available in all Delphi programs can be used to manage any printing performed by an application. Printer is defined in the "printers" unit, ShellExecute is defined in the "shellapi" unit.

  1. Drop a TComboBox on a form. Name it "cboPrinter". Set Style to csDropDownLidt
  2. Put the next two lines in the form's OnCreate even handler:
    //have available printers in the combo box
    cboPrinter.Items.Assign(printer.Printers);
    //pre-select the default / active printer
    cboPrinter.ItemIndex := printer.PrinterIndex;

Now, here's the function you can use to print any document type to a specified printer:

uses shellapi, printers;

procedure PrintDocument( const documentToPrint : string) ;
var
  printCommand : string;
  printerInfo : string;
  Device, Driver, Port: array[0..255] of Char;
  hDeviceMode: THandle;
begin

   if Printer.PrinterIndex = cboPrinter.ItemIndex then
   begin
    printCommand := 'print';
    printerInfo := '';
   end
  else
  begin

    printCommand := 'printto';
    Printer.PrinterIndex := cboPrinter.ItemIndex;
    Printer.GetPrinter(Device, Driver, Port, hDeviceMode) ;
    printerInfo := Format('"%s" "%s" "%s"', [Device, Driver, Port]) ;
   end;

  ShellExecute(Application.Handle, PChar(printCommand), PChar(documentToPrint), PChar(printerInfo), nil, SW_HIDE) ;
end;

Note: if the selected printer is the default one, the function uses "print" action. If the selected printer is not the default one, the function uses the "printo" method.

Note, also: some document types do NOT have an application associated for printing. Some do not have the "printto" action specified.

Delphi 打印实例 unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,printers,inifiles, StdCtrls; type TForm1 = class(Tform 阅读详情

相关推荐

Delphi PrintText打印文本文件内容

Delphi PrintText打印文本文件内

delphi打印pdf文件_在Delphi应用程序中使用Adobe Acrobat(PDF)文件

delphi打印pdf文件 Delphi supports the display of Adobe PDF files from within an application. As long as you've got Adobe Reader installed, your PC will automatically have the relevant ActiveX control you'...

cumao2792的博客 2905

DELPHI PDF文件打印

DELPHI 正确打印PDF文件,解决PDF文件打印顺序问题。好用请帮忙给个5星

delphi acropdf打印多个pdf_将多个PDF文件整合成一个文件

pdfFactory不仅可以将单个文件创建为PDF文件进行打印,还可以将多个文件整合为一个PDF文件,同时,也可以随时删除其中的一些文件,创建新的PDF文件。图1:pdfFactory工具界面一、 载入第一个文档我们今天将以Word文档来演示具体的操作。首先,我们需要将第一个文档载入到pdfFactory中。如图2所示,打开文件菜单中的“打印”面板,并在打印机下拉菜单中选择“pdfFactory ...

weixin_39752087的博客 758

SSE消息推送避坑指南:从Chrome兼容性到Spring超时配置

本文深入探讨了在Java Spring Boot项目中实现SSE消息推送时,从浏览器兼容性到后端配置的完整避坑指南。针对Chrome并发连接限制、Safari自动断开、Nginx代理缓冲以及Spring SseEmitter内存泄漏与超时管理等生产环境常见问题,提供了具体的解决方案和最佳实践配置,帮助开发者构建稳定可靠的实时推送系统。

work3的博客 453

从梯度泄露到数据复原:DLG与iDLG算法实战解析

本文深入解析了DLG与iDLG算法在梯度泄露与数据复原中的实战应用,揭示了联邦学习中的隐私风险。通过详细代码示例和实验数据,展示了如何从梯度中复原原始训练数据,并探讨了梯度压缩、差分隐私等防御措施。文章特别强调了iDLG算法在标签破解中的高效性,为AI安全领域提供了重要参考。

weixin_30552811的博客 198

Delphi文本和图形的打印

 对于每一个Windows应用程序开发者来说,打印功能的实现都是极为棘手的,因为要涉及到相当多的代码,而且常常是相当复杂,但是在Delphi中实现文本和图像的打印功能却是非常容易的。 文本的打印功能     Delphi中文本的打印功能就如同在DOS中一样容易,需要做的只是取得打印参数,打开打印机,然后发送文本的每一行内容。在Delphi中提供了一个printers程序单元,它说明了一个tprin

walce的专栏 5768

[捷稀 JCG Q30 Pro 刷机说明] 以刷入openwrt24.10为例

摘要: 本文详细介绍了捷稀JCG Q30 Pro路由器刷入OpenWrt 24.10系统的步骤。通过先刷入OpenWrt 23.05过渡固件,再升级Uboot,最后通过TFTP加载工厂镜像完成刷机。关键步骤包括解锁分区、刷写Uboot、进入恢复模式及配置网络。文章还提供了无线设置、Tailscale内网穿透等进阶命令,并附参考文档和固件下载链接。适用于有一定技术基础的玩家,需注意操作风险,严格按流程执行。

贤时间 7239

【亲测免费】 DELPHI PDF文件打印解决方案

DELPHI PDF文件打印解决方案 去发现同类优质开源项目:https://gitcode.com/ 此仓库提供的资源文件是针对DELPHI环境下打印PDF文件的专项解决方案。该方案能够有效解决DELPHI在打印PDF文件时常见的顺序问题,确保打印输出符合预期。 使用本资源文件后,您将能够轻松实现DELPHI环境中PDF文件的正确打印,提升工作效率。如果您在使用过程中发现该工具对您有所帮助,欢迎...

gitblog_06757的博客 524

智能体评测技术与实践:从评估维度到DeepEval实战指南

智能体评测技术与实践:从评估维度到DeepEval实战指南 本文系统探讨了AI智能体的评测体系与实践方法。首先从四个关键维度构建评估框架:基础能力(意图理解、知识完整性)、复杂任务(多轮对话、动态工作流)、可靠性(幻觉抑制、安全合规)以及工具效率(参数填充、多工具协同)。其次分析了主流评测工具,包括xBench的动态双轨评估、DeepEval的全链路监控等解决方案。最后详细介绍了DeepEval的实战应用,涵盖环境配置、测试用例构建、指标配置及CI/CD集成,展示了如何通过代码化评估实现智能体质量的持续提升

Linda的自动化测试之旅 3453

推荐算法之Thompson(汤普森)采样

如果想理解汤普森采样算法,就必须先熟悉了解贝塔分布。 一、Beta(贝塔)分布 Beta分布是一个定义在[0,1]区间上的连续概率分布族,它有两个正值参数,称为形状参数,一般用α和β表示,Beta分布的概率密度函数形式如下: 这里的Γ表示gamma函数。 Beta分布的均值是: 方差: ...

weixin_30508241的博客 4261

用Delphi打印各类文档

ShellExecute(Handle, print, PChar(c:/document.doc), nil, nil, SW_HIDE) ;Drop a TComboBox on a form. Name it "cboPrinter". Set Style to csDropDownLidtPut the next two lines in the fo

SUNSTONE的Delphi笔记 2630

软考高级-系统架构设计师 知识点整理 (一篇就够了 建议收藏)

软考高级-系统架构设计师知识点整理---十万字总结,助你通过高级

片言可决讼,半纸能缚龙 7381

DELPHIPDF文件打印解决方案:高效打印PDF文件的专项工具

DELPHIPDF文件打印解决方案:高效打印PDF文件的专项工具 去发现同类优质开源项目:https://gitcode.com/ 在软件开发领域,DELPHI环境下PDF文件的打印一直是一个令人头疼的问题。本文将为您介绍一款针对该问题的专项解决方案——DELPHIPDF文件打印解决方案,帮助您轻松应对这一挑战。 项目介绍 DELPHIPDF文件打印解决方案是一款专门为DELPHI环境下打印PDF...

gitblog_06703的博客 750

汽车OTA 测试用例

汽车OTA 测试用例

天赐好车的博客 992

Serial RapidIO Gen2 IP 说明(一)

概述 高速IO(RapidIO)互联架构,用来兼容当前最流行的集成通信处理器、主处理器、联网的数字信号处理器,是一种高性能、分组交换(报文交换)的互联技术。它解决了高性能嵌入式产业稳定性、高带宽以及系统内部更快的总线速率的需求。 高速IO(RapidIO)标准由三层定义:逻辑层、传输层、物理层。逻辑层定义了所有的协议内容和报文格式。这些是终端发起和完成一次传输所必需的信息。传输层提供了报文从一个端点传输到另外一个端点必要的路线信息。物理层则描述了设备级的端口声明,如报...

记录所学,分享知识,结识挚友 1504

delphi acropdf打印多个pdf_PDF合并打印技巧!这样打印!速度6到飞起

我们在工作中经常都会跟PDF格式的文件打交道,PDF文件有着其他格式文件无法比拟的优越之处,在传输和打印的过程中PDF有很好的稳定性,无需担心格式错乱等问题,同时又有着很好的兼容性,所以我们经常会把文件转化成PDF格式之后再打印。不过有时候我们需要打印比较多的PDF文件,那么有什么办法可以快速打印呢?小编来教你一招!1、PDF文件合并首先我们可以将多个PDF文件合并成一个文件,然后再打印,这样不仅...

weixin_39792686的博客 715

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

【亲测免费】 SynPDF Delphi 全源码:轻松实现 PDF 文档生成

SynPDF Delphi 全源码:轻松实现 PDF 文档生成 【下载地址】SynPDFDelphi全源码下载 SynPDF Delphi 全源码下载 项目地址: https://gitcode.com/open-source-t...

gitblog_09759的博客 1439

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Delphi6中实现PDF文件打印功能

推荐使用TMS PDF Component或llPDFLib等商业库,提供完整的PDF处理API。安装组件后,通过创建实例、加载文件、设置打印参数(如页码范围/质量)并调用打印方法实现。对于稳定性要求高的场景,建议采用DELPHIPDF文件打印解决方案,该开源工具专门处理打印顺序异常问题,支持多版本Delphi环境。通过导入Adobe Acrobat的ActiveX控件,在窗体添加PDF阅读器组件。属性指向PDF路径,调用控件的打印方法即可输出,适合需要预览后打印的场景。需先获取打印机列表,再通过。

chinalog的专栏 718

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Delphi 打印对象 Tprinter 常用属性、方法、函数、打印示例

在 Delphi 中提供了一个 Printers程序单元,它说明了一个 TPrint 对象,封装了Windows 打印工作和输出打印机之间的接口,并提供常用的属性和方法,其中画布Canvas 是一个非常有用的属性,它代表了当前打印文件的表面,是以图形方式来工作的,整个的打印输出工作仅仅是将用户打印的内容输出到TPrinter的属性 Canvas 上,当全部的输出工作完成之后,打印对象(TPrinter)把 Canvas 的属性值送到打印机上。这时,就要注意屏幕的分辨率和打印机的分辨率的差别了。

全粘工程师 1225

delphi acropdf打印多个pdf_如何打印出一张好看的思维导图?

哈咯,大家好,为大家答疑解惑的酱酱已上线。很多小伙伴喜欢把思维导图笔记打印出来,再进行记忆和背诵。但在打印过程中总是会遭遇以下各类难题:内容多时打印出来的字太小,整张图很拥挤;打印出的内容很模糊,有些主题看不清字;打印出的导图空白太多,纸张利用率不高;做的导图笔记太长太宽,很难完整打印;......想打印出一张清晰、好看的思维导图似乎并简单,很多小伙伴甚至会花钱请人帮忙。但其实大可不必,掌握以下秘...

weixin_39620099的博客 550
上一篇: Group Items in Delphi's TListView Control
下一篇: TCoolMemo
iseekcode
博客等级 码龄18年 36粉丝 14原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址