We help IT Professionals succeed at work.
Get Started

Add image to PDF with iTextSharp and VB.Net

PNRT
PNRT asked
on
3,723 Views
Last Modified: 2015-12-07
Hi Experts

I have the following code using iTextSharp.   It creates a barcode and inserts it into a PDF document created by iTextSharp

The code works correctly but only with newly created documents.   I need to be able to do the same thing but with an already existing PDF.   I have searched and found many examples but all assume a newly created document which doesn't help me.

Any assistance would be appreciated


        Dim Batch As String = "12345"

        'Define a new PDF Doc
        Dim doc As New Document(New iTextSharp.text.Rectangle(350, 400), 5, 5, 1, 1)  ''the size of the rectangle width x height

        Try
            Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("C:\somepath\TestLabel.abc", FileMode.Create))
            doc.Open()

            Dim dt As New DataTable()
            dt.Columns.Add("Batch")
            Dim row As DataRow = dt.NewRow()
            row("Batch") = Batch.ToString    '& i.ToString()
            dt.Rows.Add(row)
          
            Dim img1 As System.Drawing.Image = Nothing
            For i As Integer = 0 To dt.Rows.Count - 1
                If i <> 0 Then
                    doc.NewPage()
                End If

                Dim cb As iTextSharp.text.pdf.PdfContentByte = writer.DirectContent
                Dim bc As iTextSharp.text.pdf.Barcode128 = New Barcode128() 
                bc.TextAlignment = Element.ALIGN_LEFT
                bc.Code = dt.Rows(i)("Batch").ToString()
                bc.StartStopText = False
                bc.CodeType = iTextSharp.text.pdf.Barcode128.EAN13
                bc.Extended = True

                Dim img As iTextSharp.text.Image = bc.CreateImageWithBarcode(cb, iTextSharp.text.Color.BLACK, iTextSharp.text.Color.BLACK)

                cb.SetTextMatrix(100.5F, 320.0F)
                img.ScaleToFit(240, 600)
                img.SetAbsolutePosition(5.5F, 320) 
                cb.AddImage(img)
            Next i

            doc.Close()

            Catch ex As Exception
                doc.Close()
                MsgBox(ex.ToString)
            End Try

Open in new window

Comment
Watch Question
Software Architect
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE