Click here to Skip to main content
15,885,648 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
Iam trying to add two images in vb.net pdf file it shows error one image adding properly working.again iam adding the code with another image variable it shows pdf blank with out writing any content.how can i add two images.

What I have tried:

VB
img = System.Drawing.Image.FromFile(strcollFileName)

            Using ms As New MemoryStream() '
                If (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) Then
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
                ElseIf (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp)) Then
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
                End If
                bytes2 = ms.ToArray()
            End Using
            If Not IsNothing(bytes2) Then
                If bytes2.GetUpperBound(0) > 0 Then
                    objpdf.ImageFlag = True
                    objpdf.ByteImageSetting(bytes2, "I1")
                End If
            End If

            If Not IsNothing(bytes2) Then
                If bytes2.GetUpperBound(0) > 0 Then
                    objpdf.ImageShowing("I1", 420, 680, 75, 75)
                    ' objpdf.ImageShowing("I1", 400, 350, 75, 75)
                    ' objpdf.ImageShowing("I2", 30, 500, 120, 75)

                    'objpdf.ImageShowing("I1", 8, 100, 65, 65)
                End If
            End If
Posted
Updated 1-Oct-16 6:58am
v3

1 solution

Please check this code, its for adding images to pdf by itextsharp

VB.NET
Dim sMergedFiles As String = "D:\Temp\BASE.PDF"
Dim sTiffFiles As String = "D:\Temp\IMG\"
Dim document As New Document(PageSize.A4, 50, 50, 50, 50)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(sMergedFiles, FileMode.CreateNew))
document.Open()
Dim imgFileName As String = Path.Combine(sTiffFiles, "A.bmp")
AddImg2Pdf(imgFileName, writer, document)
imgFileName = Path.Combine(sTiffFiles, "B.bmp")
AddImg2Pdf(imgFileName, writer, document)
document.Close()


VB.NET
Private Sub AddImg2Pdf(imgFileName As String, ByRef writer As PdfWriter, ByRef document As Document)

    Dim bitmap As New Bitmap(imgFileName)

    Dim numberOfPages As Integer = bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)

    Dim cb As PdfContentByte = writer.DirectContent

    For page As Integer = 0 To numberOfPages - 1

        bitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, page)

        Dim stream As New System.IO.MemoryStream()

        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png)

        Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(stream.ToArray())

        stream.Close()

        img.ScalePercent(72F / bitmap.HorizontalResolution * 100)

        img.SetAbsolutePosition(0, 0)

        cb.AddImage(img)

        document.NewPage()
    Next
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900