如何将图像添加为pdf标题?

时间:2013-07-19 17:44:49

标签: asp.net vb.net pdf-generation itextsharp

我使用下面的代码生成pdf。有没有办法可以指向图像并将其用作标题? “〜/图片/ Header.png”?任何帮助,将不胜感激。谢谢。

Dim myUniqueFileName = String.Format("{0}.pdf", random)
    Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, New FileStream(path & myUniqueFileName, FileMode.Create))
    Dim ev As New itsEvents
    pdfWrite.PageEvent = ev

    Doc1.Open()
    Dim test As String
    test = Session("PDF")
    Doc1.Add(New Paragraph(test))

    Doc1.Close()
End Sub
Public Class itsEvents
    Inherits PdfPageEventHelper
    Public Overrides Sub OnStartPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)
        Dim ch As New Chunk("This is my Header on page " & writer.PageNumber)
        document.Add(ch)
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

试试这个:

Dim imagepath As String = Server.MapPath(".") & "/logo/Anjanlogo.jpg"
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagepath)
image.ScalePercent(24F)
doc.Add(image)

注意:24F缩放来自以下事实:默认情况下,嵌入的图像为72 DPI,大多数商业打印机使用300 DPI,因此72/300 * 100 = 24%。

要在页面上移动图像,您可以使用SetAbsolutePosition方法,如下所示:

image.SetAbsolutePosition(36F, 36F)

注意:36F是PDF的边距,因此这会将徽标推到PDF角落的左上角,但仍保持边框。