Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to use iText7 for the first time. I need to incorporate it into a VB.NET project. Users need to be able to select 2 or more PDFs from a list and then my iText7 code will combine them into a single PDF and save it to disc.

The PDF documents are stored in SQL Server file tables. I'm letting users select specific PDFs from a list. My code loops through the chosen PDFs and puts each into a byte array and then I'm creating a memoryStream from there because I figured that's what iText7 wants.

I found different examples about how to go about this, but they aren't working for me at all. Most of these examples don't really do what I need. I am not able to find sufficient documentation for iText7 and some of the exceptions I'm getting are useless (like "unknown PDFexception").

What I have tried:

 While rdr.Read

     Dim customerScorecardID As Int32 = Convert.ToInt32(rdr.GetValue(rdr.GetOrdinal("CustomerScorecardID")))
     Dim pdfFileStream As Byte() = DirectCast(rdr.GetValue(rdr.GetOrdinal("file_stream")), Byte())
     Dim ms As New MemoryStream(pdfFileStream)

     'If pdfBundle Is Nothing Then
     '    pdfBundle = New PdfWriter(msBundle)
     '    'pdfSource.CopyPagesTo(1, pdfSource.GetNumberOfPages, pdfBundle)
     '    pdfSource = New PdfDocument(New PdfReader(ms))
     '    'pdfSource.CopyPagesTo(1, pdfSource.GetNumberOfPages, pdfBundle)
     'End If

     'PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
     'PdfMerger merger = new PdfMerger(pdf);

     '//Add pages from the first document
     'PdfDocument firstSourcePdf = new PdfDocument_
(new PdfReader(SRC1));
     'merger.merge(firstSourcePdf, _
            1, firstSourcePdf.getNumberOfPages());

     '//Add pages from the second pdf document
     'PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2));
     'merger.merge(secondSourcePdf, _
           1, secondSourcePdf.getNumberOfPages());


     Dim msTarget As MemoryStream = New MemoryStream()
     Dim pdf As PdfDocument = _
       New PdfDocument(New PdfWriter("D:\test.pdf"))
     'Dim mgr As PdfMerger = New PdfMerger(pdf)

     pdfSource = New PdfDocument(New PdfReader(ms))

     'mgr.Merge(pdfSource, 1, pdfSource.GetNumberOfPages)

 End While
Updated 2-Oct-23 5:14am
[no name] 25-Sep-23 15:12pm    
Your "business case" makes no sense. User can easily print "multiple" PDF's; "combining them" is pointless and unproductive.

1 solution

I just did a Google Search: iText7 merge 2 PDF - Google[^] and found this: Is it possible to merge several pdfs using iText7 - StackOverflow[^].

The solution given is from the itextpdf java example. Here is the .Net version: iText: Jump-Start Tutorial for .NET[^]. There is this section that explicitly explains what you are trying to do (using C#): Chapter 6: Reusing existing PDF documents | .NET - section: Merging documents with PdfMerger[^]

Here is the code from that page (and yes, it looks the same as what you have posted, but keep reading):
//Initialize PDF document with output intent
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdf);

//Add pages from the first document
PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1));
merger.Merge(firstSourcePdf, 1, firstSourcePdf.GetNumberOfPages());

//Add pages from the second pdf document
PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2));
merger.Merge(secondSourcePdf, 1, secondSourcePdf.GetNumberOfPages());


Looks pretty straight forward to me. However, it's not in Vb. That is easily fixed by using ChatGPT[^]. Here is how I would ask ChatGPT[^] to translate the code for me:
Convert the following code from C# to VB.Net:

[paste code here]

The output has both an explanation and code. Here is the code generated from the above query:
' Initialize PDF document with output intent
Dim pdf As New PdfDocument(New PdfWriter(dest))
Dim merger As New PdfMerger(pdf)

' Add pages from the first document
Dim firstSourcePdf As New PdfDocument(New PdfReader(SRC1))
merger.Merge(firstSourcePdf, 1, firstSourcePdf.GetNumberOfPages())

' Add pages from the second pdf document
Dim secondSourcePdf As New PdfDocument(New PdfReader(SRC2))
merger.Merge(secondSourcePdf, 1, secondSourcePdf.GetNumberOfPages())


If you're not happy with that solution, there are others in that StackOverflow link + others in the Google Search above.

If you find C# solutions, you now know how to easily convert to VB.Net. A warning though... ChatGPT is not always accurate, so you may need to do a little work to debug and complete the conversions.

Lastly, I suggest to create a prototyping console application to test any solution BEFORE adding to your main project. This will help to focus in on the part you are trying to implement and remove the distractions of merging into your application. It's quick and clean. Once you have it working, then focus on adding it to your main project. Quarantine, evaluate/test, then implement.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900