"Hello World" 来自 Delphi HotPDF 组件
欢迎来到关于 Delphi HotPDF 组件 的 "HelloWorld" 演示程序!
关于 HotPDF 组件
HotPDF 组件是一个强大且多功能的 PDF 处理库,专为 Delphi 和 C++Builder 开发者设计。这个全面的组件提供了一个广泛的 API,使开发者能够轻松、精确地创建、操作和修改 PDF 文档。
HotPDF 组件的关键特性:
- PDF 创建与生成: 从头开始创建 PDF 文档,并完全控制布局、格式和内容。
- 文本和图形支持: 向 PDF 页面添加文本、图像、形状和复杂图形。
- 多语言支持: 完全支持 Unicode,用于国际文本渲染。
- 字体管理: 嵌入字体,以确保不同系统上的文档外观一致。
- 压缩选项: 使用压缩算法来优化文件大小。
- 安全特性: 文档加密、密码保护和权限控制。
- 文档处理: 合并、拆分和修改现有PDF文档。
- 表格和表单支持: 创建复杂表格和交互式表单。

HelloWorld.dpr 程序概述
好的。 HelloWorld.dpr 示例程序是学习 HotPDF 组件的绝佳入门。
最基本和最重要的功能:TexOut 函数。这个演示程序也展示了基本的 PDF 创建功能。
核心功能:
1. PDF 文档创建
该程序的 主要功能是生成包含多语言“Hello World”文本的 PDF 文档。 CreatePDF 该过程演示了:
- 组件初始化: 正确设置和配置 THotPDF 组件。
- 文档属性: 设置元数据,例如压缩选项和字体嵌入。
- 多语言文本渲染: 支持11种语言的文本显示,包括英语、西班牙语、德语、法语、意大利语、葡萄牙语、俄语、日语、土耳其语、中文和韩语。
- 字体管理: 使用Arial Unicode MS字体以支持多语言显示。
- 压缩: 创建压缩的PDF文档。
2. 窗口管理与系统集成。
该程序包含高级窗口枚举功能,演示了:
- 系统窗口枚举: 使用 Windows API
EnumWindows函数来遍历所有顶级窗口 - 窗口标题匹配: 实现子字符串匹配来识别目标应用程序
- 自动窗口关闭: 发送
WM_CLOSE向关闭 PDF 查看器的消息,告知其可能存在打开的文件。 - 冲突预防: 确保在创建新的 PDF 文件时不会发生任何文件访问冲突。
3. 错误处理与资源管理
该程序展示了最佳实践,包括:
- 内存管理: 正确创建和销毁组件实例。
- 异常安全: 使用 try-finally 块以确保资源清理。
- 用户反馈: 提供控制台输出以告知用户程序进度。
技术实现细节:
压缩技术。
该程序创建包含 FlateDecode 压缩的 PDF 文档。
字体处理策略。
该程序采用一种复杂的字体选择策略:
- Arial Unicode MS: 适用于大多数语言,因为它具有广泛的字符支持。
- 字体嵌入: 确保在不同系统上具有一致的外观。
跨版本兼容性。
代码包含条件编译指令,以支持现代和旧版本的Delphi。
- XE2+ 支持: 使用命名空间单元 (WinApi.Windows, System.SysUtils 等)。
- 遗留支持: 对于较旧的Delphi版本,会回退到传统的单元名称。
教学价值。
这个HelloWorld示例具有多种教学目的:
- 组件介绍: 提供了一个对HotPDF组件用法的简单介绍。
- 最佳实践: 演示了正确的资源管理和错误处理方法。
- 系统集成: 演示如何将 PDF 生成与 Windows 系统操作集成。
- 国际化: 演示如何在 PDF 文档中处理多语言文本。
快速入门。
要运行此示例程序:
- 确保 HotPDF 组件已正确安装在您的 Delphi IDE 中。
- 打开 HelloWorld.dpr 项目文件。
- 编译并运行程序。
- 该程序将自动创建一个带有压缩的 PDF 文件。
- 检查生成的文档,以查看多语言文本的渲染效果。
源代码。
以下是 HelloWorld 演示程序的完整、带注释的源代码:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
{************************************************************} // HotPDF PDF Component // Copyright(c)2007-2025, https://www.loslab.com {************************************************************} // HelloWorld Demo Program for HotPDF Component // This program demonstrates basic PDF creation capabilities // including multilingual text support and compression options {************************************************************} program HelloWorld; {$APPTYPE CONSOLE} // Console application type {$I ..\..\..\Lib\HotPDF.inc} // Include HotPDF configuration file uses // EurekaLog units for memory leak detection and debugging (optional) {$IFDEF EurekaLog} EMemLeaks, // Memory leak detection EResLeaks, // Resource leak detection EDebugJCL, // JCL debugging support EDebugExports, // Export debugging information EFixSafeCallException, // SafeCall exception handling EMapWin32, // Win32 mapping support EAppConsole, // Console application support EDialogConsole, // Console dialog support ExceptionLog7, // Exception logging {$ENDIF EurekaLog} // System units - different namespaces for XE2+ vs older versions {$IFDEF XE2+} WinApi.Windows, // Windows API functions WinApi.Messages, // Windows message constants System.SysUtils, // System utilities System.Classes, // Base classes (TStringList, etc.) Vcl.Graphics, // Graphics support {$ELSE} Windows, // Windows API functions (older Delphi) Messages, // Windows message constants (older Delphi) SysUtils, // System utilities (older Delphi) Classes, // Base classes (older Delphi) Graphics, // Graphics support (older Delphi) {$ENDIF} HPDFDoc; // HotPDF main component unit // Creates a compressed PDF file with multilingual "Hello World" text // Parameters: // FileName: Output PDF file name procedure CreatePDF(const FileName: string); var HotPDF: THotPDF; // Main HotPDF component instance begin // Create HotPDF component instance HotPDF := THotPDF.Create(nil); try // Configure PDF properties HotPDF.AutoLaunch := true; // Automatically open PDF after creation HotPDF.FileName := FileName; // Set output file name // Enable FlateDecode compression HotPDF.Compression := cmFlateDecode; // Enable FlateDecode compression WriteLn('Creating compressed PDF: ', FileName); HotPDF.FontEmbedding := true; // Embed fonts for better compatibility HotPDF.BeginDoc; // Start PDF document creation // Set Unicode font for multilingual text support // Arial Unicode MS supports most languages except Korean // procedure SetFont ( FontName: AnsiString; FontStyle: TFontStyles; ASize: Single; FontCharset: TFontCharset = DEFAULT_CHARSET; IsVertical: boolean = false); // DEFAULT_CHARSET: 1 - Font is chosen based on Name. If the described font is not available on the system, Windows will substitute another font HotPDF.CurrentPage.SetFont('Arial Unicode MS', [], 12, 1, False); // Display "Hello World" in multiple languages // TextOut parameters: (X, Y, Angle, Text) HotPDF.CurrentPage.TextOut(80, 50, 0, 'Hello, Delphi PDF world!'); // English HotPDF.CurrentPage.TextOut(80, 70, 0, 'Hola, mundo Delphi PDF!'); // Spanish HotPDF.CurrentPage.TextOut(80, 90, 0, 'Hallo, Delphi PDF Welt!'); // German HotPDF.CurrentPage.TextOut(80, 110, 0, 'Bonjour, monde PDF Delphi!'); // French HotPDF.CurrentPage.TextOut(80, 130, 0, 'Ciao, mondo Delphi PDF!'); // Italian HotPDF.CurrentPage.TextOut(80, 150, 0, 'Olá, mundo Delphi PDF!'); // Portuguese HotPDF.CurrentPage.TextOut(80, 170, 0, 'Здравствуйте, Delphi PDF мир!'); // Russian HotPDF.CurrentPage.TextOut(80, 190, 0, 'こんにちは、Delphi PDFの世界!'); // Japanese HotPDF.CurrentPage.TextOut(80, 210, 0, 'Merhaba, Delphi PDF dünyası!'); // Turkish HotPDF.CurrentPage.TextOut(80, 230, 0, '你好,Delphi PDF世界'); // Chinese HotPDF.CurrentPage.TextOut(80, 250, 0, '여보세요, Delphi PDF 세계!'); // Korean HotPDF.EndDoc; // Finalize and save the PDF document WriteLn('PDF created successfully: ', FileName); finally HotPDF.Free; // Clean up HotPDF component end; end; // Global variables for window enumeration and PDF creation var Title: String; // Individual title string (unused in current implementation) Titles: TStrings; // List of window titles to search for Window: HWND; // Window handle (unused in current implementation) WindowText: array [0 .. 255] of Char; // Buffer for window text (unused in current implementation) WindowTitle: String; // Window title string (unused in current implementation) I: Integer; // Loop counter (unused in current implementation) // Callback function for EnumWindows API // This function is called for each top-level window in the system // Parameters: // hWnd: Handle to the current window being enumerated // lParam: User-defined data (in our case, pointer to TStrings with target titles) // Returns: True to continue enumeration, False to stop function EnumWindowsProc(HWND: HWND; lParam: lParam): BOOL; stdcall; var Titles: TStrings; // List of target window titles I: Integer; // Loop counter WindowText: array [0 .. 255] of Char; // Buffer to store window text WindowTitle: String; // Current window's title as string begin Result := true; // Continue enumeration by default Titles := TStrings(lParam); // Cast lParam back to TStrings // Get the window title text if GetWindowText(HWND, WindowText, SizeOf(WindowText)) > 0 then begin WindowTitle := String(WindowText); // Convert char array to string // Check if window title contains any of our target strings for I := 0 to Titles.Count - 1 do begin // Use Pos function to check if target string is contained in window title if Pos(Titles[I], WindowTitle) > 0 then begin // Send WM_CLOSE message to close the window PostMessage(HWND, WM_CLOSE, 0, 0); Break; // Exit loop after finding first match end; end; end; end; // Main program execution begins here begin // Close any existing PDF files that might be open in PDF viewers // This prevents file access conflicts when creating new PDFs with same names Titles := TStringList.Create; try // Define list of window title substrings to search for // Any window containing these strings will be closed // This will close Adobe and Foxit PDF reader & editor and many other PDF apps Titles.CommaText := '"HelloWorld.pdf", "Foxit"'; // Enumerate all top-level windows and close matching ones // EnumWindows calls our callback function for each window EnumWindows(@EnumWindowsProc, lParam(Titles)); finally Titles.Free; // Clean up string list end; // Display program header WriteLn('=== HotPDF HelloWorld Demo ==='); WriteLn('This demo creates a compressed PDF file with multilingual text.'); WriteLn(''); // Create compressed PDF CreatePDF('HelloWorld.pdf'); // Display completion message WriteLn(''); WriteLn('PDF file created successfully!'); WriteLn('The PDF file uses FlateDecode compression for optimal file size.'); WriteLn(''); WriteLn('Press Enter to exit...'); ReadLn; // Wait for user input before closing console end. |