Link to home
Start Free TrialLog in
Avatar of Karen Wilson
Karen WilsonFlag for United States of America

asked on

Merging PDFs with VB.NET

I am trying to append PDFs to a master PDF and my code is replacing the master PDF with the one I want to attached to it.  I need someone to look at my code and tell me what I'm doing wrong.

Many thanks!


Dim jfFile As String = "K:\ConfigMgmt\PDF\POAM\JSCForm\JF2016_10_20.pdf"

        Dim merged As Boolean = True

        Using Stream As New FileStream(jfFile, FileMode.Create)

            Dim document As New Document()
            Dim pdf As New PdfCopy(document, Stream)
            Dim reader As PdfReader = Nothing
            Try
                document.Open()

                Dim getPDF = (From id In d.tblPOAMdetails _
                             Where id.closedDate Is Nothing _
                             And id.nasacsspno = "XXXXX" _
                             Order By id.UPI Ascending _
                             Select id.poamLink).ToList

                For Each file As String In getPDF
                    reader = New PdfReader(file)
                    pdf.AddDocument(reader)
                    reader.Close()
                Next
            Catch generatedExceptionName As Exception
                merged = False
                If reader IsNot Nothing Then
                    reader.Close()
                End If
            Finally
                If document IsNot Nothing Then
                    document.Close()
                End If
            End Try
        End Using
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi Karen;

Try changing this line of code
Using Stream As New FileStream(jfFile, FileMode.Create)

Open in new window

to this
Using Stream As New FileStream(jfFile, FileMode.Append)

Open in new window

FileMode.Create over writes the original file.
Avatar of Karen Wilson

ASKER

The same thing happened.  I forgot to note that I had done that previously as well and reverted it back to create.
Also, the master PDF is in landscape while the attachments are in portrait.  Could that be causing a problem?
Hi Karen;

I have looked at the code again and nothing else sticks out at me. What library are you using? If I can download it from NuGet I will try and test on my computer.
I am using iTextSharp.  I know, it's crazy!
Here is what I am importing as well.

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.pdf.parser

Thanks for your help.
I can get this code to work.

Dim bytes As Byte() = File.ReadAllBytes("K:\ConfigMgmt\PDF\POAM\JSCForm\JF2016_10_20.pdf")

        Dim blackFont As Font = FontFactory.GetFont("Arial", 9, Font.Bold, BaseColor.BLACK)

        Using stream As New MemoryStream()

            Dim reader As New PdfReader(bytes)

            Using stamper As New PdfStamper(reader, stream)

                Dim pages As Integer = reader.NumberOfPages

                For i As Integer = 1 To pages
                    ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, New Phrase("JSC Form 1218 " & i.ToString(), blackFont), 568.0F, 15.0F, 0)
                Next

            End Using

            bytes = stream.ToArray()

        End Using

        File.WriteAllBytes("K:\ConfigMgmt\PDF\POAM\JSCForm\JF2016_10_20.pdf", bytes)
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Fernando!

That did it!!  I will work my code to remove the "original" one and rename the new one and I am good to go.  Thanks so much for your time.  It was making me bonkers!
Not a problem Karen, always glad to help.