以pdf格式逐个添加两个图像 [英] Adding image one by one two images in pdf

查看:92
本文介绍了以pdf格式逐个添加两个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图在vb.net pdf文件中添加两个图像它显示错误一个图像添加正常working.again iam添加代码与另一个图像变量它显示pdf空白与写任何content.how我可以添加两个图像。



我尝试过:



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

使用 ms 作为 MemoryStream()'
如果(img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))然后
img.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg)
ElseIf (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))然后
img.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp)
结束 如果
bytes2 = ms.ToArray()
结束 使用
如果 IsNothing(bytes2)然后
如果 bytes2.GetUpperBound( 0 )> 0 然后
objpdf.ImageFlag = True
objpdf.ByteImageSetting(bytes2, I1
< span class =code-keyword>结束 如果
结束 如果

如果 IsNothing(bytes2)然后
如果 bytes2.GetUpperBound( 0 )> 0 然后
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 如果
结束 如果

解决方案

请检查此代码,它用于通过itextsharp将图像添加到pdf



  Dim  sMergedFiles  As   String  =   D:\Temp \ BASE.PDF 
Dim sTiffFiles 作为 字符串 = D:\Temp \IMG \
Dim document As 文档(PageSize.A4, 50 50 50 50
Dim writer As PdfWriter = PdfWriter.GetInstance(document, FileStream(sMergedFiles,FileMode.CreateNew))
document.Open()
Dim imgFileName 作为 字符串 = Path.Combine(sTiffFiles, A.bmp
AddImg2Pdf(imgFileName,writer,document)
imgFileName = Path.Combine(sTiffFiles, B.bmp
AddImg2Pdf(imgFileName,writer,document)
document.Close( )





 私人 < span class =code-keyword> Sub  AddImg2Pdf(imgFileName  As   String  ByRef  writer 作为 PdfWriter, ByRef 文档作为文档)

Dim 位图 As 位图(imgFileName)

Dim numberOfPages 作为 整数 = bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)

Dim cb 作为 PdfContentByte = writer.DirectContent

对于页面作为 整数 = 0 numberOfPages - 1

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

Dim stream As System.IO.MemoryStre am()

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.Horizo​​ntalResolution * 100

img.SetAbsolutePosition( 0 0

cb.AddImage(img)

文件.NewPage()
下一步
结束


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:

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

解决方案

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

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()



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


这篇关于以pdf格式逐个添加两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