locked
create pdf with table format which cotain an images RRS feed

  • Question

  • User158566248 posted

    hi, thanks in advance

    i want to create a pdf , which create a pdf with table format, but i want to put an images in that table but i don't understand please help me my code is below,

    -----------------------------------------------------------------------------------

    Dim doc As Document = New Document
            PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\2.pdf", FileMode.Create))
            doc.Open()
            Dim table As Table = New Table(3)
            table.BorderWidth = 1
            table.BorderColor = New Color(0, 0, 255)
            table.Padding = 0
            table.Spacing = 0
            Dim gif As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(Server.MapPath("~/2257Bluehills.jpg"))
            gif.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE
            doc.Add(gif)
            Dim cell As Cell = New Cell("header")
            cell.BorderColor = New Color(12, 34, 230)
            cell.Header = True


            cell.Colspan = 3
            table.AddCell(cell)
            cell = New Cell("example cell with colspan 1 and rowspan 2")
            cell.Rowspan = 2
            cell.BorderColor = New Color(255, 0, 0)
            table.AddCell(cell)

            table.AddCell("1.1")
            table.AddCell("2.1")
            table.AddCell("1.2")
            table.AddCell("2.2")
            table.AddCell("cell test1")
            cell = New Cell("big cell")
            cell.Rowspan = 2
            cell.Colspan = 2
            cell.HorizontalAlignment = Element.ALIGN_CENTER
            cell.VerticalAlignment = Element.ALIGN_MIDDLE
            cell.BackgroundColor = New Color(192, 192, 192)
            table.AddCell(cell)
            table.AddCell("cell test2")
            doc.Add(table)

            doc.Close()
            Response.Redirect("~/2.pdf")

    ==================================================

    please help me.....


    Thursday, October 14, 2010 1:27 AM

Answers

  • User1520641890 posted

    here is a cut-down version implemented with a HTTP Handler:


    <%@ WebHandler Language="VB" Class="imageCell" %>
    Imports System
    Imports System.Web
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    
    Public Class imageCell : Implements IHttpHandler
      Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "application/pdf"
        Dim doc As Document = New Document
        PdfWriter.GetInstance(doc, context.Response.OutputStream)
        doc.Open()
        Dim table = New PdfPTable(3)
        Dim imageCell = New PdfPCell( _
          Image.GetInstance(context.Server.MapPath("outlook.jpg")), _
          True _
        )
        imageCell.Colspan = 3
        table.AddCell(imageCell)
    
        table.AddCell("1.1")
        table.AddCell("1.2")
        table.AddCell("1.3")
        table.AddCell("2.1")
        table.AddCell("2.2")
        table.AddCell("2.3")
        doc.Add(table)
        doc.Close()
      End Sub
    
      Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
          Return False
        End Get
      End Property
    End Class

    please do not copy paste the code into a *.aspx page it will not work. HTTP handlers have much less overhead than an *.aspx, so you should always use if possible when generating PDF. it's understandable, however, that in some unusual cases you may want to create a PDF from a *.aspx page. if so:

    1. only copy the code within the "ProcessRequest" sub into whatever sub/function you will be using in the code-behind file of your web form.
    2. remove the references to the HttpContext instance, i.e you need to replace all instances of "context.Response" with "Response"

    the only other thing you might be wondering about is the constructor of the PdfPCell with two parameters. the second parameter is passed as boolean true so the image is best-fit resized for the table cell.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 15, 2010 1:17 AM
  • User158566248 posted

    still it is not working if i use your code than it work . but if i implement it  this is not work i really not understand pleaseeeeeeeeeeeeeeeeee

    help me my code is below

    =======================================

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Response.ContentType = "application/pdf"
            Dim doc As Document = New Document
            PdfWriter.GetInstance(doc, Response.OutputStream)
            doc.Open()
            Dim table = New PdfPTable(3)
            Dim imageCell = New PdfPCell( _
              Image.GetInstance(Context.Server.MapPath("2257Bluehills.jpg")), _
              True _
            )
            imageCell.Colspan = 3
            table.AddCell(imageCell)

            table.AddCell("1.1")
            table.AddCell("1.2")
            table.AddCell("1.3")
            table.AddCell("2.1")
            table.AddCell("2.2")
            table.AddCell("2.3")
            doc.Add(table)
            doc.Close()
        End Sub

    ======================


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 1:19 AM

