Solved

Merging PDFs with VB.NET

Posted on 2016-10-20
10
31 Views
Last Modified: 2016-10-20
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
0
Comment
Question by:Karen Wilson
  • 6
  • 4
10 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41852576
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.
0
 

Author Comment

by:Karen Wilson
ID: 41852608
The same thing happened.  I forgot to note that I had done that previously as well and reverted it back to create.
0
 

Author Comment

by:Karen Wilson
ID: 41852610
Also, the master PDF is in landscape while the attachments are in portrait.  Could that be causing a problem?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41852647
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.
0
 

Author Comment

by:Karen Wilson
ID: 41852651
I am using iTextSharp.  I know, it's crazy!
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Karen Wilson
ID: 41852654
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.
0
 

Author Comment

by:Karen Wilson
ID: 41852665
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)
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41852802
Hi Karen;

I tried to lookup the documentation on PdfCopy.AddDocument but it seems to be missing from there documentation. I have found a workaround. By inserting the main document into the list of PDF's to be copied from your query fixes the problem. I do not know if it is a problem or not because it may require an empty document to write to. I have commented two lines in your code for you to change/add.
'' Added _New to the end of the file name so that it creates an empty file wher to copy all the PDF files to.
Dim jfFile As String = "K:\ConfigMgmt\PDF\POAM\JSCForm\JF2016_10_20_New.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

        '' Add the main document into the list as the first document to be
        '' copied into the empty file created above
        getPDF.Insert(0, "K:\ConfigMgmt\PDF\POAM\JSCForm\JF2016_10_20.pdf")

        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

Open in new window

0
 

Author Closing Comment

by:Karen Wilson
ID: 41852824
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!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41852836
Not a problem Karen, always glad to help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
*Adobe Acrobat 9 was used for this article.  Particular steps may vary depending on software versions. Adobe Acrobat has many, many variables that my be utilized to customize your forms for clarity and ease of use. The Form Editing Tool will be y…
In this second video of the Xpdf series, we discuss and demonstrate the PDFimages utility, which, in a single command, is able to extract all the images from a PDF file and save each one in a separate image file (PBM, PPM, or JPG). Download and inst…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now