itexsharp and Images RRS feed

  • Question

  • User1811554030 posted

    Guys is there a way to dynamically add images to an itextsharp pdf document, as header on maybe 3 pages?

    what I have now is that, my pdf doc is created dynamically and data from the database tb is rendered into the pdf through a gridview, and it dynamically adds more pages according to what is in the table,

    is there  a similar way to do this but for images?, right now I can render an image per single page

    Monday, August 27, 2012 4:12 AM

All replies

  • User1630798415 posted

    If you want to change the contents of an existing PDF file and add extra content such as watermarks, pagenumbers, extra headers, PdfStamper is the object you need. I have successfully used the following code to insert an image into an existing pdf file to a given absolute position:

    using System.IO; 
    using iTextSharp.text; 
    using iTextSharp.text.pdf; 
    class Program 
        static void Main(string[] args) 
            using (Stream inputPdfStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read)) 
            using (Stream inputImageStream = new FileStream("some_image.jpg", FileMode.Open, FileAccess.Read, FileShare.Read)) 
            using (Stream outputPdfStream = new FileStream("result.pdf", FileMode.Create, FileAccess.Write, FileShare.None)) 
                var reader = new PdfReader(inputPdfStream); 
                var stamper = new PdfStamper(reader, outputPdfStream); 
                var pdfContentByte = stamper.GetOverContent(1); 
                Image image = Image.GetInstance(inputImageStream); 
                image.SetAbsolutePosition(100, 100); 

    When you insert the image you have the possibility to resize it. You can take a look at transformation matrix in the iTextSharp documentation.

    Monday, August 27, 2012 4:30 AM
  • User1811554030 posted

    I understand....I see the code, but I don't see where it renders the same image for 3 or 4 pages dynamically, or am I missing something

    Monday, August 27, 2012 4:43 AM
  • User1630798415 posted

    you ca see the abolute position where image is rendered. In you case, if want to add image on multiple pages you have to set multiple abolute positions.


    Monday, August 27, 2012 5:07 AM
  • User1811554030 posted

    Ok, I understand all that guy, but in my case, I do not set a specific number of pages to create, this pages are dynamically added by the code according to the database, I have a select statement which does all this, but now I want to add the same image as a header to (x) number of pages generated.

    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
            'PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
            Using stream As New MemoryStream()
                Dim sw As New StringWriter()
                Dim hTextWriter As New HtmlTextWriter(sw)
                Dim sr As New StringReader(sw.ToString())
                Dim htmlparser As New HTMLWorker(pdfDoc)

                'Dim path As String = "C:\PDF\Results.pdf"
                'Dim fs As FileStream = File.Create(path)
                Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, stream)
                'PdfContentByte(cb = writer.DirectContent)
                ' Dim cb As PdfContentByte = writer.DirectContent
                ' cb.BeginText()
                'cb.SetTextMatrix(100, 400)
                'Dim bf As BaseFont
                ' cb.SetFontAndSize(bf)
                pdfDoc.Add(New Paragraph(" "))
                pdfDoc.Add(New Chunk(" "))
                pdfDoc.Add(New Paragraph(" "))
                pdfDoc.Add(New Paragraph(" "))
                pdfDoc.Add(New Paragraph(" "))
                pdfDoc.Add(New Paragraph("Money Anchor Profile"))
                pdfDoc.Add(New Paragraph("This Profile has been prepared for " & txtFirstName.Text & " " & txtUser_Surname.Text))
                Dim Adviseurl As String = (Server.MapPath("images/pdf_cover_banner.jpg"))
                Dim coloumnImage As String = (Server.MapPath("images/pdf_cover_column.jpg"))
                'pdfDoc.Add(New Paragraph(""))

                Dim jpg As Global.iTextSharp.text.Image = Global.iTextSharp.text.Image.GetInstance(New Uri(Adviseurl))
                jpg.SetAbsolutePosition(pdfDoc.PageSize.Width - 300.6F - 126.0F,
                pdfDoc.PageSize.Height - 1.0F - 100.0F)
                pdfDoc.PageCount = True
                ' pdfDoc.AddHeader = True

                Dim other As Global.iTextSharp.text.Image = Global.iTextSharp.text.Image.GetInstance(New Uri(coloumnImage))
                other.SetAbsolutePosition(pdfDoc.PageSize.Width - 40.0F - 560.0F,
                pdfDoc.PageSize.Height - 780.0F - 300.6F)

                writer.CloseStream = False
                stream.Position = 0

                Response.ContentType = "pdf/application"
                Response.AddHeader("content-disposition", "attachment;filename=Results.pdf")
                Response.OutputStream.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length)

                End Using
        End Sub

    Monday, August 27, 2012 5:27 AM
  • User-1760637409 posted

    Hi Friend,

    using iTextSharp.text;
    using iTextSharp.text.pdf;

                     Document doc1 = new Document();
                    string strID = "";
                    string NotesText = "";

                    strID = "ID_12121";
                    Discription_Notes_Text = "Common Text or Discription will goes here";

                    // Generation Path    
                    string path = @"F:\Generated_Files\";
                    string filename = "File_Name.pdf";
                    PdfWriter writer = PdfWriter.GetInstance(doc1, new FileStream(filename, FileMode.Create));
                    // Add the Image, this Section you can move to botton or anywhere in between of document also.
                    iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(@"F:\Your_Image_logo.JPG");
                    jpeg.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;

                    // Add the line in your document.
                    PdfContentByte cb = writer.DirectContent;
                    doc1.Add(new Paragraph("*******************************************"));
                    //Add the contents to the itextCharp paragraph object
                    doc1.Add(new Paragraph(Discription_Notes_Text));                              

    // See the below link for more reference using this you can do more formatting.
    // Working with Fonts, Large Data and even you can create Link & Bookmarks as well.

    Hope this will work for you.

    Monday, August 27, 2012 5:45 AM