All replies

  • User1520641890 posted

            Dim gif As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(Server.MapPath("~/2257Bluehills.jpg"))
            gif.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE
            doc.Add(gif)

    that code isn't even trying to add the image to the table. if you want to add an image to a cell in the table, try something like this:

    Dim imageCell =  New PdfPCell( _
      Image.GetInstance(Server.MapPath("~/2257Bluehills.jpg")), _
      true
    )
    table.AddCell(imageCell)

    your code is using an older version of iTextSharp. you should use the latest version.

    Thursday, October 14, 2010 9:13 AM
  • User158566248 posted

    Thanks for reply me,

    Still It's not working Plese give me other idea please....



    Friday, October 15, 2010 12:15 AM
  • User1520641890 posted

    here is a cut-down version implemented with a HTTP Handler:


    <%@ WebHandler Language="VB" Class="imageCell" %>
    Imports System
    Imports System.Web
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    
    Public Class imageCell : Implements IHttpHandler
      Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "application/pdf"
        Dim doc As Document = New Document
        PdfWriter.GetInstance(doc, context.Response.OutputStream)
        doc.Open()
        Dim table = New PdfPTable(3)
        Dim imageCell = New PdfPCell( _
          Image.GetInstance(context.Server.MapPath("outlook.jpg")), _
          True _
        )
        imageCell.Colspan = 3
        table.AddCell(imageCell)
    
        table.AddCell("1.1")
        table.AddCell("1.2")
        table.AddCell("1.3")
        table.AddCell("2.1")
        table.AddCell("2.2")
        table.AddCell("2.3")
        doc.Add(table)
        doc.Close()
      End Sub
    
      Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
          Return False
        End Get
      End Property
    End Class

    please do not copy paste the code into a *.aspx page it will not work. HTTP handlers have much less overhead than an *.aspx, so you should always use if possible when generating PDF. it's understandable, however, that in some unusual cases you may want to create a PDF from a *.aspx page. if so:

    1. only copy the code within the "ProcessRequest" sub into whatever sub/function you will be using in the code-behind file of your web form.
    2. remove the references to the HttpContext instance, i.e you need to replace all instances of "context.Response" with "Response"

    the only other thing you might be wondering about is the constructor of the PdfPCell with two parameters. the second parameter is passed as boolean true so the image is best-fit resized for the table cell.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 15, 2010 1:17 AM
  • User158566248 posted

    still it is not working if i use your code than it work . but if i implement it  this is not work i really not understand pleaseeeeeeeeeeeeeeeeee

    help me my code is below

    =======================================

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Response.ContentType = "application/pdf"
            Dim doc As Document = New Document
            PdfWriter.GetInstance(doc, Response.OutputStream)
            doc.Open()
            Dim table = New PdfPTable(3)
            Dim imageCell = New PdfPCell( _
              Image.GetInstance(Context.Server.MapPath("2257Bluehills.jpg")), _
              True _
            )
            imageCell.Colspan = 3
            table.AddCell(imageCell)

            table.AddCell("1.1")
            table.AddCell("1.2")
            table.AddCell("1.3")
            table.AddCell("2.1")
            table.AddCell("2.2")
            table.AddCell("2.3")
            doc.Add(table)
            doc.Close()
        End Sub

    ======================


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 1:19 AM
  • User158566248 posted

    hi,

    Thanks it's working it 's very helpful for me


    Thank you very much

    Monday, October 18, 2010 10:45 PM