Add image to PDF with iTextSharp and VB.Net

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

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

            Dim dt As New DataTable()
            Dim row As DataRow = dt.NewRow()
            row("Batch") = Batch.ToString    '& i.ToString()
            Dim img1 As System.Drawing.Image = Nothing
            For i As Integer = 0 To dt.Rows.Count - 1
                If i <> 0 Then
                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) 
            Next i


            Catch ex As Exception
            End Try

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
Hi PNRT, You can open existing PDF document by using the FileMode.Append instead of FileMode.Create. This shall open an existing PDF based on the path you have given, if that file does not exists it shall create a new one with FileMode as Append.
I tested with this change and it worked for me. However it overrides the previously draw bar code image at the same position.
if you want that every time the bar code image to be added to a new page then the above logic may need to be modified.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.