程序代码:
***********************************************************************
** 搜索所选文件夹下面的子文件夹下面的JPG文件(其他的文件类型要自己加)生成PDF,以子文件夹名称为PDF文件名
** 俊文电脑 2022.1.8 代码参照木瓜等其他高手
***********************************************************************
CLOSE DATABASES ALL
SET ANSI ON
set date to ansi
set dele on
SET CURRENCY LEFT
SET DELETED ON
SET EXACT ON
SET EXCLUSIVE OFF
SET HOURS TO 24
SET NOTIFY OFF
SET TALK OFF
SET SAFETY OFF
SET SEPARATOR TO ','
set dele on
set mult on
set cent on
SET ENGINEBEHAVIOR 70
MAINPATH = SYS(16)
RUNDIRECTORY = SUBSTR(MAINPATH,1,RAT('\',MAINPATH) - 1)
RUNDIRECTORY1 = SUBSTR(MAINPATH,1,RAT('\',MAINPATH) - 1)
SET DEFAULT TO (RUNDIRECTORY)
SET PATH TO (RUNDIRECTORY1)
dir1=GETDIR("11","指定档案所在文件夹(自动搜索下级文件夹),"+CHR(13)+"PDF将生成在所选文件夹","选择文件夹")
IF 遍历磁盘文件(dir1, "*.*", "文件表", .T.)
SELECT 文件表
REPLACE 文件名 WITH RTRIM(文件名) ALL
GO TOP
COPY TO 文件夹 FOR 文件属性="....D"
SELECT 0
USE 文件夹
总数量=0
SCAN
afile1=ALLTRIM(文件夹.文件名)
SELECT 文件表
#define HPDF_COMP_ALL 0x0F
detail_font_name="报告"
declareDlls()
pdf = HPDF_New (0, 0)
HPDF_UseCNSFonts(pdf)
HPDF_UseCNSEncodings (pdf)
HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL)
有图片=0
SCAN FOR RIGHT(ALLTRIM(文件表.文件夹),LEN(ALLTRIM(文件夹.文件名))+1)=ALLTRIM(文件夹.文件名)+'\' AND RIGHT(ALLTRIM(文件表.文件名),3)='JPG'
lcFile=ALLTRIM(文件表.文件夹)+ALLTRIM(文件表.文件名)
page1 = HPDF_AddPage (pdf)
lnHandle=HPDF_LoadJpegImageFromFile(pdf, lcFile)
HPDF_Page_DrawImage(Page1, lnHandle, 0,0,620,878)
有图片=1
ENDSCAN
IF 有图片=1
HPDF_SaveToFile (pdf,(dir1)+'\'+afile1+".pdf")
HPDF_Free (pdf)
总数量=总数量+1
ENDIF
ENDSCAN
ENDIF
=MESSAGEBOX("转换生成了 "+ALLTRIM(STR(总数量))+" 个PDF文件",0+64,"转换完成")
RETURN
***********************************************************************
** 遍历磁盘文件(c文件夹, c文件名, c临时表名, b所有子文件夹)
**
** 参数:c文件夹........要搜索的文件夹
** c文件名........要搜索的文件名,可用通配符,如“*.*”
** c临时表名......存放搜索结果的临时表别名
** b所有子文件夹..是否搜索所有子文件夹,为.F.时只搜索“c文件夹”
**
** 返回:正常返回.T.,异常返回.F.
**
** 例:
**
** IF 遍历磁盘文件("C:\temp\", "*.*", "文件表", .T.)
** SELECT 文件表
** REPLACE 文件名 WITH SPACE(级别*2) + RTRIM(文件名) ALL
** GO TOP
** BROWSE FIELDS 文件名:50,;
** 文件大小:P="9,999,999,999",;
** 修改日期, 修改时间, 文件属性, 文件夹
** USE
** ENDIF
***********************************************************************
FUNCTION 遍历磁盘文件(c文件夹, c文件名, c临时表名, b所有子文件夹)
c文件夹 = ADDBS(c文件夹)
IF (ADIR(a文件, c文件夹 + c文件名, "RASHD") > 0) && 取首层文件夹信息
* 保存文件信息的数组
* 文件信息结构:
* 文件名, 文件大小, 修改日期, 修改时间, 文件属性, 文件夹, 级别
DIMENSION a文件表(1, 7)
LOCAL i, n行号, n行数, n增加行数
n增加行数 = ALEN(a文件, 1)
DIMENSION a文件表[n增加行数, 7] && 增加文件数
ASORT(a文件, 1) && 按文件名排序
FOR i = 1 TO n增加行数 && 保存文件信息
a文件表[i,1] = a文件[i,1]
a文件表[i,2] = a文件[i,2]
a文件表[i,3] = a文件[i,3]
a文件表[i,4] = a文件[i,4]
a文件表[i,5] = a文件[i,5]
a文件表[i,6] = c文件夹
a文件表[i,7] = 0
ENDFOR
RELEASE a文件 && 释放“a文件”数组资源
IF !EMPTY(b所有子文件夹) && 如果搜索所有子文件夹
n行号 = 1 && 从第一个文件开始遍历搜索
n行数 = ALEN(a文件表, 1) && 总文件数
DO WHILE (n行号 <= n行数) && 遍历所有子文件夹
* 如果是文件夹
IF (RIGHT(a文件表[n行号,5],1) == "D") AND !INLIST(a文件表[n行号,1], ".", "..")
* 文件夹名
c文件夹 = a文件表[n行号,6] + a文件表[n行号,1] + "\"
* 用“[]”符号括起文件夹名表示
a文件表[n行号,1] = a文件表[n行号,1]
n级别 = a文件表[n行号, 7] + 1 && 文件顺序级别
IF (ADIR(a文件, c文件夹 + c文件名, "RASHD") > 0) && 取文件夹信息
n增加行数 = ALEN(a文件, 1) && 文件数
DIMENSION a文件表[n行数 + n增加行数, 7] && 增加文件信息行数
* 插入增加的文件信息
FOR i = n行数 TO n行号+1 STEP -1 && 原下面文件信息向底部移动
a文件表[i + n增加行数, 1] = a文件表[i, 1]
a文件表[i + n增加行数, 2] = a文件表[i, 2]
a文件表[i + n增加行数, 3] = a文件表[i, 3]
a文件表[i + n增加行数, 4] = a文件表[i, 4]
a文件表[i + n增加行数, 5] = a文件表[i, 5]
a文件表[i + n增加行数, 6] = a文件表[i, 6]
a文件表[i + n增加行数, 7] = a文件表[i, 7]
ENDFOR
ASORT(a文件, 1) && 按文件名排序
FOR i = 1 TO n增加行数 && 插入新增加的文件信息
a文件表[n行号 + i, 1] = a文件[i, 1]
a文件表[n行号 + i, 2] = a文件[i, 2]
a文件表[n行号 + i, 3] = a文件[i, 3]
a文件表[n行号 + i, 4] = a文件[i, 4]
a文件表[n行号 + i, 5] = a文件[i, 5]
a文件表[n行号 + i, 6] = c文件夹
a文件表[n行号 + i, 7] = n级别
ENDFOR
RELEASE a文件 && 释放“a文件”数组资源
n行数 = n行数 + n增加行数 && 总文件数
ENDIF
ENDIF
n行号 = n行号 + 1 && 继续处理下一个文件
ENDDO
ENDIF
* 将文件信息数据存入临时表
CREATE CURSOR (c临时表名) (文件名 C(240), 文件大小 I, 修改日期 D, 修改时间 C(8),;
文件属性 C(5), 文件夹 C(240), 级别 I)
APPEND FROM ARRAY a文件表 FOR !INLIST(RTRIM(文件名), ".", "..")
RELEASE a文件表 && 释放“a文件表”数组资源
RETURN .T.
ELSE
RETURN .F.
ENDIF
ENDFUNC
Procedure declareDlls
Declare Integer HPDF_New In libhpdf.dll Integer, Integer
Declare Integer HPDF_Free In libhpdf.dll Integer
Declare Integer HPDF_SaveToFile In libhpdf.dll Integer, String
Declare Integer HPDF_GetError In libhpdf.dll Integer
Declare Integer HPDF_ResetError In libhpdf.dll Integer
Declare Integer HPDF_SetPageMode In libhpdf.dll Integer, Integer
Declare Integer HPDF_GetCurrentPage In libhpdf.dll Integer
Declare Integer HPDF_AddPage In libhpdf.dll Integer
Declare Integer HPDF_Page_SetWidth In libhpdf.dll Integer, Single
Declare Integer HPDF_Page_SetHeight In libhpdf.dll Integer, Single
Declare Integer HPDF_GetFont In libhpdf.dll Integer, String, String
Declare String HPDF_LoadTTFontFromFile In libhpdf.dll Integer, String, Integer
Declare String HPDF_LoadTTFontFromFile2 In libhpdf.dll Integer, String, Integer
Declare Integer HPDF_GetEncoder In libhpdf.dll Integer, String
Declare Integer HPDF_GetCurrentEncoder In libhpdf.dll Integer
Declare Integer HPDF_SetCurrentEncoder In libhpdf.dll Integer, String
Declare Integer HPDF_Encoder_GetType In libhpdf.dll Integer
Declare Integer HPDF_Encoder_GetByteType In libhpdf.dll Integer, String, Integer
Declare String HPDF_Encoder_GetUnicode In libhpdf.dll Integer, String
Declare Integer HPDF_Encoder_GetWritingMode In libhpdf.dll Integer
Declare Integer HPDF_UseJPEncodings In libhpdf.dll Integer
Declare Integer HPDF_UseKREncodings In libhpdf.dll Integer
Declare Integer HPDF_UseCNSEncodings In libhpdf.dll Integer
Declare Integer HPDF_UseCNTEncodings In libhpdf.dll Integer
Declare Integer HPDF_LoadPngImageFromFile In libhpdf.dll Integer, String
Declare Integer HPDF_LoadJpegImageFromFile In libhpdf.dll Integer, String
Declare Integer HPDF_Image_GetWidth In libhpdf.dll Integer
Declare Integer HPDF_Image_GetHeight In libhpdf.dll Integer
Declare Integer HPDF_SetInfoAttr In libhpdf.dll Integer, Integer, String
Declare Integer HPDF_SetPassword In libhpdf.dll Integer, String, String
Declare Integer HPDF_SetPermission In libhpdf.dll Integer, Integer
Declare Integer HPDF_SetEncryptionMode In libhpdf.dll Integer, Integer, Integer
Declare Integer HPDF_SetCompressionMode In libhpdf.dll Integer, Integer
Declare Integer HPDF_Font_MeasureText In libhpdf.dll Integer, String, Integer, Single, Single, Single, Single, Integer, Single @
Declare Single HPDF_Page_GetWidth In libhpdf.dll Integer
Declare Single HPDF_Page_GetHeight In libhpdf.dll Integer
Declare Single HPDF_Page_TextWidth In libhpdf.dll Integer, String
Declare Integer HPDF_Page_GetCurrentFont In libhpdf.dll Integer
Declare Integer HPDF_Page_MeasureText In libhpdf.dll Integer, String, Single, Integer, Single @
Declare Integer HPDF_Page_GetRGBFill In libhpdf.dll Integer
Declare Integer HPDF_Page_GetCurrentFont In libhpdf.dll Integer
Declare Single HPDF_Page_GetCurrentFontSize In libhpdf.dll Integer
Declare Integer HPDF_Page_SetLineWidth In libhpdf.dll Integer, Single
Declare Integer HPDF_Page_SetDash In libhpdf.dll Integer, String, Integer, Integer
Declare Integer HPDF_Page_MoveTo In libhpdf.dll Integer, Single, Single
Declare Integer HPDF_Page_LineTo In libhpdf.dll Integer, Single, Single
Declare Integer HPDF_Page_ClosePath In libhpdf.dll Integer
Declare Integer HPDF_Page_Rectangle In libhpdf.dll Integer, Single, Single, Single, Single
Declare Integer HPDF_Page_Concat In libhpdf.dll Integer, Single, Single, Single, Single, Single, Single
Declare Integer HPDF_Page_SetCharSpace In libhpdf.dll Integer, Single
Declare Integer HPDF_Page_SetWordSpace In libhpdf.dll Integer, Single
Declare Integer HPDF_Page_SetHorizontalScalling In libhpdf.dll Integer, Single
Declare Integer HPDF_Page_SetTextLeading In libhpdf.dll Integer, Single
Declare Integer HPDF_Page_SetTextRise In libhpdf.dll Integer, Single
Declare Integer HPDF_Page_Stroke In libhpdf.dll Integer
Declare Integer HPDF_Page_ClosePathStroke In libhpdf.dll Integer
Declare Integer HPDF_Page_Fill In libhpdf.dll Integer
Declare Integer HPDF_Page_FillStroke In libhpdf.dll Integer
Declare Integer HPDF_Page_EndPath In libhpdf.dll Integer
Declare Integer HPDF_Page_BeginText In libhpdf.dll Integer
Declare Integer HPDF_Page_EndText In libhpdf.dll Integer
Declare Integer HPDF_Page_SetFontAndSize In libhpdf.dll Integer, Integer, Single
Declare Integer HPDF_Page_SetTextRenderingMode In libhpdf.dll Integer, Integer
Declare Integer HPDF_Page_MoveTextPos In libhpdf.dll Integer, Single, Single
Declare Integer HPDF_Page_MoveToNextLine In libhpdf.dll Integer
Declare Integer HPDF_Page_SetRGBFill In libhpdf.dll Integer, Single, Single, Single
Declare Integer HPDF_Page_SetRGBStroke In libhpdf.dll Integer, Single, Single, Single
Declare Integer HPDF_Page_Ellipse In libhpdf.dll Integer, Single, Single, Single, Single
Declare Integer HPDF_Page_DrawImage In libhpdf.dll Integer, Integer, Single, Single, Single, Single
Declare Integer HPDF_Page_TextRect In libhpdf.dll Integer, Single, Single, Single, Single, String, Integer, Integer
Declare Integer HPDF_Page_TextOut In libhpdf.dll Integer, Single, Single, String
Declare Integer HPDF_Page_SetTextMatrix In libhpdf.dll Integer ,Single, Single, Single, Single, Single, Single
Declare Integer HPDF_Page_ShowText In libhpdf.dll Integer, String
Declare Integer HPDF_Page_CurveTo In libhpdf.dll Integer, Single, Single, Single, Single, Single, Single
Declare integer HPDF_UseCNSFonts In libhpdf.dll Integer
EndProc