| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1617 人关注过本帖, 2 人收藏
标题:请教大佬,用什么方法可以将一个图片转成一个PDF文件
只看楼主 加入收藏
laibinhua
Rank: 2
等 级:论坛游民
威 望:1
帖 子:295
专家分:38
注 册:2010-12-23
结帖率:58.02%
收藏(2)
已结贴  问题点数:10 回复次数:16 
请教大佬,用什么方法可以将一个图片转成一个PDF文件
请教大佬,用什么方法可以将一个图片转成一个PDF文件,看到段VB的代码,VFP里也能实现吗?
    ''' <summary>
    '''  jpg图片转pdf文件
    ''' </summary>
    ''' <param name="jpgfile">jpg文件</param>
    ''' <param name="pdf">PDF文件</param>
    Public Sub ConvertJPG2PDF(ByVal jpgfile As String, ByVal pdf As String)
        Dim document = New Document(iTextSharp.text.PageSize.A4, 1, 1, 1, 1)
        Dim stream = New FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None)
        PdfWriter.GetInstance(document, stream)
        document.Open()
        Dim imageStream = New FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imageStream)
        If (image.Height > (iTextSharp.text.PageSize.A4.Height - 25)) Then
            image.ScaleToFit((iTextSharp.text.PageSize.A4.Width - 25), (iTextSharp.text.PageSize.A4.Height - 25))
        ElseIf (image.Width > (iTextSharp.text.PageSize.A4.Width - 25)) Then
            image.ScaleToFit((iTextSharp.text.PageSize.A4.Width - 25), (iTextSharp.text.PageSize.A4.Height - 25))
        End If

        image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE
        document.Add(image)
        document.Close()
    End Sub

[此贴子已经被作者于2023-6-25 14:00编辑过]

搜索更多相关主题的帖子: 文件 image document text PDF 
2023-06-25 13:46
zhken
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:11
帖 子:285
专家分:620
注 册:2012-9-21
收藏
得分:2 
导入word后另存为pdf
2023-06-25 14:44
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10608
专家分:43190
注 册:2014-5-20
收藏
得分:2 
VFP报表用pdf打印设备输出
2023-06-25 15:26
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10608
专家分:43190
注 册:2014-5-20
收藏
得分:0 
或者通过WebView2由html输出pdf
2023-06-25 16:13
星光悠蓝
Rank: 9Rank: 9Rank: 9
来 自:山水甲天下
等 级:贵宾
威 望:52
帖 子:525
专家分:1278
注 册:2010-1-11
收藏
得分:2 
直接用Adobe Acrobat打开后另存
2023-06-25 17:25
sam_jiang
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:790
专家分:1170
注 册:2021-10-13
收藏
得分:2 
我查看过pdf文件,是个类似文本文件,你可以网上搜一下pdf格式,自己写一个。。。
参考这篇文章:
https://www.

[此贴子已经被作者于2023-6-26 06:41编辑过]

2023-06-25 19:21
nbwww
Rank: 8Rank: 8
等 级:贵宾
威 望:11
帖 子:333
专家分:810
注 册:2021-1-9
收藏(1)
得分:2 
程序代码:
***********************************************************************
**  搜索所选文件夹下面的子文件夹下面的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 


libhpdf.rar (231.5 KB)
2023-06-25 19:52
pvm2000
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:180
专家分:312
注 册:2022-12-22
收藏
得分:2 
很棒,收藏了
2023-06-26 02:22
schtg
Rank: 12Rank: 12Rank: 12
来 自:Usa
等 级:贵宾
威 望:67
帖 子:1744
专家分:3333
注 册:2012-2-29
收藏
得分:2 
回复 7楼 nbwww
谢谢分享!
2023-06-26 06:01
aqyejun
Rank: 4
等 级:贵宾
威 望:10
帖 子:147
专家分:113
注 册:2010-6-11
收藏
得分:2 

【独叶为舟】工作室
2023-06-26 08:22
快速回复:请教大佬,用什么方法可以将一个图片转成一个PDF文件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051668 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved