转换图片到pdf

这篇博客介绍了如何利用iTextSharp库将图像(无论是来自数据库的二进制流还是硬盘上的文件)转换为PDF。代码示例展示了两个函数,一个处理二进制图像数据,另一个处理硬盘上的图像文件。转换过程包括调整图像尺寸以适应A4或Letter页面,并将图像居中。最后,展示了如何在ASP.NET页面中调用这些函数并输出PDF。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Imports System.Drawing
Imports System.Text
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

 

'首先需要引用itextsharp.dll,一个开源dll,直接到官网下载

 

'此函数主要处理从数据库等地来的图像,接收二进制流

   Public Function ConvertImageToPDF(ByVal fileBuffer As Byte()) As Byte()

        Dim pages As New List(Of [String])()
        Dim result() As Byte
        Dim image As iTextSharp.text.Image
        Dim mStream As New MemoryStream()
        Dim bm As Bitmap
        Dim ImageStream As New MemoryStream(fileBuffer)
        Dim pageCount As Integer

        Dim document As New Document(iTextSharp.text.PageSize.LETTER, 20, 20, 20, 20)
        'pdf
        'PdfWriter.GetInstance(document, New FileStream(desPath + name & ".pdf", FileMode.OpenOrCreate))
        PdfWriter.GetInstance(document, mStream)
        document.Open()


        Try
            bm = New Bitmap(ImageStream)
            pageCount = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)

            For i As Integer = 0 To pageCount - 1 '处理多页
                bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, i)

                image = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp)
                ''A4
                '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
                'Letter
                If image.Height > iTextSharp.text.PageSize.LETTER.Height - 20 Then
                    image.ScaleToFit(iTextSharp.text.PageSize.LETTER.Width - 20, iTextSharp.text.PageSize.LETTER.Height - 20)
                ElseIf image.Width > iTextSharp.text.PageSize.LETTER.Width - 20 Then
                    image.ScaleToFit(iTextSharp.text.PageSize.LETTER.Width - 20, iTextSharp.text.PageSize.LETTER.Height - 20)
                End If
                'center
                image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE
                document.NewPage()
                document.Add(image)
            Next
        Catch ex As Exception

        End Try

        Try
            document.Close()
        Catch ex As Exception
            Return Nothing
        End Try

        result = mStream.GetBuffer
        Return result
    End Function

 

————————————————————————————————————————————

 

'此函数主要处理来自于硬盘的图像

    Public Function ConvertImageToPDF(ByVal desPath As String, ByVal name As String) As Byte()
        Dim ImageStream As FileStream

        Try
            ImageStream = New FileStream(desPath & name, FileMode.Open)
        Catch ex As Exception
            Return Nothing
        End Try

        Dim result(ImageStream.Length) As Byte
        ImageStream.Read(result, 0, result.Length)
        ImageStream.Close()

        result = ConvertImageToPDF(result)

        Return result
    End Function

 

 

——————————————————————————————————————————————

 

'使用

 

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

        '以将图片读取到二进制流中,模拟从数据库中取数据

        Dim path As String = "E:/PrintFiles/Image/"
        Dim name As String = "Customer Collections Detail0065.tif"

        Dim fileStream As New FileStream(path + name, FileMode.Open)
        Dim sRdr As New BinaryReader(fileStream)
        Dim buffer(fileStream.Length) As Byte

        sRdr.Read(buffer, 0, fileStream.Length)

 

        '转换图片到PDF

        Dim Attachment() As Byte = ConvertImageToPDF(buffer)

        '或者直接 Dim Attachment() As Byte = ConvertImageToPDF(path,name);注意取消上面这一段

 

        '输出到前端

        Response.Clear()

        Response.ContentType = "application/octet-stream"
        Response.ContentType = "application/pdf"

        Response.AddHeader("Content-Length", Attachment.GetLength(0).ToString)
        Response.AddHeader("Content-Disposition", "inline;filename=imageTest.pdf")
        Response.Filter.Close() 'Close the filter
        Response.BinaryWrite(Attachment)
        Response.Flush()

        Response.End()
    End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值